-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (89 loc) · 3.21 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
109
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('./middleware/auth');
// definitions
const app=express();
app.use(bodyparser.urlencoded({extended : false}));
app.use(bodyparser.json());
app.use(cookieParser());
const db=require('./configuration/config').get(process.env.NODE_ENV);
// 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");
});
//get function
app.get('/',function(req,res)
{
res.status(200).send(`Welcome to login , sign-up api`);
});
// adding new user post function
app.post('/api/register',function(req,res){
// taking a user
const newuser=new User(req.body);
if(newuser.password!=newuser.password2)return res.status(400).json({message: "passwords do not match"});
User.findOne({email:newuser.email},function(err,user){
if(user) return res.status(400).json({ auth : false, message :"email already exists"});
// registering new user
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 : 'Authorisation failed, email not found'});
user.comparepassword(req.body.password,(err,isMatch)=>{
if(!isMatch) return res.json({ isAuth : false,message : "passwords don'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
});
});
});
});
}
});
});
// 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
})
});
//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);
});
});
// listening port
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.warn(`app listening on http://localhost:${PORT}`);
});