104f84f252
As discussed with the product team we want to enforce kebab-case file names for all files, with the exception of files which export a single class, in which case they should be PascalCase and reflect the class which they export. This will help find classes faster, and should push better naming for them too. Some files and packages have been excluded from this linting, specifically when a library or framework depends on the naming of a file for the functionality e.g. Ember, knex-migrator, adapter-manager |
||
---|---|---|
.. | ||
lib | ||
test | ||
.eslintignore | ||
.eslintrc.js | ||
index.js | ||
package.json | ||
README.md |
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 (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);
}
});