I’m using Node and trying to use oidc.ensureAuthentication to protect some of my routes. I am currently testing it on a page called alarms with a file called “alarms.html” in the directory. I am able to login using the oidc-middleware package however afterwards I am unable to use oidc.ensureAuthentication. My URL is a localhost port.
My server.js
const express = require('express');
const app = express();
const session = require('express-session');
const { ExpressOIDC } = require('@okta/oidc-middleware');
const port = 8080;
// Setting up the public directory
app.use(express.static(__dirname));
app.listen(port, () => console.log(`listening on port ${port}!`));
// session support is required to use ExpressOIDC
app.use(session({
secret: 'this should be secure',
resave: true,
saveUninitialized: false
}));
const oidc = new ExpressOIDC({
issuer: <URL>/oauth2/default',
client_id: '.....',
client_secret: '.....',
redirect_uri: '<URL>/authorization-code/callback',
scope: 'openid profile',
appBaseUrl: '<URL>'
});
// ExpressOIDC attaches handlers for the /login and /authorization-code/callback routes
app.use(oidc.router);
app.get('/alarms', oidc.ensureAuthenticated(), (req, res) => {
console.log('Authenticated');
res.render('alarms');
});
Hello,
I ran a test using a similar setup found at the repo, and the oidc.ensureAuthentication worked in protecting my route. If I am logged in, I am able to access routes protected by it, If I am not logged in I get redirected to the login page. Could you detail what you are seeing?
So once I enter into the localhost, I check the protected route to see if I am redirect to login page, however even if I am not logged in, I can still view and access the protected page.
Since your app does not have logout functionality are you sure you don’t have an Okta session the entire time? The below is form our repo, can you try running this sample and see if you have the same issue. Make sure that in the Okta console for the web app you are testing with you set the ‘Logout Redirect URIs’ to ‘http://localhost/’