Hey, the route “/dashboard” Should only be accessed by authorised users however when i attempt to log on nothing happens and it takes me right back to the login page.
const express = require('express');
const { auth, requiresAuth } = require('express-openid-connect');
const path = require('path');
const app = express();
const config = {
authRequired: false,
auth0Logout: true,
baseURL: 'http://localhost:3000',
clientID: // hidden
issuerBaseURL: // hidden
secret: // hidden
};
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, '..', 'public')));
app.use(auth(config)); // Initialize the express-openid-connect middleware
app.use((req, res, next) => {
res.locals.user = req.oidc?.user; // Use optional chaining to avoid the error
next();
});
// Serve static files under '/FrontEnd' with authentication
app.use('/FrontEnd', express.static(path.join(__dirname, '../../FrontEnd')));
// Define routes
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '../../FrontEnd/html/index.html'));
});
app.get('/dashboard', requiresAuth(), function (req, res, next) {
res.sendFile(path.join(__dirname, '../../FrontEnd/html/home.html'), {
userProfile: JSON.stringify(req.oidc?.user, null, 2),
title: 'Profile page'
});
}); // auth unfunctional not sending user to secure route due to inauth?
const port = 3000;
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});