-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
70 lines (69 loc) · 1.81 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
65
66
67
68
69
70
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { z } from "zod";
export const { handlers, auth, signIn, signOut } = NextAuth({
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
credentials: {
email: {},
password: {},
},
async authorize(credentials, req) {
const parsedCredentials = z
.object({
email: z.string().email(),
password: z.string().min(6),
})
.safeParse(credentials);
if (parsedCredentials.success) {
const { email, password } = parsedCredentials.data;
// const user = await getUserFromDB(email, password);
const user = {
id: "1",
username: "christian",
email: "[email protected]",
};
if (user) {
return user;
}
}
return null;
},
}),
],
pages: {
signIn: "/sign-in",
},
callbacks: {
authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = !!auth?.user;
const isOnDashboard = nextUrl.pathname.startsWith("/dashboard");
if (isOnDashboard) {
if (isLoggedIn) return true;
return Response.redirect(new URL("/sign-in", nextUrl));
} else if (isLoggedIn) {
return Response.redirect(new URL("/dashboard", nextUrl));
}
return true;
},
async signIn({ user, account, profile, email, credentials }) {
console.log(`signIn callback`);
return true;
},
async jwt({ token, user }) {
if (user) {
token.user = user;
}
return token;
},
async session({ session, token }) {
const { user } = token;
// @ts-ignore
session.user = user;
return session;
},
},
});