From 456e76f4cff89979586fcd13216a89a5b6cf8e68 Mon Sep 17 00:00:00 2001 From: StephDietz Date: Thu, 21 Sep 2023 15:45:35 -0500 Subject: [PATCH] debug --- .../app/api/auth/[...nextauth]/route.ts | 53 ++----------------- dashboard/15-final/auth.ts | 51 ++++++++++++++++++ 2 files changed, 54 insertions(+), 50 deletions(-) create mode 100644 dashboard/15-final/auth.ts diff --git a/dashboard/15-final/app/api/auth/[...nextauth]/route.ts b/dashboard/15-final/app/api/auth/[...nextauth]/route.ts index 5be3020..bafa544 100644 --- a/dashboard/15-final/app/api/auth/[...nextauth]/route.ts +++ b/dashboard/15-final/app/api/auth/[...nextauth]/route.ts @@ -1,53 +1,6 @@ -import NextAuth, { NextAuthOptions } from 'next-auth'; -import CredentialsProvider from 'next-auth/providers/credentials'; -import bcrypt from 'bcrypt'; -import { fetchUser } from '../../../lib/data'; -import { User } from '@/app/lib/definitions'; - -const authOptions: NextAuthOptions = { - providers: [ - CredentialsProvider({ - name: 'credentials', - credentials: {}, - - // TODO: Not sure how to type authorize function correctly - // @ts-ignore - async authorize(credentials: { - email: string; - password: string; - }): Promise { - const { email, password } = credentials; - - try { - const user = await fetchUser(email); - - if (!user) { - return null; - } - - const passwordsMatch = await bcrypt.compare(password, user.password); - - if (!passwordsMatch) { - console.log('Passwords do not match'); - return null; - } - - return user; - } catch (error) { - console.log('Error: ', error); - } - }, - }), - ], - session: { - strategy: 'jwt', - }, - secret: process.env.NEXTAUTH_SECRET, - pages: { - signIn: '/login', - }, -}; +import { authOptions } from '@/auth'; +import NextAuth from 'next-auth'; const handler = NextAuth(authOptions); -export { handler as GET, handler as POST, authOptions }; +export { handler as GET, handler as POST }; diff --git a/dashboard/15-final/auth.ts b/dashboard/15-final/auth.ts new file mode 100644 index 0000000..4a58a5e --- /dev/null +++ b/dashboard/15-final/auth.ts @@ -0,0 +1,51 @@ +import { NextAuthOptions } from 'next-auth'; +import CredentialsProvider from 'next-auth/providers/credentials'; +import bcrypt from 'bcrypt'; +import { User } from '@/app/lib/definitions'; +import { sql } from '@vercel/postgres'; + +async function getUser(email: string) { + try { + const user = await sql`SELECT * from USERS where email=${email}`; + console.log('USER', user); + return user.rows[0] as User; + } catch (error) { + console.error('Failed to fetch user:', error); + throw new Error('Failed to fetch user.'); + } +} + +export const authOptions: NextAuthOptions = { + providers: [ + CredentialsProvider({ + name: 'credentials', + credentials: { + password: { label: 'Password', type: 'password' }, + email: { label: 'Email', type: 'email' }, + }, + // @ts-ignore + async authorize(credentials) { + console.log('debugging credentials', credentials); + const { email, password } = credentials ?? {}; + const user = await getUser(email as string); + + if (!user || !password) { + console.log('Missing credentials'); + return null; + } + + const passwordsMatch = await bcrypt.compare(password, user.password); + + if (!passwordsMatch) { + console.log('Invalid credentials'); + return null; + } + + return user; + }, + }), + ], + pages: { + signIn: '/login', + }, +};