From 24fb87d5485f3a32c57d73001e29f7ddc8b9a0d7 Mon Sep 17 00:00:00 2001 From: pabasara-mahindapala Date: Tue, 7 Jan 2025 12:35:49 +0530 Subject: [PATCH 1/2] Update passport-asgardeo package name --- .../docs/complete-guides/nodejs/accessing-protected-api.md | 2 +- .../docs/complete-guides/nodejs/install-passport-asgardeo.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/en/asgardeo/docs/complete-guides/nodejs/accessing-protected-api.md b/en/asgardeo/docs/complete-guides/nodejs/accessing-protected-api.md index aea2a37ef7..9613dc3d35 100644 --- a/en/asgardeo/docs/complete-guides/nodejs/accessing-protected-api.md +++ b/en/asgardeo/docs/complete-guides/nodejs/accessing-protected-api.md @@ -24,7 +24,7 @@ We will add the `internal_login` scope as well to the `scope` parameter in the { ```javascript hl_lines="23 38 51" var passport = require("passport"); -var AsgardeoStrategy = require("passport-asgardeo"); +var AsgardeoStrategy = require("@asgardeo/passport-asgardeo"); const ASGARDEO_BASE_URL = "https://api.asgardeo.io/t/"; passport.use( diff --git a/en/asgardeo/docs/complete-guides/nodejs/install-passport-asgardeo.md b/en/asgardeo/docs/complete-guides/nodejs/install-passport-asgardeo.md index 5eac5c4d32..715faf8e6e 100644 --- a/en/asgardeo/docs/complete-guides/nodejs/install-passport-asgardeo.md +++ b/en/asgardeo/docs/complete-guides/nodejs/install-passport-asgardeo.md @@ -10,7 +10,7 @@ For this integration, we are going to use Passport and the passport-asgardeo str ```bash npm install passport -npm install passport-asgardeo +npm install @asgardeo/passport-asgardeo npm install dotenv ``` @@ -30,7 +30,7 @@ Add the following code to the `routes/auth.js` file: ```javascript var passport = require("passport"); -var AsgardeoStrategy = require("passport-asgardeo"); +var AsgardeoStrategy = require("@asgardeo/passport-asgardeo"); const ASGARDEO_BASE_URL = "https://api.asgardeo.io/t/"; passport.use( From e065f90e063e76000e7303eb865a577b538fed65 Mon Sep 17 00:00:00 2001 From: pabasara-mahindapala Date: Tue, 7 Jan 2025 18:25:07 +0530 Subject: [PATCH 2/2] Separate Persist user sessions page --- .../nodejs/add-login-and-logout.md | 11 +-- .../nodejs/persist-user-sessions.md | 87 +++++++++++++++++++ en/asgardeo/mkdocs.yml | 1 + 3 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 en/asgardeo/docs/complete-guides/nodejs/persist-user-sessions.md diff --git a/en/asgardeo/docs/complete-guides/nodejs/add-login-and-logout.md b/en/asgardeo/docs/complete-guides/nodejs/add-login-and-logout.md index 58052a23f0..d77baf27c1 100644 --- a/en/asgardeo/docs/complete-guides/nodejs/add-login-and-logout.md +++ b/en/asgardeo/docs/complete-guides/nodejs/add-login-and-logout.md @@ -139,12 +139,11 @@ Begin by installing the necessary dependencies: ```bash npm install express-session -npm install connect-sqlite3 ``` Open `app.js` and modify the file as shown below to add passport authentication with session support. -```javascript hl_lines="8-10 27-35" +```javascript hl_lines="8 9 26-33" require("dotenv").config(); var createError = require('http-errors'); @@ -154,7 +153,6 @@ var cookieParser = require('cookie-parser'); var logger = require('morgan'); var session = require("express-session"); var passport = require("passport"); -var SQLiteStore = require("connect-sqlite3")(session); var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); @@ -176,7 +174,6 @@ app.use( secret: "keyboard cat", resave: false, saveUninitialized: false, - store: new SQLiteStore({ db: "sessions.db", dir: "./var/db" }), }) ); app.use(passport.authenticate("session")); @@ -204,13 +201,9 @@ app.use(function(err, req, res, next) { module.exports = app; ``` -Run the following command to create the SQLite database location: -```bash -mkdir -p ./var/db -``` -Next we need to configure Passport to manage the login session by adding serializeUser and deserializeUser functions. Open `routes/auth.js` and add the following code lines after AsgardeoStrategy configuration. +Next we need to configure Passport to manage the login session by adding serializeUser and deserializeUser functions. Open `routes/auth.js` and add the following code lines after the AsgardeoStrategy configuration. ```javascript passport.serializeUser(function (user, cb) { diff --git a/en/asgardeo/docs/complete-guides/nodejs/persist-user-sessions.md b/en/asgardeo/docs/complete-guides/nodejs/persist-user-sessions.md new file mode 100644 index 0000000000..aff77003f4 --- /dev/null +++ b/en/asgardeo/docs/complete-guides/nodejs/persist-user-sessions.md @@ -0,0 +1,87 @@ +--- +template: templates/complete-guide.html +heading: Persist user sessions +read_time: 2 min +--- + +## Persist user sessions + +For improved user experience, let's persist user sessions in our app using a SQLite in-memory database. + +Begin by installing the necessary dependency: + +```bash +npm install connect-sqlite3 +``` + +Open `app.js` and modify the file as shown below to add session persistence. + +```javascript hl_lines="10 32" +require("dotenv").config(); + +var createError = require('http-errors'); +var express = require('express'); +var path = require('path'); +var cookieParser = require('cookie-parser'); +var logger = require('morgan'); +var session = require("express-session"); +var passport = require("passport"); +var SQLiteStore = require("connect-sqlite3")(session); + +var indexRouter = require('./routes/index'); +var usersRouter = require('./routes/users'); +var authRouter = require("./routes/auth"); + +var app = express(); + +// view engine setup +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'ejs'); + +app.use(logger('dev')); +app.use(express.json()); +app.use(express.urlencoded({ extended: false })); +app.use(cookieParser()); +app.use(express.static(path.join(__dirname, 'public'))); +app.use( + session({ + secret: "keyboard cat", + resave: false, + saveUninitialized: false, + store: new SQLiteStore({ db: "sessions.db", dir: "./var/db" }), + }) +); +app.use(passport.authenticate("session")); + +app.use('/', indexRouter); +app.use('/users', usersRouter); +app.use("/", authRouter); + +// catch 404 and forward to error handler +app.use(function(req, res, next) { + next(createError(404)); +}); + +// error handler +app.use(function(err, req, res, next) { + // set locals, only providing error in development + res.locals.message = err.message; + res.locals.error = req.app.get('env') === 'development' ? err : {}; + + // render the error page + res.status(err.status || 500); + res.render('error'); +}); + +module.exports = app; +``` + +Run the following command to create the SQLite database location: + +```bash +mkdir -p ./var/db +``` + +Now, when the user logs in, the user session will be stored in the SQLite database. + +We've now got session persistence set up in our app. \ No newline at end of file diff --git a/en/asgardeo/mkdocs.yml b/en/asgardeo/mkdocs.yml index 557537e4c1..68888eb5a2 100644 --- a/en/asgardeo/mkdocs.yml +++ b/en/asgardeo/mkdocs.yml @@ -510,6 +510,7 @@ nav: - Create a Node.js app: complete-guides/nodejs/create-a-nodejs-app.md - Configure Passport Asgardeo: complete-guides/nodejs/install-passport-asgardeo.md - Add login and logout: complete-guides/nodejs/add-login-and-logout.md + - Persist user sessions: complete-guides/nodejs/persist-user-sessions.md - Display user details: complete-guides/nodejs/display-logged-in-user-details.md - Securing Routes: complete-guides/nodejs/securing-routes-within-the-app.md - Accessing protected API : complete-guides/nodejs/accessing-protected-api.md