Skip to content

Commit

Permalink
sigup route is up
Browse files Browse the repository at this point in the history
  • Loading branch information
Auro-jyoti committed Jan 23, 2024
1 parent 8d49782 commit 8d385ca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
JWT_SECRET: "your-jwt-secret"
JWT_SECRET: "goldie"
}
51 changes: 51 additions & 0 deletions backend/routes/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
const express = require("express");
const zod = require("zod");
const { User } = require("../db");
const { JWT_SECRET } = require("../config");
const jwt = require("jsonwebtoken");

const router = express.Router();

const signupBody = zod.object({
username: zod.string().email(),
firstName: zod.string(),
lastName: zod.string(),
password: zod.string(),
});

router.post("/signup", async (req, res) => {
const { success } = signupBody.safeParse(req.body);
if (!success) {
return res.status(411).json({
message: "Email already taken / Incorrect inputs",
});
}

const existingUser = await User.findOne({
username: req.body.username,
});

if (existingUser) {
return res.status(411).json({
message: "Email already taken/Incorrect inputs",
});
}

const user = await User.create({
username: req.body.username,
password: req.body.password,
firstName: req.body.firstName,
lastName: req.body.lastName,
});

const userId = user._id;

const token = jwt.sign(
{
userId,
},
JWT_SECRET
);

res.json({
message: "User Created Successfully",
token: token,
});
});

module.exports = router;

0 comments on commit 8d385ca

Please sign in to comment.