-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (39 loc) · 972 Bytes
/
index.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
const fetch = require("node-fetch");
async function postData(data = {}) {
const url = "https://soauth.sojs.repl.co/checkValid"
const response = await fetch(url, {
method: 'POST',
mode: "cors",
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify(data)
});
return response.json()
}
function soauth(req,res,next){
if(!req.session){
console.log("Please install a session manager");
}
if(!req.session.so_auth){
req.session.so_auth = {}
}
if(!req.cookies){
console.log("Please install a cookie parser middleware")
}else{
if(req.cookies["so-auth"]){
postData({"token":req.cookies["so-auth"]}).then(d=>{
if(d.message){ console.log(d.message); req.session.so_auth.user = false; next()}else{
req.session.so_auth.user = d.data;
next()
}
})
}else{
next();
}
}
}
module.exports = {
soauth
}