-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
159 lines (122 loc) · 3.64 KB
/
app.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require('dotenv').config();
require('./config/db').connect();
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const User = require('./model/user');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const cookieParser = require('cookie-parser');
const auth = require('./middleware/auth');
app.use(express.json());
app.use(cookieParser());
app.get("/", (req,res)=>{
res.send("<h1>Home Route</h1>")
})
// register route
app.post('/register', async (req,res) => {
try {
// fetching data
const {firstName, lastName, email, password} = req.body;
// validating data
if(!(firstName && lastName && email && password)){
res.status(400).json({
"message":"insufficient data"
});
}
// checking if user already exists
const existUser = await User.findOne({email});
if(existUser){
res.status(401).json({
email,
"message":"already exist"
})
}
// encrypting the password
const salt = await bcrypt.genSalt(10);
const myEncrypPassword = await bcrypt.hash(password,salt);
// creating user in database
const user = await User.create({
firstName,
lastName,
email,
password:myEncrypPassword
});
// create a token and send it to user
const token = jwt.sign(
{
id:user._id,
email:user.email,
},
"shhhhh",
{ expiresIn: "2h" }
);
user.token = token;
// don't want to send the password
user.password = undefined;
res.status(201).json(user);
} catch (error) {
console.log(error);
}
});
// login route
app.post('/login', async(req,res) => {
try {
const {email, password} = req.body;
if(!(email && password)){
res.status('401').json({
"message":"insufficient data"
});
}
const user = await User.findOne({email});
if(!user){
res.status(401).json({"message":"does not exist"});
}
// matching the password
if(user && (await bcrypt.compare(password,user.password))){
// credentials are correct and thus logging in user
const token = jwt.sign({
id:user._id,
email
}, 'shhhhh',
{
expiresIn: "2h"
}
);
user.password = undefined;
user.token = token;
const options = {
expires: new Date(Date.now() + 3*24*60*60*1000 ),
httpOnly:true,
}
res.status(200).cookie("token", token, options).json({
success: true,
user
});
}
res.status(401).json({
"message":"invalid credentials"
});
} catch (error) {
console.log(error);
}
})
// dashboard
app.get('/dashboard', auth, async(req,res) => {
res.status(200).json({
"message":"Welcome to Dashboard",
"user": req.user
});
})
app.get('/profile', auth, async(req,res) => {
// extracting email of logged in user
const {email} = req.user;
// finding user in database
const user = await User.findOne({email});
user.password = undefined;
res.status(200).json({
"message":"User Profile",
"userDetails":user
})
})
module.exports = app;