so now we have completed all the steps for integration
since we have the node and auth0 as two separate things
the integration happens in the app.js
we need to modify the app.js with some auth0 code
app.js /
const http = require('http');
const express = require('express');
var router = require('express').Router();
const { auth } = require('express-openid-connect');
const app = express();
app.use('/', router);
const hostname = '127.0.0.1';
const port = 3000;
http.createServer(app)
.listen(port, () => {
console.log(`Listening on `);
});
const config = {
authRequired: false,
auth0Logout: true,
secret: 'a long, randomly-generated string stored in env',
baseURL: 'http://localhost:3000',
clientID: 'QxpAXsXkzy0Vm2VdT6dFFS0VOKdrah67',
issuerBaseURL: 'https://dev-bnga041p.us.auth0.com'
};
console.log(config)
// 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) => {
console.log('entering au')
res.send(req.oidc.isAuthenticated() ? 'Logged in your App' : 'Logged out from App');
});
//add this to validate routes for authentication it used the same express-openid-connect as ///above for auth
const { requiresAuth } = require('express-openid-connect');
app.get('/profile', requiresAuth(), (req, res) => {
res.send(JSON.stringify(req.oidc.user));
});
two things to add
var router = require('express').Router();
const { auth } = require('express-openid-connect');
the express openid is used for authentication
so we need to install ' express-openid-connect ' in node to add the dependency
alright now once the code is added restart node app
now u can see the app behaviour as
when u use the URL -
http://localhost:3000/login
a new login page for authentication will be popped up
enter ur username and password if u don't have one
u can signup using the same screen
the authentication will be done through auth0 and u will land onto page
Logged in your App is from the code once it is authenticated which u can see from the code
to verify some user details
use the URL - http://localhost:3000/profile
to logout from use http://localhost:3000/logout
so now if you again try to login u will land in the auth page for credentials
a quick look on the code
const config = {
authRequired: false,
auth0Logout: true,
secret: 'a long, randomly-generated string stored in env',
baseURL: 'http://localhost:3000',
clientID: 'QxpAXsXkzy0Vm2VdT6dFFS0VOKdrah67',
issuerBaseURL: 'https://dev-bnga041p.us.auth0.com'
};
u can see the clientid is the one from our auth signup
alright then u can get the code frm the git too
see u for now .
git -
Comments
Post a Comment