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
Ivar Conradi Østhus c4b697b57d
Feat/api key scoping (#941)
Co-authored-by: Christopher Kolstad <chriswk@getunleash.ai>
2021-09-15 20:28:10 +02:00

52 lines
1.5 KiB
TypeScript

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;
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`, (req, res, next) => {
// @ts-ignore
if (req.user) {
return next();
}
return res
.status(401)
.json(
new AuthenticationRequired({
path: `${basePath}/api/admin/login`,
type: 'demo',
message:
'You have to identify yourself in order to use Unleash.',
}),
)
.end();
});
}
export default demoAuthentication;