-
Notifications
You must be signed in to change notification settings - Fork 0
/
login1.js
142 lines (129 loc) · 4.06 KB
/
login1.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
var mysql = require('mysql');
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var path = require('path');
var nodemailer = require('nodemailer');
//var async = require('async');
var crypto = require('crypto');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'nodelogin'
});
var hbs = require('nodemailer-express-handlebars'),
email = process.env.MAILER_EMAIL_ID || '[email protected]',
pass = process.env.MAILER_PASSWORD || 'generator@2020'
nodemailer = require('nodemailer');
var smtpTransport = nodemailer.createTransport({
service: process.env.MAILER_SERVICE_PROVIDER || 'Gmail',
auth: {
user: email,
pass: pass
}
});
var handlebarsOptions = {
viewEngine: 'handlebars',
viewPath: path.resolve('./api/templates/'),
extName: '.html'
};
smtpTransport.use('compile', hbs(handlebarsOptions));
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'generator@2020'
}
});
var app = express();
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());
app.get('/', function(request, response) {
response.sendFile(path.join(__dirname + '/register.html'));
});
app.get('/forgot-password', function(request, response) {
response.sendFile(path.join(__dirname + '/forgot-password.html'));
});
app.post('/reset-password', function(request, response) {
var email = request.body.email;
token = crypto.randomBytes(32).toString('hex');
console.log(token);
const mailOptions = {
from: '[email protected]', // sender address
to: email, // list of receivers
subject: 'Your password reset link.', // Subject line
html: '<p>http://localhost:3000/</p>{{token}}'// plain text body
};
transporter.sendMail(mailOptions, function (err, info) {
if(err)
console.log(err)
else
console.log(info);
});
response.end();
});
app.post('/register-user', function(request, response) {
var email = request.body.email;
var username = request.body.username;
var password = request.body.password;
var cpassword = request.body.cpassword;
var sql = "SELECT USERNAME FROM USERS WHERE username = '"+username+"'";
connection.query(sql, function (err, results,fields) {
if (results.length > 0) {
//return response.render('',{message:'Username already exists.'});
response.send("Username already exists.");
}
else if(password!==cpassword)
{ response.send("Passwords do not match.");}
else {
sql = "INSERT INTO users (EMAIL,USERNAME,PASSWORD) VALUES ('"+email+"','"+username+"','"+password+"')";
connection.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
response.redirect('/login');
}
response.end();
});
//response.sendFile(path.join(__dirname + '/login.html'));
});
app.get('/login', function(request, response) {
response.sendFile(path.join(__dirname + '/login.html'));
});
app.get('/mycss', function(request, response) {
response.sendFile(path.join(__dirname + '/login.css'));
});
app.post('/auth', function(request, response) {
var username = request.body.username;
var password = request.body.password;
if (username && password) {
connection.query('SELECT USERNAME,PASSWORD FROM USERS WHERE username = ? AND password = ?', [username, password], function(error, results, fields) {
if (results.length > 0) {
request.session.loggedin = true;
request.session.username = username;
response.redirect('/home');
} else {
response.send('Incorrect Username and/or Password!');
}
response.end();
});
} else {
response.send('Please enter Username and Password!');
response.end();
}
});
app.get('/home', function(request, response) {
if (request.session.loggedin) {
response.send('Welcome back, ' + request.session.username + '!');
} else {
response.send('Please login to view this page!');
}
response.end();
});
app.listen(3000);