-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.js
96 lines (72 loc) · 2.2 KB
/
profile.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
const express = require('express');
const router = express.Router();
const profileSchema = require('../schemas/profile-schema');
const notificationSchema = require('../schemas/notification-schema');
const DiscordOauth2 = require("discord-oauth2");
require("dotenv").config();
const jwt = require('jsonwebtoken')
const oauth = new DiscordOauth2({
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
redirectUri: `${process.env.websiteURL}/discord`,
});
router.get('/settings', async (req, res, next) => {
try {
const user = await oauth.getUser(jwt.verify(req.cookies.get('key'), process.env.jwtSecret))
const notifRes = await notificationSchema.find({userId: user.id})
let notif = notifRes.reverse()
res.render('settings', {user: user, profile: await profileSchema.findOne({userId: user.id}), notifs: notif})
}catch(err) {
console.log(err)
return res.redirect('/logout')
}
})
router.get('/logout', async (req, res, next) => {
res.clearCookie('key')
res.redirect('/')
})
router.get('/profile', async (req, res, next) => {
try {
const user = await oauth.getUser(jwt.verify(req.cookies.get('key'), process.env.jwtSecret))
if (req.cookies.get('key')) {
res.render('profile', {
user: user,
profile: await profileSchema.findOne({userId: user.id})
});
}
else {
res.redirect('/settings')
}
}catch(error) {
console.log(error)
res.redirect('/logout')
}
});
router.post('/settings', async (req, res, next) => {
try {
console.log(req.body)
const user = await oauth.getUser(jwt.verify(req.cookies.get('key'), process.env.jwtSecret))
profileSchema.findOneAndUpdate({userId: user.id}, {
wallet: req.body.wallet,
name: req.body.name,
gender: req.body.gender,
country: req.body.country,
zipCode: req.body.zip,
houseNumber: req.body.house,
city: req.body.city,
streetName: req.body.street,
}, null, async (err, data) => {
if (err) {
console.log(err)
}
else {
//console.log(data)
}
})
res.redirect('/settings')
}catch(error) {
console.log(error)
res.redirect('/logout')
}
})
module.exports = router;