-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.ts
46 lines (45 loc) · 1.27 KB
/
auth.ts
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
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
import { insertUser } from "./lib/actions/auth.actions";
import { User } from "./lib/models/user.model";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
providers: [
Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
authorization:
"https://accounts.google.com/o/oauth2/auth?response_type=code&hd=nitkkr.ac.in",
}),
],
callbacks: {
async session({ session }) {
if (session.user) {
const user = await User.findOne({ Email: session.user?.email });
session.user.id = user._id;
session.user.image = user.Avatar;
session.user.email = user.Email;
}
return session;
},
async signIn({ user, profile }) {
const isAllowedToSignIn =
user.email && user.email.endsWith("@nitkkr.ac.in");
if (isAllowedToSignIn && profile) {
await insertUser({
Username: profile.email!.substring(0, profile.email!.indexOf("@")),
Email: profile.email!,
Avatar: profile.picture!,
Name: profile.name!,
});
return true;
} else {
return false;
}
},
},
});