2023-07-23 16:04:49 -07:00

52 lines
1.3 KiB
TypeScript

import { HttpConfig } from '../../http/server';
import { HttpWebDependencies } from '../server';
import { ErrorCode, ErrorInfo } from '../../http/send-error';
import { redirect_303_see_other } from '../../http/redirects';
import { FastifyInstance, FastifyReply, RouteShorthandOptions } from 'fastify';
export function register_login_page_endpoint(http_server: FastifyInstance, conf: HttpConfig, { pkce_cookie, session }: HttpWebDependencies) {
const opts: RouteShorthandOptions = {
schema: { },
};
http_server.get('/login', opts, async (req, res) => {
try {
await session.check_login(req);
return redirect_303_see_other(res, conf.exposed_url, 'Already logged in');
}
catch (error) {
return send_login_page(res);
}
});
function send_login_page(res: FastifyReply) {
res.status(200);
res.header('content-type', 'text/html; charset=utf-8');
session.reset(res);
pkce_cookie.reset(res);
return render_login_page();
}
}
export const render_login_page = (error_code?: ErrorCode, error?: ErrorInfo) => `<!doctype html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="/login" method="POST">
<button type="submit">Login with OpenID Connect</button>
</form>
${error_code ? `
<div>
<h4>Login failed</h4>
<b>Error Code:</b> ${error_code}<br />
<b>Error Message:</b> ${error.message}
</div>
` : ''}
</body>
</html>
`;