Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DPCP-19] Nyx: Upgrading to Next Auth 5 + Prisma Adapter #16

Merged
merged 26 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
555ab41
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
e06baeb
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
f2eb590
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
eb7bdd0
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
519f176
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
684377e
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
db1051c
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
7ad5b78
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
1b195ec
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
a652445
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
eeec385
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
42d3f26
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
7538294
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
52dcb06
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
df08b39
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
e1fabcb
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
52e34c6
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
f000540
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
6b95820
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
a11610b
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
66739d4
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
795fe27
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
6f6f318
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
700ae58
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
c6534bd
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
5fca595
ar(temp) auth stuff messy
angeloreale Jul 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run format:fix
# npm run format:fix
# npm run build
git add .
# git commit -m "husky(lint)"
116 changes: 42 additions & 74 deletions lib/auth/constants.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,48 @@
// constants.ts TS-Doc?
import type { AuthOptions } from 'next-auth';
import GithubProvider from 'next-auth/providers/github';
import GoogleProvider from 'next-auth/providers/google';
import AppleProvider from 'next-auth/providers/apple';
import FacebookProvider from 'next-auth/providers/facebook';
import EmailProvider from 'next-auth/providers/email';
// import InstagramProvider from 'next-auth/providers/instagram';

export const authOptions: AuthOptions = {
providers: [
EmailProvider({
server: process.env.EMAIL_SERVER as string,
from: process.env.EMAIL_FROM as string,
// maxAge: 24 * 60 * 60, // How long email links are valid for (default 24h)
}),
GithubProvider({
clientId: process.env.GITHUB_ID as string,
clientSecret: process.env.GITHUB_SECRET as string,
}),
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
}),
AppleProvider({
clientId: process.env.APPLE_CLIENT_ID as string,
clientSecret: process.env.APPLE_CLIENT_SECRET as string,
}),
FacebookProvider({
clientId: process.env.FACEBOOK_CLIENT_ID as string,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET as string,
}),
// ...add more providers here
],
session: {
strategy: 'jwt',
},
events: {},
callbacks: {
async signIn() {
// extra sign-in checks
return true;
},
async redirect({ url, baseUrl }) {
return url.startsWith(baseUrl) ? Promise.resolve(url) : Promise.resolve(baseUrl);
},
async jwt({ user, token }) {
if (user) {
// Note that this if condition is needed
token.user = { ...user };
}
return token;
},
async session({ session, token }) {
if (token?.user) {
// Note that this if condition is needed
session.user = token.user;
}
return session;
},
export const providers = [
{ id: 'email', name: 'Email', type: 'email' },
{ id: 'github', name: 'GitHub', type: 'oauth' },
{ id: 'google', name: 'Google', type: 'oidc' },
{ id: 'apple', name: 'Apple', type: 'oidc' },
{ id: 'facebook', name: 'Facebook', type: 'oauth' },
];

const methods = {
signIn: () => {},
signOut: async () => {
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/signout`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ csrfToken: await methods.getCsrf() }),
});
return response;
} catch (e) {
console.error(e);
}
},
cookies: {
pkceCodeVerifier: {
name: 'next-auth.pkce.code_verifier',
options: {
httpOnly: true,
sameSite: 'none',
path: '/',
secure: true,
},
},
getCsrf: async () => {
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/csrf`);
const csrf = await response.json();
return csrf.csrfToken;
} catch (e) {
console.error(e);
}
},
pages: {
signIn: '/signin',
signOut: '/',
error: '/error', // Error code passed in query string as ?error=
verifyRequest: '/verify', // (used for check email message)
newUser: '/services/rickmorty', // New users will be directed here on first sign in (leave the property out if not of interest)
getSession: async () => {
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/session`);
const session = await response.json();
return session;
} catch (e) {
console.error(e);
}
},
};

export const { signIn, signOut, getCsrf, getSession } = methods;
2 changes: 1 addition & 1 deletion lib/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// index.ts
export { authOptions } from './constants';
export { getCsrf, getSession, signIn, signOut, providers } from './constants';
5 changes: 2 additions & 3 deletions lib/model/decorators/hypnos-public-decorator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint @typescript-eslint/consistent-type-assertions:0, @typescript-eslint/no-unused-vars:0 */
// hypnos-public-decorator.ts input: hypnos meta; output: decorated hypnos meta;
'use server';
import type { UserSchema } from '@types';
import type { ICard } from '@dreampipcom/oneiros';

/* private */
const decorateListing = (listing: Record<string, any>, uMeta: UserSchema): ICard => {
const decorateListing = (listing: Record<string, any>, uMeta: any): ICard => {
const decd: ICard = {
id: `list__card--${listing?.title?.es}`,
className: '',
Expand All @@ -29,6 +28,6 @@ const decorateListing = (listing: Record<string, any>, uMeta: UserSchema): ICard
/* public */
export const decorateHypnosPublicListings = async (listings: Record<string, any>[], uid: string): Promise<ICard[]> => {
// const uMeta: UserSchema = await getUserMeta({ email: uid });
const decd: ICard[] = listings?.map((card) => decorateListing(card, { email: uid } as UserSchema));
const decd: ICard[] = listings?.map((card) => decorateListing(card, { email: uid }));
return decd;
};
6 changes: 3 additions & 3 deletions lib/model/decorators/rm-decorator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint @typescript-eslint/consistent-type-assertions:0 */
// rm-decorator.ts input: rm meta; output: decorated rm meta;
'use server';
import type { IDCharacter, INCharacter, UserSchema } from '@types';
import type { IDCharacter, INCharacter } from '@types';

/* private */
const decorateCharacter = (character: INCharacter, uMeta: UserSchema): IDCharacter => {
const decorateCharacter = (character: INCharacter, uMeta: any): IDCharacter => {
const decd: IDCharacter = { ...character };
decd.favorite = undefined;
if (uMeta?.rickmorty?.favorites?.characters?.includes(character?.id)) decd.favorite = true;
Expand All @@ -15,6 +15,6 @@ const decorateCharacter = (character: INCharacter, uMeta: UserSchema): IDCharact
/* public */
export const decorateRMCharacters = async (characters: INCharacter[], uid: string): Promise<IDCharacter[]> => {
// const uMeta: UserSchema = await getUserMeta({ email: uid });
const decd: IDCharacter[] = characters.map((char) => decorateCharacter(char, { email: uid } as UserSchema));
const decd: IDCharacter[] = characters.map((char) => decorateCharacter(char, { email: uid }));
return decd;
};
18 changes: 7 additions & 11 deletions lib/state/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
'use client';
import type { IAuthContext, IGlobalContext, IRMContext, IHypnosPublicContext } from '@types';
import { useContext, useState, useEffect, useRef } from 'react';
import { SessionProvider } from 'next-auth/react';
import { AuthContext, GlobalContext, RMContext, HypnosPublicContext } from '@state';
import { Globals } from '@dreampipcom/oneiros';

Expand All @@ -13,7 +12,6 @@ export function RootProviders({ children }: { children: React.ReactNode }) {
const [authState, setAuthState] = useState<IAuthContext>({ ...authContext });
const [globalState, setGlobalState] = useState<IGlobalContext>({ ...globalContext });
const init = useRef(false);
const base = process.env.NEXT_PUBLIC_NEXUS_BASE_PATH || '';

useEffect(() => {
if (!init.current && authContext && !authState?.setter) {
Expand All @@ -28,15 +26,13 @@ export function RootProviders({ children }: { children: React.ReactNode }) {
if (!authState?.initd) return;

return (
<SessionProvider basePath={base ? `${base}/api/auth` : '/api/auth'}>
<AuthContext.Provider value={authState}>
<GlobalContext.Provider value={globalState}>
<Globals theme={globalState?.theme || 'dark'}>
<main className="min-h-screen">{children}</main>
</Globals>
</GlobalContext.Provider>
</AuthContext.Provider>
</SessionProvider>
<AuthContext.Provider value={authState}>
<GlobalContext.Provider value={globalState}>
<Globals theme={globalState?.theme || 'dark'}>
<main className="min-h-screen">{children}</main>
</Globals>
</GlobalContext.Provider>
</AuthContext.Provider>
);
}

Expand Down
Loading
Loading