Skip to content

Commit

Permalink
signup api done
Browse files Browse the repository at this point in the history
  • Loading branch information
mridul891 committed Aug 7, 2024
1 parent 4265238 commit 4c2f614
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
38 changes: 38 additions & 0 deletions backend/Routes/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
const express = require("express")
const router = express.Router()
const zod = require('zod')
const User = require("../db")
const JWT_SECRET = require("../config")
const jwt = require("jsonwebtoken")
const signupSchema = zod.object({
username: zod.string(),
password: zod.string(),
firstname: zod.string(),
lastname: zod.string()
})
router.post('/signup', async (req, res) => {
const body = req.body;
// Checks for the inputs using zod
const { success } = signupSchema.safeParse(req.body)
if (!success) {
return res.status(411).json({ message: "Email already taken / InCorrect input" })
}

// Finding Existing Users
const existingUser = await User.findOne({
username: body.username
})
if (existingUser._id) {
return res.status(411).json({ message: "User Already Exists" })
}

// New User

const dbUser = await User.create(body)
const Token = jwt.sign({ userId: dbUser._id }, JWT_SECRET)
res.json({
message: "User Created Successfully",
token: Token
})
})

router.post('/signup', (req, res) => {

})
module.exports = router
2 changes: 1 addition & 1 deletion backend/config.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const JWT_SECRET = "Mridul"
module.exports = { JWT_SECRET }
module.exports = JWT_SECRET
2 changes: 1 addition & 1 deletion backend/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ const userSchema = mongoose.Schema({
})

const User = mongoose.model("User", userSchema)
module.exports = { User }
module.exports = User
5 changes: 3 additions & 2 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const express = require("express");
const mainRouter = require("./routes/index")
const cors = require("cors")

const PORT = 3000
app.use(cors())
app.use(express.json())


app.use("/api/v1", mainRouter)
app.listen(port, () => {

app.listen(PORT, () => {
console.log(`the app is running at localhost :${port}`)
})

0 comments on commit 4c2f614

Please sign in to comment.