-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashed.js
71 lines (59 loc) · 1.57 KB
/
hashed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const bcrypt = require('bcrypt')
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const { UserModel, TodoModel } = require("./db");
const jwt = require("jsonwebtoken");
const JWT_SECERT = "JOKER";
const z = require('zod')
mongoose.connect("")
app.use(express.json());
app.post('/signup', async function(req,res){
const requireBody = z.object({
email:z.string().min(3).max(13).email(),
password:z.string().min(3).max(10),
name:z.string().min(3).max(10)
})
const parsedData = requireBody.safeParse(req.body)
if(!parsedData.success){
res.json({
message:"Incorrect Format",
error:parsedData.error
})
return
}
const {email,password,name} = req.body
const HashedPassword = await bcrypt.hash(password,5);
console.log(HashedPassword);
await UserModel.create({
email:email,
password:HashedPassword,
name:name
})
res.json({
message:"User signup Succesfull"
})
})
app.post('/signin',async function(req,res){
const {email,password} = req.body;
const response = await UserModel.findOne({
email:email
})
const passwordHashed = await bcrypt.compare(password,response.password)
if(!email){
res.status(403).json({msg:"Email Not Found"})
}
if(passwordHashed){
const token = jwt.sign({
id:response._id.toString()
},JWT_SECERT)
res.json({
token:token
})
}else{
res.json({
message:"Token not Found"
})
}
})
app.listen(3000);