app-backend: log users out if session is invalid

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-04-09 12:20:14 +02:00
parent 2ff147b0d6
commit f9ad268ff2
+18 -8
View File
@@ -177,16 +177,26 @@ export async function createRouter(
const publicRouter = Router();
publicRouter.use(async (req, _res, next) => {
const credentials = await httpAuth.credentials(req, {
allow: ['user', 'service', 'none'],
allowLimitedAccess: true,
});
publicRouter.use(async (req, res, next) => {
try {
const credentials = await httpAuth.credentials(req, {
allow: ['user', 'service', 'none'],
allowLimitedAccess: true,
});
if (credentials.principal.type === 'none') {
if (credentials.principal.type === 'none') {
next();
} else {
next('router');
}
} catch {
// If we fail to authenticate, make sure the session cookie is cleared
// and continue as unauthenticated. If the user is logged in they will
// immediately be redirected back to the protected app via the POST.
await httpAuth.issueUserCookie(res, {
credentials: await auth.getNoneCredentials(),
});
next();
} else {
next('router');
}
});