Skip to content

Commit

Permalink
Separate Persist user sessions page
Browse files Browse the repository at this point in the history
  • Loading branch information
pabasara-mahindapala committed Jan 7, 2025
1 parent 24fb87d commit e065f90
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 9 deletions.
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
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 @@ -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
Expand Down

0 comments on commit e065f90

Please sign in to comment.