-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (71 loc) · 2.07 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
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
const path = require('path');
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const cookieparser = require("cookie-parser");
app.use(cookieparser());
app.use(express.urlencoded({extended:false}));
app.use(express.static(path.join(__dirname, '/public')))
const template_path = path.join(__dirname,"/templates/views")
app.set('view engine','hbs')
app.set('views',template_path);
app.use(express.json());
require("./src/db/connection");
const register = require("./src/model/regschema");
const Register = require('./src/model/regschema');
app.get('/', (req, res) => {
res.render('sort')
})
app.get('/sort', (req, res) => {
res.render('sort')
})
app.get('/searching', (req, res) => {
if (!req.cookies.sett) {
res.render('login_page')
}
else{
res.render('searching')
}
})
app.get('/log-in', (req, res) => {
res.render('login_page')
})
app.post("/register",async(req,res)=>{
try {
const registeruser = new register({
firstname:req.body.fname,
lastname:req.body.lname,
password:req.body.password,
username:req.body.username
})
res.cookie("sett",req.body.username);
const registered = await registeruser.save();
res.render('searching');
// res.json({ "registration": 'success' });
}catch (error) {
console.log(error);
res.render('login_page');
}
})
app.post("/login",async(req,res)=>{
try {
const username = req.body.usernamelog;
const password = req.body.passwordlog;
const data = await Register.findOne({username:username})
const isMatch = await bcrypt.compare(password,data.password);
if (isMatch == true) {
res.cookie("sett",req.body.username);
res.render('searching');
// res.json({ "log-in": 'success' });
} else {
res.render('login_page');
}
} catch (error) {
res.render('login_page');
}
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})