refactor: move auth check into pages
This commit is contained in:
parent
dd6b82515e
commit
ddcb14e564
3 changed files with 13 additions and 18 deletions
|
@ -1,3 +1,9 @@
|
||||||
export default function Home() {
|
import { auth } from '@/auth';
|
||||||
return <div></div>;
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
|
export default async function Home() {
|
||||||
|
const session = await auth();
|
||||||
|
|
||||||
|
if (!session?.user) redirect('/login');
|
||||||
|
else redirect('/home');
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,10 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
|
||||||
),
|
),
|
||||||
callbacks: {
|
callbacks: {
|
||||||
authorized: async ({ auth }) => {
|
authorized: async ({ auth }) => {
|
||||||
return !!auth;
|
return !!auth?.user;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
pages: {
|
||||||
|
signIn: '/login',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,18 +1,4 @@
|
||||||
import { auth } from '@/auth';
|
export { auth as middleware } from '@/auth';
|
||||||
|
|
||||||
export default auth((req) => {
|
|
||||||
if (
|
|
||||||
!req.auth &&
|
|
||||||
req.nextUrl.pathname !== '/login' &&
|
|
||||||
process.env.MEETUP_SKIP_LOGIN !== 'true'
|
|
||||||
) {
|
|
||||||
const newUrl = new URL('/login', req.nextUrl.origin);
|
|
||||||
return Response.redirect(newUrl);
|
|
||||||
} else if (req.auth != null && req.nextUrl.pathname === '/') {
|
|
||||||
const newUrl = new URL('/home', req.nextUrl.origin);
|
|
||||||
return Response.redirect(newUrl);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
|
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue