-
Notifications
You must be signed in to change notification settings - Fork 3
/
signup.js
79 lines (72 loc) · 2.39 KB
/
signup.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
const mysql = require('mysql');
const express = require('express');
const session = require('express-session');
const path = require('path');
const connection = mysql.createConnection({
host : 'localhost',
user : 'newuser',
password : 'Newmysql@123',
database : 'userdb'
});
const app = express();
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'static')));
// http://localhost:3000/
app.get('/', function(request, response) {
// Render login template
response.sendFile(path.join(__dirname + '/signup.html'));
});
// http://localhost:3000/new
app.post('/register', function(request, response) {
// Capture the input fields
let username = request.body.username;
let password = request.body.password;
let confirm_password = request.body.confirm_password;
// Ensure the input fields exists and are not empty
if (username && password && confirm_password) {
// Execute SQL query that'll select the account from the database based on the specified username and password
connection.query('SELECT * FROM userTable WHERE user = ?', [username], function(error, results, fields) {
// If there is an issue with the query, output the error
if (error) throw error;
// If the account exists
if (results.length > 0) {
response.send('Already Exists!')
} else if(confirm_password!=password) {
response.send('Password & Confirm Password is not Matched');
} else {
var users={
user : request.body.username,
password: request.body.password
}
var sql = 'INSERT INTO userTable SET ?';
connection.query(sql,users, function(error,results){
if (error) throw error;
});
response.send('entered into db!');
}
response.end();
});
} else {
response.send('Please enter Username and Password!');
response.end();
}
});
// http://localhost:3000/home
app.get('/home', function(request, response) {
// If the user is loggedin
if (request.session.loggedin) {
// Output username
response.send('Welcome back, ' + request.session.username + '!');
} else {
// Not logged in
response.send('Please login to view this page!');
}
response.end();
});
app.listen(3000);