Skip to content

Commit

Permalink
Merge pull request #4950 from pabasara-mahindapala/nodejs-passport-as…
Browse files Browse the repository at this point in the history
…gardeo-guide

Separate Persist user sessions page and update package name
  • Loading branch information
sagara-gunathunga authored Jan 7, 2025
2 parents b9f4a9c + e065f90 commit 72d9199
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 2 additions & 9 deletions en/asgardeo/docs/complete-guides/nodejs/add-login-and-logout.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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"));
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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(
Expand Down
87 changes: 87 additions & 0 deletions en/asgardeo/docs/complete-guides/nodejs/persist-user-sessions.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions en/asgardeo/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,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
Expand Down

0 comments on commit 72d9199

Please sign in to comment.