mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-30 12:55:02 +00:00
implemented user sign in against database
This commit is contained in:
parent
b3cdf6ac27
commit
d1e9d8949d
3 changed files with 36 additions and 9 deletions
|
|
@ -1,8 +1,13 @@
|
|||
import NextAuth, { NextAuthOptions } from "next-auth";
|
||||
import GoogleProvider from "next-auth/providers/google";
|
||||
import { db } from "@/app/db/db";
|
||||
import { user as userTable, User } from "@/app/db/schema/users";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
const { GOOGLE_CLIENT_ID = "", GOOGLE_CLIENT_SECRET = "" } = process.env;
|
||||
|
||||
type OauthProvider = "google";
|
||||
|
||||
// TODO: handle token expiration
|
||||
// https://next-auth.js.org/v3/tutorials/refresh-token-rotation
|
||||
// propertly set options too
|
||||
|
|
@ -26,14 +31,33 @@ export const AuthOptions: NextAuthOptions = {
|
|||
signIn: "/",
|
||||
},
|
||||
callbacks: {
|
||||
async signIn({ user, account, profile, email, credentials }) {
|
||||
// TODO: While we don't have a database with verified users,
|
||||
// for the demo, allow any user to sign in
|
||||
// if (user.email?.endsWith("@hestia.homes")) {
|
||||
// return true;
|
||||
// }
|
||||
// TODO: Handle this more elegantly
|
||||
// return "/beta";
|
||||
async signIn({ user, account }) {
|
||||
if (user == null || user.email == null) {
|
||||
return "/beta";
|
||||
}
|
||||
|
||||
const dbUser: User[] = await db
|
||||
.select()
|
||||
.from(userTable)
|
||||
.where(eq(userTable.email, String(user.email)));
|
||||
|
||||
if (dbUser.length !== 1 || account == null) {
|
||||
return "/beta";
|
||||
}
|
||||
|
||||
if (!dbUser[0].oauthId) {
|
||||
// We make a second query to populate the oauthId and oauthProvider
|
||||
console.log("Updating user with oauthId and oauthProvider");
|
||||
const provider = account.provider as OauthProvider;
|
||||
|
||||
await db
|
||||
.update(userTable)
|
||||
.set({ oauthId: user.id, oauthProvider: provider })
|
||||
.where(eq(userTable.email, String(user.email)));
|
||||
|
||||
console.log("Updated oauthId and oauthProvider");
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
async redirect({ baseUrl }) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
// db.ts
|
||||
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
|
||||
import { drizzle } from "drizzle-orm/node-postgres";
|
||||
import { Pool } from "pg";
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { serial, text, timestamp, pgTable } from "drizzle-orm/pg-core";
|
||||
import { InferModel } from "drizzle-orm";
|
||||
|
||||
export const user = pgTable("user", {
|
||||
id: serial("id").primaryKey(),
|
||||
|
|
@ -21,3 +22,6 @@ export const user = pgTable("user", {
|
|||
.defaultNow()
|
||||
.notNull(),
|
||||
});
|
||||
|
||||
export type User = InferModel<typeof user, "select">;
|
||||
export type NewUser = InferModel<typeof user, "insert">;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue