From 5b7562c732f21a213c9928ad092e761baf0f99f7 Mon Sep 17 00:00:00 2001 From: Pieter Stols Date: Fri, 3 May 2024 22:43:26 +0200 Subject: [PATCH] adde identity provider --- prisma/schema.prisma | 16 +++++++++++----- src/lib/authOptions.ts | 20 +++++++++++++++++--- src/lib/services/user.ts | 13 ++++++++----- src/lib/types.ts | 4 ++++ 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 5a3ad2f..3890fd6 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -14,12 +14,18 @@ datasource db { url = env("DATABASE_URL") } +enum IndentityProvider { + email + google +} + model User { - id String @id @default(uuid()) @map("_id") - username String - email String @unique - password String? - clients Client[] + id String @id @default(uuid()) @map("_id") + username String + email String @unique + password String? + clients Client[] + identityProvider IndentityProvider @default(email) } model Client { diff --git a/src/lib/authOptions.ts b/src/lib/authOptions.ts index b246568..c36665e 100644 --- a/src/lib/authOptions.ts +++ b/src/lib/authOptions.ts @@ -50,7 +50,11 @@ const OPTIONS: NextAuthOptions = { const { email, sub, name: username } = profile; const existingUser = await prisma.user.findUnique({ - where: { email: email, username: username }, + where: { + email: email, + username: username, + identityProvider: "google", + }, select: { id: true, }, @@ -59,11 +63,21 @@ const OPTIONS: NextAuthOptions = { if (!existingUser) { if (email && username) { await prisma.user.create({ - data: { id: sub, email, username }, + data: { + id: sub, + email, + username, + identityProvider: "google", + }, }); } else if (email) { await prisma.user.create({ - data: { id: sub, email, username: "unknown" }, + data: { + id: sub, + email, + username: "unknown", + identityProvider: "google", + }, }); } else { throw new Error("No email was found"); diff --git a/src/lib/services/user.ts b/src/lib/services/user.ts index 5726fb6..f8266f2 100644 --- a/src/lib/services/user.ts +++ b/src/lib/services/user.ts @@ -1,10 +1,13 @@ import prisma from "../prisma"; import { exclude, hashText } from "../utils"; -import { TUser } from "../types"; +import { TIdentityProvider, TUser } from "../types"; -export async function getUser(email: string) { +export async function getUser( + email: string, + identityProvider: TIdentityProvider +) { const user = await prisma.user.findUnique({ - where: { email: email }, + where: { email, identityProvider }, select: { id: true, username: true, @@ -24,7 +27,7 @@ export async function loginUserHandler({ password: string; }): Promise { try { - const user = await getUser(email); + const user = await getUser(email, "email"); if (user && user.password === hashText(password)) { return exclude(user, ["password"]); } else { @@ -38,7 +41,7 @@ export async function loginUserHandler({ export async function createUserHandler(data: any) { try { const existingUser = await prisma.user.findUnique({ - where: { email: data.email }, + where: { email: data.email, identityProvider: "email" }, select: { id: true, }, diff --git a/src/lib/types.ts b/src/lib/types.ts index 87ac47e..79b8af6 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -65,3 +65,7 @@ export const ZUser = z.object({ }); export type TUser = z.infer; + +export const ZIdentityProvider = z.enum(["email", "google"]); + +export type TIdentityProvider = z.infer;