Handle all existing magic links at once

This commit is contained in:
Alice Gaudon 2021-11-08 13:22:20 +01:00
parent 7d2b088635
commit 95f6333d6a

View File

@ -116,32 +116,41 @@ export default class MagicLinkController<A extends Application> extends Controll
} }
protected async getLobby(req: Request, res: Response): Promise<void> { protected async getLobby(req: Request, res: Response): Promise<void> {
const link = await MagicLink.select() const links = await MagicLink.select()
.where('session_id', req.getSession().id) .where('session_id', req.getSession().id)
.sortBy('authorized') .sortBy('authorized')
.where('used', 0) .where('used', 0)
.first(); .get();
if (!link) { if (links.length === 0) {
throw new NotFoundHttpError('magic link', req.url); throw new NotFoundHttpError('magic link', req.url);
} }
if (!await link.isValid()) { let validLink;
for (const link of links) {
if (await link.isValid()) {
validLink = link;
} else {
req.flash('error', 'This magic link has expired. Please try again.'); req.flash('error', 'This magic link has expired. Please try again.');
res.redirect(link.getOrFail('original_url')); await link.delete();
}
}
if (!validLink) {
res.redirect(req.getIntendedUrl() || route('home'));
return; return;
} }
if (await link.isAuthorized()) { if (await validLink.isAuthorized()) {
link.use(); validLink.use();
await link.save(); await validLink.save();
await this.performAction(link, req, res); await this.performAction(validLink, req, res);
return; return;
} }
res.render('magic_link_lobby', { res.render('magic_link_lobby', {
email: link.getOrFail('email'), email: validLink.getOrFail('email'),
type: link.getOrFail('action_type'), type: validLink.getOrFail('action_type'),
validUntil: link.getExpirationDate().getTime(), validUntil: validLink.getExpirationDate().getTime(),
websocketUrl: config.get<string>('app.public_websocket_url') + this.magicLinkWebsocketPath, websocketUrl: config.get<string>('app.public_websocket_url') + this.magicLinkWebsocketPath,
}); });
} }