Skip to content

Instantly share code, notes, and snippets.

@ganeshan
Created April 19, 2022 01:58
Show Gist options
  • Select an option

  • Save ganeshan/8cd23fa5b8d5be291a049703e2e0533e to your computer and use it in GitHub Desktop.

Select an option

Save ganeshan/8cd23fa5b8d5be291a049703e2e0533e to your computer and use it in GitHub Desktop.
Barebones NodeJS app - Add Node.js User Authentication in 10 Minutes!
const { auth, requiresAuth } = require('express-openid-connect');
const express = require('express');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3000;
const config = {
authRequired: false,
auth0Logout: true,
baseURL: process.env.BASE_URL,
clientID: process.env.CLIENT_ID,
issuerBaseURL: process.env.ISSUER_BASE_URL,
secret: process.env.SECRET
};
// auth router attaches /login, /logout, and /callback routes to the baseURL
app.use(auth(config));
// req.isAuthenticated is provided from the auth router
app.get('/', (req, res) => {
res.send(req.oidc.isAuthenticated() ? 'Logged in' : 'Logged out')
});
app.get('/profile', requiresAuth(), (req, res) => {
res.json(req.oidc.user);
});
app.listen(port, () => {
console.log(`Listening on port: ${port}`)
})
@ganeshan
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment