forked from 100xdevs-cohort-2/paytm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
43 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
}) |