-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
66 lines (52 loc) · 1.71 KB
/
auth.ts
File metadata and controls
66 lines (52 loc) · 1.71 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { AuthOptions, getServerSession, User, } from "next-auth"
import GitHubProvider from "next-auth/providers/github";
import CredentialsProvider from "next-auth/providers/credentials";
import { getUserForAuth, providerloginAction } from "./utils/user-auth";
import { credType } from "./helper/authTypes";
const authOptions: AuthOptions = {
providers: [
GitHubProvider({
clientId: process.env.GITHUB_CLIENT as string,
clientSecret: process.env.GITHUB_SECRET as string,
}),
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" },
},
async authorize(credentials){
const user =(await getUserForAuth(credentials as credType) as unknown) as User;
return user;
}
})
],
session: {
strategy: "jwt",
},
callbacks: {
async signIn({ user, account }) {
if (account!.provider === 'github') {
providerloginAction(user);
}
return true;
},
async jwt({ token, user }) {
if (user) {
// On first time sign-in, add user data to JWT
token.id = user.id;
token.email = user.email;
}
return token;
},
async session({ session, token }) {
// Pass the user data from the JWT token to the session
//storing the user data so that we can access it throughout whole application
session.user!.id = token.id as string;
session.user!.email = token.email;
return session;
},
},
}
const serverSession = () => getServerSession(authOptions)
export { authOptions, serverSession }