-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.mjs
75 lines (63 loc) · 2.03 KB
/
auth.mjs
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
/*
* auth.js
*/
import passport from "passport";
import express from "express";
const router = express.Router();
import {CURRENT_TERMS_VERSION, ensureLogIn} from "../helpers/auth.mjs";
import {dataHandler} from "../helpers/handlers.mjs";
import {setTermsConsent} from "../models/consents.mjs";
// deserializeUser takes care of loading consentedToTerms
const respondWithUser = (req, res) => dataHandler(res)(req.user ?? undefined);
router.get("/user", respondWithUser);
router.put(
"/user",
ensureLogIn,
(req, res) => {
// TODO: Validate body
console.log(req.body);
const consent = Boolean(req.body.consentedToTerms);
setTermsConsent(req.user.ip, CURRENT_TERMS_VERSION, consent)
.then(() => {
req.user.consentedToTerms = consent;
return respondWithUser(req, res);
});
});
router.get("/login",
// Add some middleware to handle specified redirects
// (req, res, next) => {
// // We are fine not to sanitize this, since the IdP will not redirect if it's an invalid destination
// const returnTo = req.query.redirect;
// if (returnTo) {
// req.session.returnTo = `${process.env.VARWIG_BASE_URL}${returnTo}`;
// req.session.save(err => {if (err) console.error(err); next();});
// } else {
// next();
// }
// },
passport.authenticate("custom"),
(req, res, next) => {
const returnTo = req.query.redirect;
if (returnTo) {
console.info(`/api/auth/login: redirecting to ${returnTo}`);
res.redirect(returnTo);
} else {
next();
}
});
// router.get("/callback", passport.authenticate("openidconnect", {
// successReturnToOrRedirect: `${process.env.VARWIG_BASE_URL ?? ""}/`,
// failureRedirect: `${process.env.VARWIG_BASE_URL ?? ""}/auth-failure`,
// failureMessage: true,
// keepSessionInfo: true,
// }));
router.get('/logout', (req, res, next) => {
req.logout(err => {
if (err) return next(err);
req.session.destroy(err => {
if (err) return next(err);
res.redirect('/');
});
});
});
export default router;