Skip to content

Commit 75c3ddc

Browse files
committed
Support for BasePath
1 parent a7971ab commit 75c3ddc

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

app.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ var strategy = new Auth0Strategy(
2020
domain: process.env.AUTH0_DOMAIN,
2121
clientID: process.env.AUTH0_CLIENT_ID,
2222
clientSecret: process.env.AUTH0_CLIENT_SECRET,
23-
callbackURL:
24-
process.env.AUTH0_CALLBACK_URL || 'http://localhost:3000/callback'
23+
callbackURL: process.env.AUTH0_CALLBACK_URL || `http://localhost:3000${process.env.URL_CONTEXT || ''}/callback`
2524
},
2625
function (accessToken, refreshToken, extraParams, profile, done) {
2726
// accessToken is the token to call Auth0 API (not needed in the most cases)
@@ -53,7 +52,7 @@ app.use(cookieParser());
5352

5453
// config express-session
5554
var sess = {
56-
secret: 'CHANGE THIS SECRET',
55+
secret: process.env.SESSION_SECRET || 'CHANGE THIS SECRET',
5756
cookie: {},
5857
resave: false,
5958
saveUninitialized: true
@@ -118,4 +117,12 @@ app.use(function (err, req, res, next) {
118117
});
119118
});
120119

121-
module.exports = app;
120+
app.locals.basePath = process.env.URL_CONTEXT || '';
121+
let export_app = app;
122+
123+
if(app.locals.basePath) {
124+
//mount on subpath
125+
export_app = express().use(app.locals.basePath, app);
126+
}
127+
128+
module.exports = export_app;

routes/auth.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ var router = express.Router();
33
var passport = require('passport');
44

55
// Perform the login, after login Auth0 will redirect to callback
6-
router.get('/login', passport.authenticate('auth0', {
7-
scope: 'openid email profile'
8-
}), function (req, res) {
9-
res.redirect('/');
6+
router.get('/login',
7+
(req,res,next) => {
8+
// Save the referrer URL to redirect back after authentication...
9+
if(!req.session.returnTo && req.headers.referer && req.headers.referer != req.originalUrl) {
10+
req.session.returnTo = req.headers.referer;
11+
}
12+
next();
13+
},
14+
passport.authenticate('auth0', {
15+
scope: 'openid email profile'
16+
}),
17+
function (req, res) {
18+
res.redirect('/');
1019
});
1120

1221
// Perform the final stage of authentication and redirect to previously requested URL or '/user'

routes/users.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var router = express.Router();
66
router.get('/user', secured(), function (req, res, next) {
77
const { _raw, _json, ...userProfile } = req.user;
88
res.render('user', {
9-
userProfile: JSON.stringify(req.user, null, 2),
9+
userProfile: JSON.stringify(userProfile, null, 2),
1010
title: 'Profile page'
1111
});
1212
});

views/index.pug

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ block content
55
if locals.user
66
h4 You are logged in!
77
else
8-
h4 You are not logged in! Please #[a(href="/login") Log In] to continue.
8+
h4 You are not logged in! Please #[a(href=basePath+"/login") Log In] to continue.

views/layout.pug

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ doctype html
22
html
33
head
44
title= title
5-
link(rel='stylesheet', href='/stylesheets/style.css')
5+
link(rel='stylesheet', href=basePath+'/stylesheets/style.css')
66
link(rel='stylesheet', href='https://www.w3schools.com/w3css/4/w3.css')
77
link(rel='shortcut icon', href='//cdn2.auth0.com/styleguide/latest/lib/logos/img/favicon.png')
88

99
body
1010
nav.w3-bar.w3-border.w3-light-grey( role="navigation" )
1111
.w3-bar-item.w3-text-grey Auth0 - NodeJS
12-
a(href="/").w3-bar-item.w3-button Home
12+
a(href=basePath+"/").w3-bar-item.w3-button Home
1313
if locals.user
14-
a(href="/user" ).w3-bar-item.w3-button Profile
15-
a(id="qsLogoutBtn" href="/logout").w3-bar-item.w3-button Log Out
14+
a(href=basePath+"/user" ).w3-bar-item.w3-button Profile
15+
a(id="qsLogoutBtn" href=basePath+"/logout").w3-bar-item.w3-button Log Out
1616
else
17-
a(id="qsLoginBtn" href="/login").w3-bar-item.w3-button Log In
17+
a(id="qsLoginBtn" href=basePath+"/login").w3-bar-item.w3-button Log In
1818
block content

0 commit comments

Comments
 (0)