first commit
This commit is contained in:
@@ -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}</>
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
'use client'
|
||||
export * from './auth-to-apicontext-connection.provider'
|
||||
@@ -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
|
||||
},
|
||||
}),
|
||||
],
|
||||
}
|
||||
@@ -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>
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
'use client'
|
||||
export * from './auth.provider'
|
||||
28
examples/data-fetching/src/modules/auth/providers/auth/nextauth.d.ts
vendored
Normal file
28
examples/data-fetching/src/modules/auth/providers/auth/nextauth.d.ts
vendored
Normal 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 {}
|
||||
}
|
||||
Reference in New Issue
Block a user