-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
50 lines (46 loc) · 1.59 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Credentials from 'next-auth/providers/credentials';
import NextAuth from 'next-auth';
import { login } from '@/actions/auth/login.action';
import { getUser } from '@/actions/user/get-user.action';
import { isTokenValid } from '@/utils/functions/is-token-valid';
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Credentials({
id: 'credentials',
name: 'credentials',
credentials: {
username: { label: 'Username', type: 'text' },
password: { label: 'Password', type: 'password' }
},
async authorize(credentials) {
const user = await login({
username: credentials.username,
password: credentials.password
});
if (!user) return null;
const userInfo = await getUser(user.token);
if (!userInfo) return null;
const userInformation = { ...user, ...userInfo };
return {
user: userInformation,
accessToken: user.token.split(' ')[1]
};
}
})
],
callbacks: {
async jwt({ token, user }) {
return { ...token, ...user };
},
async session({ session, token }) {
if (!isTokenValid(token.accessToken)) return null;
session.accessToken = token.accessToken;
session.user = token.user;
return session;
}
},
pages: {
signIn: '/login'
},
trustHost: true
});