Skip to content

Commit

Permalink
adde identity provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan-Roberts123 committed May 3, 2024
1 parent 439ac0c commit 5b7562c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
16 changes: 11 additions & 5 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 17 additions & 3 deletions src/lib/authOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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");
Expand Down
13 changes: 8 additions & 5 deletions src/lib/services/user.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,7 +27,7 @@ export async function loginUserHandler({
password: string;
}): Promise<TUser> {
try {
const user = await getUser(email);
const user = await getUser(email, "email");
if (user && user.password === hashText(password)) {
return exclude(user, ["password"]);
} else {
Expand All @@ -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,
},
Expand Down
4 changes: 4 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ export const ZUser = z.object({
});

export type TUser = z.infer<typeof ZUser>;

export const ZIdentityProvider = z.enum(["email", "google"]);

export type TIdentityProvider = z.infer<typeof ZIdentityProvider>;

0 comments on commit 5b7562c

Please sign in to comment.