updated schema for next auth

This commit is contained in:
ImAlpha 2023-12-07 22:38:51 -08:00
parent bbf9507dd2
commit 91c79ed6b9
9 changed files with 108 additions and 26 deletions

View File

@ -1,11 +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 { DrizzleAdapter } from "@auth/drizzle-adapter";
import { db, connection } from "@/db/db"; import { db, connection } from "@/db/db";
import { users } from "@/db/schema"; import { users } from "@/db/schema";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import isValidCredentials from "zod";
const handler = NextAuth({ const handler = NextAuth({
adapter: DrizzleAdapter(db),
session: { session: {
strategy: "jwt", strategy: "jwt",
}, },
@ -27,19 +30,20 @@ const handler = NextAuth({
.select() .select()
.from(users) .from(users)
.where(eq(users.email, `${credentials?.email}`)); .where(eq(users.email, `${credentials?.email}`));
if (!response) {
return null;
}
// compare hashed password with password entered // compare hashed password with password entered
const passwordCheck = await compare( const passwordCheck = await compare(
`${credentials?.password}`, `${credentials?.password}` || "",
response[0].password response[0].password
); );
console.log({ passwordCheck }); console.log({ passwordCheck });
if (passwordCheck) { if (passwordCheck) {
return { return users;
id: response[0].id,
email: response[0].email,
};
} }
return null; return null;
}, },

View File

@ -3,7 +3,7 @@ import { migrate } from 'drizzle-orm/mysql2/migrator';
import { db, connection } from './db'; import { db, connection } from './db';
// This will run migrations on the database, skipping the ones already applied // This will run migrations on the database, skipping the ones already applied
await migrate(db, { migrationsFolder: './drizzle/migrations' }); const push = await migrate(db, { migrationsFolder: './drizzle/migrations' });
// Don't forget to close the connection, otherwise the script will hang // Don't forget to close the connection, otherwise the script will hang
await connection.end(); const con = await connection.end();

View File

@ -1,26 +1,90 @@
// db/schema.ts // db/schema.ts
// pnpm drizzle-kit generate:mysql // pnpm drizzle-kit generate:mysql
// pnpm tsx db/migrate.ts // pnpm tsx db/migrate.ts
import { // import {
index, // index,
int, // int,
mysqlTable, // mysqlTable,
bigint, // bigint,
varchar, // varchar,
timestamp, // timestamp,
mysqlSchema, // mysqlSchema,
} from "drizzle-orm/mysql-core"; // } from "drizzle-orm/mysql-core";
// export const mySchema = mysqlSchema("drizzle_jefes") for creating a new db // export const mySchema = mysqlSchema("drizzle_jefes") for creating a new db
export const users = mysqlTable("users", { // export const users = mysqlTable("users", {
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(), // id: int("id").primaryKey().autoincrement(),
email: varchar("email", { length: 256 }).notNull(), // email: varchar("email", { length: 256 }).notNull(),
password: varchar("password", { length: 256 }).notNull(), // password: varchar("password", { length: 256 }).notNull(),
}); // });
// export const menuItems = mysqlTable("menu_item", { // export const menuItems = mysqlTable("menu_item", {
// id: bigint("id", { mode: "number" }).primaryKey().autoincrement(), // id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
// itemName: varchar("item_name", { length: 256 }), // itemName: varchar("item_name", { length: 256 }),
// itemDescription: varchar("item_description", { length: 256 }), // itemDescription: varchar("item_description", { length: 256 }),
// }); // });
import {
int,
timestamp,
mysqlTable,
primaryKey,
varchar,
} from "drizzle-orm/mysql-core";
import type { AdapterAccount } from "@auth/core/adapters";
export const users = mysqlTable("user", {
id: varchar("id", { length: 255 }).notNull().primaryKey(),
name: varchar("name", { length: 255 }),
email: varchar("email", { length: 255 }).notNull(),
emailVerified: timestamp("emailVerified", {
mode: "date",
fsp: 3,
}).defaultNow(),
image: varchar("image", { length: 255 }),
});
export const accounts = mysqlTable(
"account",
{
userId: varchar("userId", { length: 255 })
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
type: varchar("type", { length: 255 })
.$type<AdapterAccount["type"]>()
.notNull(),
provider: varchar("provider", { length: 255 }).notNull(),
providerAccountId: varchar("providerAccountId", { length: 255 }).notNull(),
refresh_token: varchar("refresh_token", { length: 255 }),
access_token: varchar("access_token", { length: 255 }),
expires_at: int("expires_at"),
token_type: varchar("token_type", { length: 255 }),
scope: varchar("scope", { length: 255 }),
id_token: varchar("id_token", { length: 2048 }),
session_state: varchar("session_state", { length: 255 }),
},
(account) => ({
compoundKey: primaryKey(account.provider, account.providerAccountId),
})
);
export const sessions = mysqlTable("session", {
sessionToken: varchar("sessionToken", { length: 255 }).notNull().primaryKey(),
userId: varchar("userId", { length: 255 })
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
expires: timestamp("expires", { mode: "date" }).notNull(),
});
export const verificationTokens = mysqlTable(
"verificationToken",
{
identifier: varchar("identifier", { length: 255 }).notNull(),
token: varchar("token", { length: 255 }).notNull(),
expires: timestamp("expires", { mode: "date" }).notNull(),
},
(vt) => ({
compoundKey: primaryKey(vt.identifier, vt.token),
})
);

View File

@ -2,6 +2,10 @@
import type { Config } from "drizzle-kit"; import type { Config } from "drizzle-kit";
import "dotenv/config"; import "dotenv/config";
if (!process.env.DB_HOST) {
throw new Error('DATABASE_URL is missing');
}
export default { export default {
schema: "./db/schema.ts", schema: "./db/schema.ts",
out: "./drizzle/migrations", out: "./drizzle/migrations",

View File

@ -1,5 +1,5 @@
CREATE TABLE `users` ( CREATE TABLE `users` (
`id` bigint AUTO_INCREMENT NOT NULL, `id` int AUTO_INCREMENT NOT NULL,
`email` varchar(256) NOT NULL, `email` varchar(256) NOT NULL,
`password` varchar(256) NOT NULL, `password` varchar(256) NOT NULL,
CONSTRAINT `users_id` PRIMARY KEY(`id`) CONSTRAINT `users_id` PRIMARY KEY(`id`)

View File

@ -1,7 +1,7 @@
{ {
"version": "5", "version": "5",
"dialect": "mysql", "dialect": "mysql",
"id": "27fe79c5-67fc-42d1-be66-66202ed1e005", "id": "9e600bcf-c5c3-4ba9-9a1f-0d981d718812",
"prevId": "00000000-0000-0000-0000-000000000000", "prevId": "00000000-0000-0000-0000-000000000000",
"tables": { "tables": {
"users": { "users": {
@ -9,7 +9,7 @@
"columns": { "columns": {
"id": { "id": {
"name": "id", "name": "id",
"type": "bigint", "type": "int",
"primaryKey": false, "primaryKey": false,
"notNull": true, "notNull": true,
"autoincrement": true "autoincrement": true

View File

@ -5,8 +5,8 @@
{ {
"idx": 0, "idx": 0,
"version": "5", "version": "5",
"when": 1701503074409, "when": 1702012909346,
"tag": "0000_friendly_lady_deathstrike", "tag": "0000_charming_silhouette",
"breakpoints": true "breakpoints": true
} }
] ]

View File

@ -8,6 +8,7 @@
"name": "jefes-nextjs", "name": "jefes-nextjs",
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"@auth/drizzle-adapter": "^0.3.9",
"@planetscale/database": "^1.11.0", "@planetscale/database": "^1.11.0",
"@radix-ui/react-slot": "^1.0.2", "@radix-ui/react-slot": "^1.0.2",
"@react-google-maps/api": "^2.19.2", "@react-google-maps/api": "^2.19.2",
@ -94,6 +95,14 @@
} }
} }
}, },
"node_modules/@auth/drizzle-adapter": {
"version": "0.3.9",
"resolved": "https://registry.npmjs.org/@auth/drizzle-adapter/-/drizzle-adapter-0.3.9.tgz",
"integrity": "sha512-xDHYofXyxIvqkLqsrxnnRWpNLJVpvPw+2qlFuZxkMYs83+geYHQqMg0GBrB/pAoNx1+1hG1DjGQvCeSr52FPKg==",
"dependencies": {
"@auth/core": "0.18.4"
}
},
"node_modules/@babel/runtime": { "node_modules/@babel/runtime": {
"version": "7.23.2", "version": "7.23.2",
"resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.23.2.tgz", "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.23.2.tgz",

View File

@ -9,6 +9,7 @@
"lint": "next lint" "lint": "next lint"
}, },
"dependencies": { "dependencies": {
"@auth/drizzle-adapter": "^0.3.9",
"@planetscale/database": "^1.11.0", "@planetscale/database": "^1.11.0",
"@radix-ui/react-slot": "^1.0.2", "@radix-ui/react-slot": "^1.0.2",
"@react-google-maps/api": "^2.19.2", "@react-google-maps/api": "^2.19.2",