-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
45 lines (38 loc) · 991 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
43
44
45
const express = require('express')
const createError = require('http-errors')
const { auth, requiresAuth } = require('express-openid-connect')
const app = express()
const port = process.env.PORT || 3000
// view
app.set('view engine', 'pug')
// auth middleware
// this also listens to /login path for login
app.use(
auth({
authRequired: false,
// idpLogout: true,
authorizationParams: {
response_type: 'code id_token',
},
})
)
// anonymous route
app.get('/', (req, res) => {
res.render('index', { req, title: 'Home' })
})
// protected route
app.get('/whoami', requiresAuth(), async (req, res) => {
// we can use req.oidc.user or fetch most updated user
const userInfo = await req.oidc.fetchUserInfo()
res.render('whoami', {
title: 'Who AM I',
req,
userInfo,
})
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
app.use(function (req, res, next) {
next(createError(404, 'Not found.'))
})