Skip to content

Commit 11de1fc

Browse files
committed
Add Project files
1 parent 8326a85 commit 11de1fc

File tree

9 files changed

+460
-0
lines changed

9 files changed

+460
-0
lines changed

node-login/app.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const express = require('express');
4+
const app = express();
5+
const bodyParser = require('body-parser');
6+
const logger = require('morgan');
7+
const router = express.Router();
8+
const port = process.env.PORT || 8080;
9+
10+
app.use(bodyParser.json());
11+
app.use(logger('dev'));
12+
13+
require('./routes')(router);
14+
app.use('/api/v1', router);
15+
16+
app.listen(port);
17+
18+
console.log(`App Runs on ${port}`);

node-login/config/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name" : "Your Name",
3+
"email" : "[email protected]",
4+
"password" : "Your Password",
5+
"secret" : "Learn2Crack"
6+
}

node-login/functions/login.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const user = require('../models/user');
4+
const bcrypt = require('bcryptjs');
5+
6+
exports.loginUser = (email, password) =>
7+
8+
new Promise((resolve,reject) => {
9+
10+
user.find({email: email})
11+
12+
.then(users => {
13+
14+
if (users.length == 0) {
15+
16+
reject({ status: 404, message: 'User Not Found !' });
17+
18+
} else {
19+
20+
return users[0];
21+
22+
}
23+
})
24+
25+
.then(user => {
26+
27+
const hashed_password = user.hashed_password;
28+
29+
if (bcrypt.compareSync(password, hashed_password)) {
30+
31+
resolve({ status: 200, message: email });
32+
33+
} else {
34+
35+
reject({ status: 401, message: 'Invalid Credentials !' });
36+
}
37+
})
38+
39+
.catch(err => reject({ status: 500, message: 'Internal Server Error !' }));
40+
41+
});
42+
43+

node-login/functions/password.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
'use strict';
2+
3+
const user = require('../models/user');
4+
const bcrypt = require('bcryptjs');
5+
const nodemailer = require('nodemailer');
6+
const randomstring = require("randomstring");
7+
const config = require('../config/config.json');
8+
9+
exports.changePassword = (email, password, newPassword) =>
10+
11+
new Promise((resolve, reject) => {
12+
13+
user.find({ email: email })
14+
15+
.then(users => {
16+
17+
let user = users[0];
18+
const hashed_password = user.hashed_password;
19+
20+
if (bcrypt.compareSync(password, hashed_password)) {
21+
22+
const salt = bcrypt.genSaltSync(10);
23+
const hash = bcrypt.hashSync(newPassword, salt);
24+
25+
user.hashed_password = hash;
26+
27+
return user.save();
28+
29+
} else {
30+
31+
reject({ status: 401, message: 'Invalid Old Password !' });
32+
}
33+
})
34+
35+
.then(user => resolve({ status: 200, message: 'Password Updated Sucessfully !' }))
36+
37+
.catch(err => reject({ status: 500, message: 'Internal Server Error !' }));
38+
39+
});
40+
41+
exports.resetPasswordInit = email =>
42+
43+
new Promise((resolve, reject) => {
44+
45+
const random = randomstring.generate(8);
46+
47+
user.find({ email: email })
48+
49+
.then(users => {
50+
51+
if (users.length == 0) {
52+
53+
reject({ status: 404, message: 'User Not Found !' });
54+
55+
} else {
56+
57+
let user = users[0];
58+
59+
const salt = bcrypt.genSaltSync(10);
60+
const hash = bcrypt.hashSync(random, salt);
61+
62+
user.temp_password = hash;
63+
user.temp_password_time = new Date();
64+
65+
return user.save();
66+
}
67+
})
68+
69+
.then(user => {
70+
71+
const transporter = nodemailer.createTransport(`smtps://${config.email}:${config.password}@smtp.gmail.com`);
72+
73+
const mailOptions = {
74+
75+
from: `"${config.name}" <${config.email}>`,
76+
to: email,
77+
subject: 'Reset Password Request ',
78+
html: `Hello ${user.name},<br><br>
79+
&nbsp;&nbsp;&nbsp;&nbsp; Your reset password token is <b>${random}</b>.
80+
If you are viewing this mail from a Android Device click this <a href = "http://learn2crack/${random}">link</a>.
81+
The token is valid for only 2 minutes.<br><br>
82+
Thanks,<br>
83+
Learn2Crack.`
84+
85+
};
86+
87+
return transporter.sendMail(mailOptions);
88+
89+
})
90+
91+
.then(info => {
92+
93+
console.log(info);
94+
resolve({ status: 200, message: 'Check mail for instructions' })
95+
})
96+
97+
.catch(err => {
98+
99+
console.log(err);
100+
reject({ status: 500, message: 'Internal Server Error !' });
101+
102+
});
103+
});
104+
105+
exports.resetPasswordFinish = (email, token, password) =>
106+
107+
new Promise((resolve, reject) => {
108+
109+
user.find({ email: email })
110+
111+
.then(users => {
112+
113+
let user = users[0];
114+
115+
const diff = new Date() - new Date(user.temp_password_time);
116+
const seconds = Math.floor(diff / 1000);
117+
console.log(`Seconds : ${seconds}`);
118+
119+
if (seconds < 120) {
120+
121+
return user;
122+
123+
} else {
124+
125+
reject({ status: 401, message: 'Time Out ! Try again' });
126+
}
127+
})
128+
129+
.then(user => {
130+
131+
if (bcrypt.compareSync(token, user.temp_password)) {
132+
133+
const salt = bcrypt.genSaltSync(10);
134+
const hash = bcrypt.hashSync(password, salt);
135+
user.hashed_password = hash;
136+
user.temp_password = undefined;
137+
user.temp_password_time = undefined;
138+
139+
return user.save();
140+
141+
} else {
142+
143+
reject({ status: 401, message: 'Invalid Token !' });
144+
}
145+
})
146+
147+
.then(user => resolve({ status: 200, message: 'Password Changed Sucessfully !' }))
148+
149+
.catch(err => reject({ status: 500, message: 'Internal Server Error !' }));
150+
151+
});

node-login/functions/profile.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const user = require('../models/user');
4+
5+
exports.getProfile = email =>
6+
7+
new Promise((resolve,reject) => {
8+
9+
user.find({ email: email }, { name: 1, email: 1, created_at: 1, _id: 0 })
10+
11+
.then(users => resolve(users[0]))
12+
13+
.catch(err => reject({ status: 500, message: 'Internal Server Error !' }))
14+
15+
});

node-login/functions/register.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
3+
const user = require('../models/user');
4+
const bcrypt = require('bcryptjs');
5+
6+
exports.registerUser = (name, email, password) =>
7+
8+
new Promise((resolve,reject) => {
9+
10+
const salt = bcrypt.genSaltSync(10);
11+
const hash = bcrypt.hashSync(password, salt);
12+
13+
const newUser = new user({
14+
15+
name: name,
16+
email: email,
17+
hashed_password: hash,
18+
created_at: new Date()
19+
});
20+
21+
newUser.save()
22+
23+
.then(() => resolve({ status: 201, message: 'User Registered Sucessfully !' }))
24+
25+
.catch(err => {
26+
27+
if (err.code == 11000) {
28+
29+
reject({ status: 409, message: 'User Already Registered !' });
30+
31+
} else {
32+
33+
reject({ status: 500, message: 'Internal Server Error !' });
34+
}
35+
});
36+
});
37+
38+

node-login/models/user.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
const mongoose = require('mongoose');
4+
5+
const Schema = mongoose.Schema;
6+
7+
const userSchema = mongoose.Schema({
8+
9+
name : String,
10+
email : String,
11+
hashed_password : String,
12+
created_at : String,
13+
temp_password : String,
14+
temp_password_time: String
15+
16+
});
17+
18+
mongoose.Promise = global.Promise;
19+
mongoose.connect('mongodb://localhost:27017/node-login');
20+
21+
module.exports = mongoose.model('user', userSchema);

node-login/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "node-login",
3+
"version": "1.0.0",
4+
"main": "app.js",
5+
"dependencies": {
6+
"basic-auth": "^1.0.4",
7+
"bcryptjs": "^2.3.0",
8+
"body-parser": "^1.15.2",
9+
"express": "^4.14.0",
10+
"jsonwebtoken": "^7.1.9",
11+
"mongoose": "^4.6.0",
12+
"morgan": "^1.7.0",
13+
"nodemailer": "^2.6.0",
14+
"randomstring": "^1.1.5"
15+
}
16+
}

0 commit comments

Comments
 (0)