-
Notifications
You must be signed in to change notification settings - Fork 1
/
verification.ts
103 lines (94 loc) · 3.08 KB
/
verification.ts
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
require('dotenv').config();
import express from 'express';
import jwt from 'jsonwebtoken';
import axios from 'axios';
import exphbs from 'express-handlebars';
import qs from 'qs';
import { discordUsers } from './models/schema';
const app = express();
export interface TokenInterface{
discordId : string;
studentNumber : string;
name : string;
}
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.engine('handlebars', exphbs({
extname: "hbs",
defaultLayout: "",
layoutsDir: ""
}));
app.set('view engine', 'handlebars');
app.get('/verify/:id', (req, res) => {
res.render('verify', { captcha : process.env.HCAPTCHA_SITE_KEY});
});
app.post('/verify/:id', async(req, res) => {
if(!req.body['h-captcha-response']){
return res.render('verify', {
captcha : process.env.HCAPTCHA_SITE_KEY,
error : "Invalid captcha, please try again"
});
}
axios({
method: 'POST',
url: `https://hcaptcha.com/siteverify`,
data: qs.stringify({
response : req.body['h-captcha-response'],
secret : process.env.HCAPTCHA_SECRET_KEY
}),
headers: { 'Content-Type':'application/x-www-form-urlencoded' }
})
.then(async response => {
if(response.data.success){
try{
let decoded = jwt.verify(req.params.id, process.env.JWT_SECRET) as TokenInterface;
let discordId = decoded.discordId;
let studentNumber = decoded.studentNumber;
let fullname = decoded.name;
if(await discordUsers.exists({email : `${studentNumber}@student.publicboard.ca`})){
res.render('verify', {
error: "Student ID already being used! Please contact an admin!"
});
}
else{
await discordUsers.findOneAndUpdate({discordId : discordId},
{
name : fullname,
discordId: discordId,
email: `${studentNumber}@student.publicboard.ca`
},
{ new : true, upsert: true});
res.render('verify', {
success: true,
redirect : process.env.REDIRECT_URL
});
}
}
catch(err){
res.render('verify', {
error: "Invalid token, contact an admin"
});
}
}
else{
res.render('verify', {
captcha : process.env.HCAPTCHA_SITE_KEY,
error : "Captcha error, please try again"
});
}
})
.catch(err => {
res.render('verify', {
captcha : process.env.HCAPTCHA_SITE_KEY,
error : "Captcha error, please try again"
});
});
});
app.use((req, res) => {
res.redirect(process.env.REDIRECT_URL);
})
app.listen(3000, ()=>{
console.log('Listening on port 3000');
});