Ghost/ghost/session-service
Sam Lord 07afa6500d
Changed SSO adapter to automatically verify sessions (#21388)
ref ENG-1680

SSO is a different flow that wouldn't need the extra email verification
flow
2024-11-11 22:26:40 +00:00
..
lib Changed SSO adapter to automatically verify sessions (#21388) 2024-11-11 22:26:40 +00:00
test Changed SSO adapter to automatically verify sessions (#21388) 2024-11-11 22:26:40 +00:00
.eslintignore Added @tryghost/session-service module (#35) 2020-04-02 15:26:05 +02:00
.eslintrc.js Remove trailing commas from .eslintrc.js files 2020-08-04 14:48:07 +01:00
index.js Added eslint rule for file naming convention 2023-05-09 12:34:34 -04:00
package.json Added support for fetching device details when creating session 2024-10-21 11:01:40 +01:00
README.md Added function names to more middleware 2024-05-06 17:51:39 +02:00

Session Service

Usage

const SessionService = require('@tryghost/session-service');

const sessionService = SessionService({
    async getSession(req, res) {
        return new Promise((resolve, reject) => {
            require('express-session')(config)(req, res, (err) => {
                if (err) {
                    reject(err);
                }
                resolve(req.session);
            })
        })
    },
    async findUserById({id}) {
        return UserModel.findUserById(id);
    },
    getOriginOfRequest(req) {
        return req.headers.origin;
    }
});

app.use(async function sessionMiddleware(req, res, next) {
    try {
        const user = await sessionService.getUserForSession(req, res);
        req.user = user;
        next();
    } catch (err) {
        next(err);
    }
});

app.post('/login', async (req, res) => {
    try {
        const user = await UserModel.verify(req.body);
        await sessionService.createSessionForUser(req, res, user);
        res.redirect('/home');
    } catch (err) {
        return next(err);
    }
});

app.post('/logout', async (req, res) => {
   try {
        await sessionService.destroyCurrentSession(req, res);
        res.redirect('/login');
    } catch (err) {
        return next(err);
    }
});