working auth + db
This commit is contained in:
parent
a625b077cc
commit
5b93901976
6 changed files with 201 additions and 22 deletions
|
|
@ -9,11 +9,11 @@ datasource db {
|
|||
}
|
||||
|
||||
model users {
|
||||
UserId Int @id @default(autoincrement())
|
||||
Name String?
|
||||
Email String?
|
||||
Password String?
|
||||
Role String?
|
||||
GoogleId String?
|
||||
LoginType String?
|
||||
userid Int @id @default(autoincrement())
|
||||
email String @unique
|
||||
name String?
|
||||
password String?
|
||||
role String?
|
||||
googleid String?
|
||||
logintype String?
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,53 @@
|
|||
const express = require("express");
|
||||
const instructorRouter = express.Router();
|
||||
const { PrismaClient } = require("@prisma/client");
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// For new users sign-up via Google oAuth:
|
||||
instructorRouter.post('/register-user', async (req, res) => {
|
||||
try {
|
||||
const { username, password } = req.body;
|
||||
const newUser = await prisma.user.create({
|
||||
data: { username, email, password },
|
||||
instructorRouter.post("/register-user", async (req, res) => {
|
||||
try {
|
||||
console.log("Received request to register user");
|
||||
|
||||
const { id, displayName, emails } = req.body.user;
|
||||
console.log("User details from request:", { id, displayName, emails });
|
||||
|
||||
const email = emails[0].value;
|
||||
console.log("Extracted email:", email);
|
||||
|
||||
// Check if user exists
|
||||
const user = await prisma.users.findFirst({
|
||||
where: {
|
||||
email: {
|
||||
equals: email,
|
||||
mode: "insensitive",
|
||||
},
|
||||
},
|
||||
});
|
||||
console.log("User lookup result:", user);
|
||||
|
||||
// if it is a new user, insert it into the DB
|
||||
if (!user) {
|
||||
console.log("User does not exist, creating new user");
|
||||
const newUser = await prisma.users.create({
|
||||
data: {
|
||||
name: displayName,
|
||||
email: email,
|
||||
role: "instructor",
|
||||
googleid: id,
|
||||
logintype: "google",
|
||||
},
|
||||
});
|
||||
res.json({ message: 'User added successfully', user: newUser });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
console.log("New user created:", newUser);
|
||||
|
||||
res.json({ message: "User added successfully", user: newUser });
|
||||
} else {
|
||||
console.log("User already exists:", user);
|
||||
res.json({ message: "User exist", user: user });
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Error during user registration:", err.message);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = instructorRouter;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue