forked from france-connect/service-provider-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (66 loc) · 2.51 KB
/
app.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
/**
* Entry point of the service provider(FS) demo app.
* @see @link{ https://partenaires.franceconnect.gouv.fr/fcp/fournisseur-service# }
*/
import express from 'express';
import logger from 'morgan';
import session from 'express-session';
import sessionstore from 'sessionstore';
import bodyParser from 'body-parser';
import config from './config';
import { SCOPES_GROUPS } from './helpers/utils';
import {
oauthLoginCallback,
oauthLogoutCallback,
getUser,
oauthLoginAuthorize,
oauthLogoutAuthorize,
} from './controllers/oauthAuthentication';
import { oauthDataCallback, getData, oauthDataAuthorize } from './controllers/oauthData';
import { callbackParamsValidatorMiddleware } from './validators/callbackParams';
const app = express();
// Note this enable to store user session in memory
// As a consequence, restarting the node process will wipe all sessions data
app.use(session({
store: sessionstore.createSessionStore(),
secret: 'demo secret', // put your own secret
cookie: {},
saveUninitialized: true,
resave: true,
}));
if (process.env.NODE_ENV !== 'test') {
app.use(logger('dev'));
}
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
// pass the user data from session to template global variables
app.use((req, res, next) => {
res.locals.user = req.session.user;
res.locals.data = req.session.data;
next();
});
// define variable globally for the footer
app.locals.franceConnectKitUrl = `${config.FC_URL}${config.FRANCE_CONNECT_KIT_PATH}`;
app.get('/', (req, res) => res.render('pages/home'));
app.get('/login', (req, res) => {
const scopesSelectedByDefault = `${config.MANDATORY_SCOPES} ${config.FC_SCOPES}`.split(' ');
return res.status(200).render('pages/login', { scopesSelectedByDefault, scopesFamilies: SCOPES_GROUPS });
});
app.post('/login-authorize', oauthLoginAuthorize);
app.get('/login-callback', callbackParamsValidatorMiddleware, oauthLoginCallback);
app.get('/logout', oauthLogoutAuthorize);
app.get('/logout-callback', oauthLogoutCallback);
app.get('/data-authorize', oauthDataAuthorize);
app.get('/data-callback', callbackParamsValidatorMiddleware, oauthDataCallback);
app.get('/user', getUser);
app.get('/data', getData);
// Setting app port
const port = process.env.PORT || '3000';
// Starting server
const server = app.listen(port, () => {
// eslint-disable-next-line no-console
console.log(`\x1b[32mServer listening on http://localhost:${port}\x1b[0m`);
});
export default server;