working towards next auth working on login

This commit is contained in:
ImAlpha 2023-12-06 22:44:51 -08:00
parent 045e5d56a6
commit bbf9507dd2
2 changed files with 28 additions and 4 deletions

View File

@ -14,4 +14,7 @@ DB_PASSWORD="pscale_pw_AyxGMWCMpaZOsaeeXXem8mrxXPRNJEG0uPUrW2zo1f8"
DB_NAME="dev_db" DB_NAME="dev_db"
DB_URI="mysql://11lq0wcjbxyku4zmubji:pscale_pw_AyxGMWCMpaZOsaeeXXem8mrxXPRNJEG0uPUrW2zo1f8@aws.connect.psdb.cloud/dev_db?ssl={"rejectUnauthorized":true}" DB_URI="mysql://11lq0wcjbxyku4zmubji:pscale_pw_AyxGMWCMpaZOsaeeXXem8mrxXPRNJEG0uPUrW2zo1f8@aws.connect.psdb.cloud/dev_db?ssl={"rejectUnauthorized":true}"
ADMIN_EMAIL="Kevin@test.com" ADMIN_EMAIL="Kevin@test.com"
ADMIN_PWD="!Testingpwd123" ADMIN_PWD="!Testingpwd123"
NEXTAUTH_URL = "http://localhost:3000"
// Generate in Bash shell, with openssl rand -base64 32
NEXTAUTH_SECRET = "uM3+Q6lVz3l9tqaMKPVWmnY1r5tftjgxmFEi0/Z0+PA="

View File

@ -1,9 +1,14 @@
import NextAuth from "next-auth"; import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials"; import CredentialsProvider from "next-auth/providers/credentials";
import { compare } from "bcrypt"; 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({ const handler = NextAuth({
session: {
strategy: "jwt",
},
providers: [ providers: [
CredentialsProvider({ CredentialsProvider({
// The name to display on the sign in form (e.g. "Sign in with...") // The name to display on the sign in form (e.g. "Sign in with...")
@ -17,9 +22,25 @@ const handler = NextAuth({
password: {}, password: {},
}, },
async authorize(credentials, req) { 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 // compare hashed password with password entered
const passwordCheck = await compare(
console.log({ credentials }); `${credentials?.password}`,
response[0].password
);
console.log({ passwordCheck });
if (passwordCheck) {
return {
id: response[0].id,
email: response[0].email,
};
}
return null; return null;
}, },
}), }),