2021-08-12 15:04:37 +02:00
|
|
|
import { Application } from 'express';
|
|
|
|
import AuthenticationRequired from '../types/authentication-required';
|
|
|
|
import { IUnleashServices } from '../types/services';
|
2017-11-16 16:45:01 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
function demoAuthentication(
|
|
|
|
app: Application,
|
|
|
|
basePath: string = '',
|
|
|
|
{ userService }: Pick<IUnleashServices, 'userService'>,
|
|
|
|
): void {
|
2021-04-09 13:46:53 +02:00
|
|
|
app.post(`${basePath}/api/admin/login`, async (req, res) => {
|
|
|
|
const { email } = req.body;
|
|
|
|
const user = await userService.loginUserWithoutPassword(email, true);
|
2021-08-12 15:04:37 +02:00
|
|
|
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)
|
2021-08-12 15:04:37 +02:00
|
|
|
// @ts-ignore
|
2017-11-16 16:45:01 +01:00
|
|
|
.json(req.session.user)
|
|
|
|
.end();
|
|
|
|
});
|
|
|
|
|
2019-03-03 11:41:56 +01:00
|
|
|
app.use(`${basePath}/api/admin/`, (req, res, next) => {
|
2021-08-12 15:04:37 +02:00
|
|
|
// @ts-ignore
|
2017-11-16 16:45:01 +01:00
|
|
|
if (req.session.user && req.session.user.email) {
|
2021-08-12 15:04:37 +02:00
|
|
|
// @ts-ignore
|
2017-11-16 16:45:01 +01:00
|
|
|
req.user = req.session.user;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2019-03-03 11:41:56 +01:00
|
|
|
app.use(`${basePath}/api/admin/`, (req, res, next) => {
|
2021-08-12 15:04:37 +02:00
|
|
|
// @ts-ignore
|
2017-11-16 16:45:01 +01:00
|
|
|
if (req.user) {
|
2020-04-14 22:29:11 +02:00
|
|
|
return next();
|
2017-11-16 16:45:01 +01:00
|
|
|
}
|
2020-04-14 22:29:11 +02:00
|
|
|
return res
|
2021-08-12 15:04:37 +02:00
|
|
|
.status(401)
|
2020-04-14 22:29:11 +02:00
|
|
|
.json(
|
|
|
|
new AuthenticationRequired({
|
|
|
|
path: `${basePath}/api/admin/login`,
|
2021-04-22 10:53:47 +02:00
|
|
|
type: 'demo',
|
2020-04-14 22:29:11 +02:00
|
|
|
message:
|
2021-02-04 12:21:08 +01:00
|
|
|
'You have to identify yourself in order to use Unleash.',
|
2020-04-14 22:29:11 +02:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
.end();
|
2017-11-16 16:45:01 +01:00
|
|
|
});
|
|
|
|
}
|
2021-08-12 15:04:37 +02:00
|
|
|
export default demoAuthentication;
|