1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/src/lib/middleware/demo-authentication.ts

52 lines
1.5 KiB
TypeScript
Raw Normal View History

import { Application } from 'express';
import AuthenticationRequired from '../types/authentication-required';
import { IUnleashServices } from '../types/services';
function demoAuthentication(
app: Application,
basePath: string = '',
{ userService }: Pick<IUnleashServices, 'userService'>,
): void {
app.post(`${basePath}/api/admin/login`, async (req, res) => {
const { email } = req.body;
const user = await userService.loginUserWithoutPassword(email, true);
const session = req.session || {};
// @ts-ignore
session.user = user;
// @ts-ignore
req.session = session;
2018-11-22 20:47:06 +01:00
res.status(200)
// @ts-ignore
.json(req.session.user)
.end();
});
app.use(`${basePath}/api/admin/`, (req, res, next) => {
// @ts-ignore
if (req.session.user && req.session.user.email) {
// @ts-ignore
req.user = req.session.user;
}
next();
});
app.use(`${basePath}/api/admin/`, (req, res, next) => {
// @ts-ignore
if (req.user) {
return next();
}
return res
.status(401)
.json(
new AuthenticationRequired({
path: `${basePath}/api/admin/login`,
2021-04-22 10:53:47 +02:00
type: 'demo',
message:
'You have to identify yourself in order to use Unleash.',
}),
)
.end();
});
}
export default demoAuthentication;