Created
February 10, 2024 13:15
-
-
Save thexeromin/5e55f4fdd40405b2d0bd58c11bfab642 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import NextAuth from 'next-auth' | |
import type { NextAuthOptions } from 'next-auth' | |
import CredentialsProvider from 'next-auth/providers/credentials' | |
import axios, { AxiosResponse } from 'axios' | |
import type { JWT } from 'next-auth/jwt' | |
import type { User, Session, Account } from 'next-auth' | |
interface AuthResponse { | |
id: string | |
customer_id: string | |
username: string | |
email: string | |
verification_status: true | |
user_type: string | |
tokens: { | |
refresh: string | |
access: string | |
} | |
message: string | |
} | |
type UserExtendedd = User & AuthResponse | |
interface JwtExtended { | |
name: string | |
email: string | |
sub: string | |
accessToken: string | |
refreshToken: string | |
expiresAt: number | |
userType: string | |
userId: number | |
iat?: number | |
exp?: number | |
jti?: string | |
} | |
interface JwtParams { | |
token: JWT | JwtExtended | |
user?: UserExtendedd | |
account: Account | |
} | |
interface SessionParams extends JWT { | |
session: Session | |
user?: UserExtendedd | |
} | |
type combinedJWT = JWT | JwtExtended | |
const refreshAccessToken = async (token: combinedJWT) => { | |
const response: AxiosResponse<{ access: string }, any> = await axios.post( | |
`${process.env.BACKEND}/api/token/refresh/`, | |
{ | |
refresh: token.refreshToken, | |
} | |
) | |
if (response.status === 200 && response.data.access) { | |
console.log('access: ' + response.data.access) | |
return { | |
...token, | |
accessToken: response.data.access, | |
expiresAt: Date.now() + 5000, | |
} | |
} | |
return { | |
...token, | |
error: 'RefreshAccessTokenError', | |
} | |
} | |
export const authOptions = { | |
providers: [ | |
CredentialsProvider({ | |
name: 'Credentials', | |
credentials: { | |
otp: { label: 'otp', type: 'text' }, | |
userId: { label: 'userId', type: 'text' }, | |
}, | |
async authorize(credentials) { | |
const user: AxiosResponse<AuthResponse> = await axios.post( | |
`${process.env.BACKEND}/auth/login/${credentials?.userId}/`, | |
{ otp: credentials?.otp } | |
) | |
if (user.status === 200) { | |
return { | |
id: user.data.id, | |
customer_id: user.data.customer_id, | |
username: user.data.username, | |
email: user.data.email, | |
user_type: user.data.user_type, | |
tokens: user.data.tokens, | |
} | |
} | |
return null | |
}, | |
}), | |
], | |
pages: { | |
signIn: '/signin', | |
}, | |
callbacks: { | |
async jwt({ token, user }: any) { | |
if (user) { | |
return { | |
...token, | |
accessToken: user.tokens.access, | |
refreshToken: user.tokens.refresh, | |
expiresAt: Date.now() + 5000, | |
userType: user.user_type, | |
name: user.username, | |
email: user.email, | |
userId: user.id, | |
customer_id: user.customer_id, | |
} | |
} | |
if (Date.now() < token.expiresAt) { | |
return token | |
} | |
const newToken = await refreshAccessToken(token) | |
return newToken | |
}, | |
async session({ session, token, user }: any) { | |
session.accessToken = token.accessToken | |
if (user) { | |
session.user = { | |
...user, | |
id: token.userId, | |
name: token.name, | |
email: token.email, | |
customer_id: token.customer_id, | |
user_type: token.userType, | |
} | |
} else { | |
session.user = { | |
id: token.userId, | |
customer_id: token.customer_id, | |
email: token.email, | |
name: token.name, | |
user_type: token.userType, | |
} | |
} | |
return session | |
}, | |
}, | |
debug: false, | |
} as NextAuthOptions | |
export default NextAuth(authOptions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment