You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.1 KiB
66 lines
2.1 KiB
import NextAuth, {Session, User} from 'next-auth';
|
|
import CredentialsProvider from 'next-auth/providers/credentials';
|
|
import {JWT} from "next-auth/jwt";
|
|
import {AdapterUser} from "next-auth/adapters";
|
|
|
|
import {environment} from "../../../environments/environment";
|
|
import {signInStrapi} from '../../../libs/auth';
|
|
|
|
export default NextAuth({
|
|
|
|
// Configure one or more authentication providers
|
|
providers: [
|
|
CredentialsProvider({
|
|
name: 'Sign in with Email',
|
|
credentials: {
|
|
email: {label: 'Email', type: 'text'},
|
|
password: {label: 'Password', type: 'password'},
|
|
},
|
|
async authorize(credentials, req) {
|
|
/**
|
|
* This function is used to define if the user is authenticated or not.
|
|
* If authenticated, the function should return an object contains the user data.
|
|
* If not, the function should return `null`.
|
|
*/
|
|
if (credentials == null) return null;
|
|
/**
|
|
* credentials is defined in the config above.
|
|
* We can expect it contains two properties: `email` and `password`
|
|
*/
|
|
try {
|
|
const {user, jwt} = await signInStrapi({
|
|
email: credentials.email,
|
|
password: credentials.password,
|
|
});
|
|
return {...user, jwt};
|
|
} catch (error) {
|
|
// Sign In Fail
|
|
return null;
|
|
}
|
|
},
|
|
}),
|
|
],
|
|
callbacks: {
|
|
session: async ({session, token}: {session: Session, token: JWT}) => {
|
|
(session as any).id = token.id;
|
|
(session as any).jwt = token.jwt;
|
|
(session as any).user = {
|
|
avatar: (token.user as any).avatar,
|
|
username: (token.user as any).username,
|
|
email: (token.user as any).email,
|
|
};
|
|
return Promise.resolve(session);
|
|
},
|
|
jwt: async ({token, user}: {token: JWT, user: User | AdapterUser}) => {
|
|
const isSignIn = user ? true : false;
|
|
if (isSignIn) {
|
|
token.id = user.id;
|
|
token.jwt = (user as any).jwt;
|
|
token.user = (user as any);
|
|
}
|
|
return Promise.resolve(token);
|
|
},
|
|
},
|
|
secret: environment.nextAuthSecret
|
|
});
|