-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
31 lines (27 loc) · 927 Bytes
/
auth.ts
File metadata and controls
31 lines (27 loc) · 927 Bytes
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
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [GitHub],
callbacks: {
// This controls where users can go
authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = !!auth?.user;
const isOnDashboard = nextUrl.pathname.startsWith('/dashboard');
const isOnLanding = nextUrl.pathname === '/';
if (isOnDashboard) {
if (isLoggedIn) return true;
return false; // Redirect unauthenticated users to login
}
if (isOnLanding) {
if (isLoggedIn) {
// If user is logged in and hits landing, bounce them to dashboard
return Response.redirect(new URL('/dashboard', nextUrl));
}
}
return true;
},
},
pages: {
signIn: '/', // If they need to sign in, send them to landing (where your modal is)
},
});