-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
156 lines (104 loc) · 2.69 KB
/
Copy pathserver.js
File metadata and controls
156 lines (104 loc) · 2.69 KB
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
const express =require('express');
const path=require ('path');
const bodyParser = require('body-parser');
const knex = require('knex');
const { Client } = require('pg');
const client = new Client({
host: '127.0.0.1',
user:'postgres',
password:'123456',
database:'loginform1',
port: 5432, // Default PostgreSQL port
});
const db=knex({
client:'pg',
connection:{
host: '127.0.0.1',
user:'postgres',
password:'123456',
database:'loginform1'
}
})
const app=express();
app.use(bodyParser.json());
let initialPath=path.join(__dirname,"public");
app.use(bodyParser.json());
app.use(express.static(initialPath));
app.get('/',(req,res)=>{
res.sendFile(path.join(initialPath,"index.html"));
})
app.get('/login',(req,res)=>{
res.sendFile(path.join(initialPath,"login.html"));
})
app.get('/register',(req,res)=>{
res.sendFile(path.join(initialPath,"register.html"));
})
//forgot password
app.get('/forgot',(req,res)=>{
res.sendFile(path.join(initialPath,"forgot.html"));
})
app.post('/register-user',(req,res)=>{
const {name, email, password } = req.body;
if(!name.length || !email.length || !password.length){
res.json('fill all the fields');
}
else{
db("users").insert({
name:name,
email:email,
password:password
})
.returning(["name", "email"])
.then(data=>{
res.json(data[0])
})
.catch(err =>{
if(err.detail.includes('already exists')){
res.json('email already exists');
}
})
}
})
app.post('/login-user',(req,res)=>{
const{email, password}=req.body;
db.select('name','email')
.from('users')
.where({
email:email,
password:password
})
.then(data=>{
if(data.length){
res.json(data[0]);
}else{
res.json('email or password is incorrect');
}
})
})
// //forgot password
app.put('/forgot-user',(req,res)=>{ //changed
const{reemail, repassword}=req.body;
client.connect()
// Update query
const updateQuery = `
UPDATE users
SET password = $1
WHERE email = $2
`;
// Execute the update query
return client.query(updateQuery, [repassword, reemail] )
.then(result => {
console.log('Update successful:', result.rowCount, 'row(s) affected');
res.json("password changed successfully \n redirecting to login page")
})
.catch(error => {
console.error('Error:', error);
})
.finally(() => {
// Close the database connection
client.end();
});
})
app.listen(3000,(req,res)=>{
console.log('listening on port 3000......')
})