From bbf9507dd2461b2f1caaa6f044a3c39ba06ed1e4 Mon Sep 17 00:00:00 2001 From: ImAlpha Date: Wed, 6 Dec 2023 22:44:51 -0800 Subject: [PATCH] working towards next auth working on login --- Websites/jefes-nextjs/.env | 5 +++- .../app/api/auth/[...nextauth]/route.ts | 27 ++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Websites/jefes-nextjs/.env b/Websites/jefes-nextjs/.env index 2c5f396..44c3f7d 100644 --- a/Websites/jefes-nextjs/.env +++ b/Websites/jefes-nextjs/.env @@ -14,4 +14,7 @@ DB_PASSWORD="pscale_pw_AyxGMWCMpaZOsaeeXXem8mrxXPRNJEG0uPUrW2zo1f8" DB_NAME="dev_db" DB_URI="mysql://11lq0wcjbxyku4zmubji:pscale_pw_AyxGMWCMpaZOsaeeXXem8mrxXPRNJEG0uPUrW2zo1f8@aws.connect.psdb.cloud/dev_db?ssl={"rejectUnauthorized":true}" ADMIN_EMAIL="Kevin@test.com" -ADMIN_PWD="!Testingpwd123" \ No newline at end of file +ADMIN_PWD="!Testingpwd123" +NEXTAUTH_URL = "http://localhost:3000" +// Generate in Bash shell, with openssl rand -base64 32 +NEXTAUTH_SECRET = "uM3+Q6lVz3l9tqaMKPVWmnY1r5tftjgxmFEi0/Z0+PA=" \ No newline at end of file diff --git a/Websites/jefes-nextjs/app/api/auth/[...nextauth]/route.ts b/Websites/jefes-nextjs/app/api/auth/[...nextauth]/route.ts index da79e48..127fbd1 100644 --- a/Websites/jefes-nextjs/app/api/auth/[...nextauth]/route.ts +++ b/Websites/jefes-nextjs/app/api/auth/[...nextauth]/route.ts @@ -1,9 +1,14 @@ import NextAuth from "next-auth"; import CredentialsProvider from "next-auth/providers/credentials"; import { compare } from "bcrypt"; -import mysql from 'mysql2/promise'; +import { db, connection } from "@/db/db"; +import { users } from "@/db/schema"; +import { eq } from "drizzle-orm"; const handler = NextAuth({ + session: { + strategy: "jwt", + }, providers: [ CredentialsProvider({ // The name to display on the sign in form (e.g. "Sign in with...") @@ -17,9 +22,25 @@ const handler = NextAuth({ password: {}, }, async authorize(credentials, req) { + // Check if input email is in the database and return user + const response = await db + .select() + .from(users) + .where(eq(users.email, `${credentials?.email}`)); // compare hashed password with password entered - - console.log({ credentials }); + const passwordCheck = await compare( + `${credentials?.password}`, + response[0].password + ); + + console.log({ passwordCheck }); + + if (passwordCheck) { + return { + id: response[0].id, + email: response[0].email, + }; + } return null; }, }),