-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
64 lines (61 loc) · 1.61 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { signInSchema } from "@/app/lib/zod";
import { authUser, getUser } from "@/app/lib/actions";
import { User } from "@/app/lib/types";
import { ZodError, string } from "zod";
export const { handlers, signIn, signOut, auth } = NextAuth({
session: {
strategy: "jwt",
},
pages: {
signIn: "/login",
},
providers: [
Credentials({
credentials: {
email: {},
password: {},
},
authorize: async (credentials) => {
try {
let user: User | null = null;
const { email, password } =
await signInSchema.parseAsync(credentials);
// logic to verify if user exists
user = await authUser(email, password);
// return user object with the their profile data
return user;
} catch (error) {
// console.log(error);
if (error instanceof ZodError) {
// Return `null` to indicate that the credentials are invalid
return null;
} else {
return null;
}
}
},
}),
],
callbacks: {
jwt({ token, user }) {
if (user && user.id) {
// User is available during sign-in
token.picture = user.id;
}
return token;
},
session({ session, token }) {
const id: string = token.picture ? token.picture : "";
return {
user: {
name: session.user.name,
email: session.user.email,
id: id,
},
expires: session.expires,
};
},
},
});