From ff22cbc024b2c522f5c080937d88ca80123ad618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Sat, 14 Oct 2023 00:28:44 +0200 Subject: [PATCH] second alternative --- dashboard/15-final/auth.config.ts | 11 +++++++++++ dashboard/15-final/auth.ts | 11 +++-------- dashboard/15-final/middleware.ts | 4 +++- 3 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 dashboard/15-final/auth.config.ts diff --git a/dashboard/15-final/auth.config.ts b/dashboard/15-final/auth.config.ts new file mode 100644 index 0000000..4ec8683 --- /dev/null +++ b/dashboard/15-final/auth.config.ts @@ -0,0 +1,11 @@ +import type { NextAuthConfig } from 'next-auth'; + +export default { + providers: [], + callbacks: { + authorized({ auth, request: { nextUrl } }) { + return !nextUrl.pathname.startsWith('/dashboard') || !!auth?.user; + }, + }, + pages: { signIn: '/login' }, +} satisfies NextAuthConfig; diff --git a/dashboard/15-final/auth.ts b/dashboard/15-final/auth.ts index 88aac2c..83b7a2f 100644 --- a/dashboard/15-final/auth.ts +++ b/dashboard/15-final/auth.ts @@ -3,6 +3,7 @@ import Credentials from 'next-auth/providers/credentials'; import bcrypt from 'bcrypt'; import { sql } from '@vercel/postgres'; import type { User } from '@/app/lib/definitions'; +import authConfig from './auth.config'; async function getUser(email: string) { try { @@ -20,7 +21,9 @@ export const { signIn, signOut, } = NextAuth({ + ...authConfig, providers: [ + ...authConfig.providers, Credentials({ name: 'Sign-In with Credentials', credentials: { @@ -48,12 +51,4 @@ export const { }, }), ], - callbacks: { - authorized({ auth, request: { nextUrl } }) { - return !nextUrl.pathname.startsWith('/dashboard') || !!auth?.user; - }, - }, - pages: { - signIn: '/login', - }, }); diff --git a/dashboard/15-final/middleware.ts b/dashboard/15-final/middleware.ts index db54448..e299fcf 100644 --- a/dashboard/15-final/middleware.ts +++ b/dashboard/15-final/middleware.ts @@ -1 +1,3 @@ -export { auth as middleware } from './auth'; +import authConfig from './auth.config'; +import NextAuth from 'next-auth'; +export default NextAuth(authConfig).auth;