-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
43 lines (29 loc) · 1.09 KB
/
auth.ts
File metadata and controls
43 lines (29 loc) · 1.09 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
import express from "express";
import bodyParser from "body-parser";
import {decryptToken, makeRandomSalt, makeToken, seedCrypto} from "./tokenCrypto";
const authEndpoint = express.Router();
const seed = seedCrypto(makeRandomSalt());
const makePayload = (username, password) => {
return {
username: username ?? '',
password: password ?? '',
}
}
authEndpoint.get('/isAuthenticated', (req, res) => {
try {
//console.log(req.hostname,req.socket.localPort);
const cookie = req.signedCookies['session'];
const result = decryptToken(seed,cookie)
console.log('Decrypted to', result);
res.status(200).end();
} catch(e) {
res.status(401).end();
}
});
authEndpoint.post('/',bodyParser.json(), (req, res) => {
let jsonPayload = makePayload(req.body['user'],req.body['pass']);
const token = makeToken(seed, jsonPayload)
res.cookie('session', token, { httpOnly: true , signed: true, secure: req.protocol !== 'http'});
res.status(200).end();
})
export default authEndpoint;