-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
63 lines (59 loc) · 1.99 KB
/
server.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
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const User = require("./model/user.model");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const JWT_SECRET = "suhaibgour";
mongoose.connect('mongodb+srv://FastEasyShare:[email protected]/inshare?retryWrites=true&w=majority');
const app = express();
app.use("/", express.static(path.join(__dirname, 'static')));
app.use(bodyParser.json());
//api
app.post('/api/register', async(req,res)=>{
const {name, email, mobile, password:plainTextPassword}= req.body;
if(!name || typeof name !== 'string'){
return res.json({status:"Invalid name"});
}
const password = await bcrypt.hash(plainTextPassword, 8);
try{
const response = await User.create({
name,
email,
mobile,
password,
});
console.log("user created successfully", response);
}
catch(err){
if(err.code === 11000){
return res.json({status:"error", error:"Username is already in use"});
}
throw err;
}
res.json({status:'ok'});
});
app.post('/api/login', async (req,res)=>{
const {email, password} = req.body;
const user = await User.findOne({email}).lean().exec();
console.log(user);
if(!user){
return res.json({status:"error", error:"User not found"});
}
if(await bcrypt.compare(password, user.password)){
const token = jwt.sign({
id:user._id,
email:user.email,
},JWT_SECRET)
return res.json({status:"ok", data:token});
}
res.json({status:'not ok', error:"Invalid email/password"});
});
app.listen(3000, () => {
try {
console.log("Listening on port 3000");
} catch (err) {
console.log("error",err);
}
});