forked from gdg4dev/nager-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (88 loc) · 3.11 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const express=require('express');
const mongoose= require('mongoose');
const bodyparser=require('body-parser');
const cookieParser=require('cookie-parser');
const User=require('./models/user');
const {auth} =require('./middlewares/auth');
const db=require('./config/config').get(process.env.NODE_ENV);
const app=express();
// app use
app.use(bodyparser.urlencoded({extended : false}));
app.use(bodyparser.json());
app.use(cookieParser());
// database connection
mongoose.Promise=global.Promise;
mongoose.connect(db.DATABASE,{ useNewUrlParser: true,useUnifiedTopology:true },function(err){
if(err) console.log(err);
console.log("database is connected");
});
// adding new user (sign-up route)
app.post('/api/register',function(req,res){
res.send("hi")
// taking a user
// const newuser=new User(req.body);
// console.log(newuser);
// if(newuser.password!=newuser.password2)return res.status(400).json({message: "password not match"});
// User.findOne({email:newuser.email},function(err,user){
// if(user) return res.status(400).json({ auth : false, message :"email exits"});
// newuser.save((err,doc)=>{
// if(err) {console.log(err);
// return res.status(400).json({ success : false});}
// res.status(200).json({
// succes:true,
// user : doc
// });
// });
// });
});
// login user
app.post('/api/login', function(req,res){
let token=req.cookies.auth;
User.findByToken(token,(err,user)=>{
if(err) return res(err);
if(user) return res.status(400).json({
error :true,
message:"You are already logged in"
});
else{
User.findOne({'email':req.body.email},function(err,user){
if(!user) return res.json({isAuth : false, message : ' Auth failed ,email not found'});
user.comparepassword(req.body.password,(err,isMatch)=>{
if(!isMatch) return res.json({ isAuth : false,message : "password doesn't match"});
user.generateToken((err,user)=>{
if(err) return res.status(400).send(err);
res.cookie('auth',user.token).json({
isAuth : true,
id : user._id
,email : user.email
});
});
});
});
}
});
});
//logout user
app.get('/api/logout',auth,function(req,res){
req.user.deleteToken(req.token,(err,user)=>{
if(err) return res.status(400).send(err);
res.sendStatus(200);
});
});
// get logged in user
app.get('/api/profile',auth,function(req,res){
res.json({
isAuth: true,
id: req.user._id,
email: req.user.email,
name: req.user.firstname + req.user.lastname
})
});
app.get('/',function(req,res){
res.status(200).send(`Welcome to login , sign-up api`);
});
// listening port
const PORT=process.env.PORT||5500;
app.listen(PORT,()=>{
console.log(`app is live at ${PORT}`);
});