first commit

This commit is contained in:
2024-01-15 23:13:55 +01:00
commit 39705a7b43
286 changed files with 47391 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
import { useApiContext } from '@/common/providers/api-context'
import { useSession } from 'next-auth/react'
import { PropsWithChildren, useLayoutEffect } from 'react'
export const AuthToApiContextConnectionProvider = ({
children,
}: PropsWithChildren) => {
const apiContext = useApiContext()
const { data: session } = useSession()
const apiSession = session?.apiSession
useLayoutEffect(() => {
apiContext.setAuthorizationToken(apiSession?.accessToken)
}, [apiContext, apiSession])
return <>{children}</>
}

View File

@@ -0,0 +1,2 @@
'use client'
export * from './auth-to-apicontext-connection.provider'

View File

@@ -0,0 +1,57 @@
import { getApiContext } from '@/common/providers/api-context/api-context.default'
import { AuthOptions, User } from 'next-auth'
import CredentialsProvider from 'next-auth/providers/credentials'
export const authOptions: AuthOptions = {
session: {
strategy: 'jwt',
},
pages: {
signIn: '/auth/sign-in',
},
callbacks: {
async session({ session, token }) {
if (token?.accessToken && session) {
// Update server side API_CONTEXT
getApiContext().setAuthorizationToken(token.accessToken)
session.apiSession = {
accessToken: token.accessToken,
refreshToken: token.refreshToken,
}
}
return session
},
async jwt({ token, user }) {
if (user?.apiSession) {
token.accessToken = user.apiSession.accessToken
token.refreshToken = user.apiSession.refreshToken
}
return token
},
},
providers: [
CredentialsProvider({
name: 'custom-credentials',
credentials: {
email: { type: 'email' },
password: { type: 'password' },
},
authorize: async (credentials, _req) => {
// TODO: Connect with login API in the backend
const user: User = {
id: '1',
email: 'dpenai@pibone.com',
name: 'Dani Peña Iglesias',
role: 'admin',
// TODO: Set the incoming api token here, if needed
apiSession: {
accessToken: 'jwt-token',
// refreshToken: 'refresh-token-if-any',
},
}
return credentials?.password === 'safe-password' ? user : null
},
}),
],
}

View File

@@ -0,0 +1,10 @@
import { Session } from 'next-auth'
import { SessionProvider } from 'next-auth/react'
import { PropsWithChildren } from 'react'
export const AuthProvider = ({
children,
session,
}: PropsWithChildren<{ session?: Session }>) => {
return <SessionProvider session={session}>{children}</SessionProvider>
}

View File

@@ -0,0 +1,2 @@
'use client'
export * from './auth.provider'

View File

@@ -0,0 +1,28 @@
import { ApiSession, DefaultSession } from 'next-auth'
import { DefaultJWT } from 'next-auth/jwt'
declare module 'next-auth' {
declare type Role = 'user' | 'admin'
declare interface ApiSession {
accessToken: string
refreshToken?: string
}
declare interface User {
id: string
role: Role
image?: string
name?: string
email?: string
apiSession?: ApiSession
}
declare interface Session extends DefaultSession {
apiSession?: ApiSession
}
}
declare module 'next-auth/jwt' {
declare interface JWT
extends WithOptional<ApiSession, 'accessToken'>,
DefaultJWT {}
}