Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 52 additions & 20 deletions apps/http-server/src/routes/UserRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express, { Router } from "express";
import jwt from "jsonwebtoken";
import bcrypt from "bcryptjs";
import { userSchema } from "../types/zod";
import{prisma} from "@repo/primary-db/prisma"
import { prisma } from "@repo/primary-db/prisma";

const router: Router = express.Router();
const JWT_SECRET = process.env.JWT_SECRET || "secret";
Expand All @@ -15,26 +15,28 @@ router.post("/signup", async (req, res) => {
if (!parsed.success) {
return res.status(403).json({ message: "Invalid input data" });
}

const existingUser = await prisma.user.findUnique({
where:{
email:parsed.data.email
}
})
where: {
email: parsed.data.email,
},
});
if (existingUser) {
return res.status(400).json({ message: "User already exists, please login" });
return res
.status(400)
.json({ message: "User already exists, please login" });
}

const hashedPassword = await bcrypt.hash(parsed.data.password, 10);

const newUser=prisma.user.create({
data:{
email:parsed.data.email,
password:hashedPassword
}
})
const newUser = prisma.user.create({
data: {
email: parsed.data.email,
password: hashedPassword,
},
});

res.status(200).json({ userId:(await newUser).id });
res.status(200).json({ userId: (await newUser).id });
} catch (error) {
console.log("error signing up", error);
res.status(403).json({ message: "Error while signing up" });
Expand All @@ -51,10 +53,10 @@ router.post("/signin", async (req, res) => {
}

const user = await prisma.user.findUnique({
where:{
email:parsed.data.email
}
})
where: {
email: parsed.data.email,
},
});
if (!user) {
return res.status(403).json({ message: "Invalid credentials" });
}
Expand All @@ -64,12 +66,42 @@ router.post("/signin", async (req, res) => {
return res.status(403).json({ message: "Invalid credentials" });
}

const token = jwt.sign({ id: user.id, email: user.email }, JWT_SECRET, { expiresIn: "7d" });
const token = jwt.sign(
{ id: user.id, email: user.email },
JWT_SECRET,
{ expiresIn: "7d" }
);
res.status(200).json({ token });
} catch (error) {
console.error("error signing in", error);
res.status(403).json({ message: "Incorrect credentials" });
}
});

export default router;
// Get User Balance
router.get("/balance", async (req, res) => {
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).json({ message: "No token provided" });
}

const token = authHeader.split(" ")[1];
try {
const payload = jwt.verify(token, JWT_SECRET) as { id: string };
const user = await prisma.user.findUnique({
where: { id: payload.id },
});

if (!user) {
return res.status(404).json({ message: "User not found" });
}

// Assuming the user model has a `balance` field of type number
res.status(200).json({ balance: user.balance ?? 0 });
} catch (err) {
console.error("Invalid token:", err);
res.status(401).json({ message: "Invalid token" });
}
});

export default router;