fix(back/auth): refactor auth success, fix message saying 'Welcome undefined' when there is no UserNameComponent

This commit is contained in:
Alice Gaudon 2022-03-07 17:02:03 +01:00
parent 8c083d562d
commit 27e9abc5f4
3 changed files with 15 additions and 4 deletions

View File

@ -12,6 +12,15 @@ import UserNameComponent from "./models/UserNameComponent.js";
import UserPasswordComponent from "./password/UserPasswordComponent.js"; import UserPasswordComponent from "./password/UserPasswordComponent.js";
export default class AuthController extends Controller { export default class AuthController extends Controller {
public static flashSuccessfulAuthenticationWelcomeMessage(
user: User,
req: Request,
messagePrefix: string,
): void {
const name = user.asOptional(UserNameComponent)?.getName();
req.flash('success', `${messagePrefix} Welcome${name ? `, ${name}` : ''}.`);
}
public getRoutesPrefix(): string { public getRoutesPrefix(): string {
return '/auth'; return '/auth';
} }

View File

@ -12,6 +12,7 @@ import Mail from "../../mail/Mail.js";
import MailTemplate from "../../mail/MailTemplate.js"; import MailTemplate from "../../mail/MailTemplate.js";
import Throttler from "../../Throttler.js"; import Throttler from "../../Throttler.js";
import AuthComponent, {AuthMiddleware} from "../AuthComponent.js"; import AuthComponent, {AuthMiddleware} from "../AuthComponent.js";
import AuthController from "../AuthController.js";
import {AuthError, PendingApprovalAuthError, RegisterCallback} from "../AuthGuard.js"; import {AuthError, PendingApprovalAuthError, RegisterCallback} from "../AuthGuard.js";
import MagicLink from "../models/MagicLink.js"; import MagicLink from "../models/MagicLink.js";
import MagicLinkUserNameComponent from "../models/MagicLinkUserNameComponent.js"; import MagicLinkUserNameComponent from "../models/MagicLinkUserNameComponent.js";
@ -198,8 +199,7 @@ export default class MagicLinkController<A extends Application> extends Controll
if (!res.headersSent && user) { if (!res.headersSent && user) {
// Auth success // Auth success
const name = user.asOptional(UserNameComponent)?.getName(); AuthController.flashSuccessfulAuthenticationWelcomeMessage(user, req, 'Authentication success.');
req.flash('success', `Authentication success. Welcome${name ? `, ${name}` : ''}`);
res.redirect(req.getIntendedUrl() || route('home')); res.redirect(req.getIntendedUrl() || route('home'));
} }
break; break;

View File

@ -8,6 +8,7 @@ import Validator, {InvalidFormatValidationError} from "../../db/Validator.js";
import {ServerError} from "../../HttpError.js"; import {ServerError} from "../../HttpError.js";
import Throttler from "../../Throttler.js"; import Throttler from "../../Throttler.js";
import AuthComponent from "../AuthComponent.js"; import AuthComponent from "../AuthComponent.js";
import AuthController from "../AuthController.js";
import {AuthError, PendingApprovalAuthError, RegisterCallback} from "../AuthGuard.js"; import {AuthError, PendingApprovalAuthError, RegisterCallback} from "../AuthGuard.js";
import AuthMethod from "../AuthMethod.js"; import AuthMethod from "../AuthMethod.js";
import User from "../models/User.js"; import User from "../models/User.js";
@ -89,7 +90,7 @@ export default class PasswordAuthMethod implements AuthMethod<PasswordAuthProof>
} }
} }
req.flash('success', `Welcome, ${user.name}.`); AuthController.flashSuccessfulAuthenticationWelcomeMessage(user, req, 'Authentication success.');
res.redirect(req.getIntendedUrl() || route('home')); res.redirect(req.getIntendedUrl() || route('home'));
} }
@ -136,8 +137,9 @@ export default class PasswordAuthMethod implements AuthMethod<PasswordAuthProof>
} }
const user = await passwordAuthProof.getResource(); const user = await passwordAuthProof.getResource();
if (!user) throw new Error('Password auth proof has no user.');
req.flash('success', `Your account was successfully created! Welcome, ${user?.as(UserNameComponent).getName()}.`); AuthController.flashSuccessfulAuthenticationWelcomeMessage(user, req, 'Your account was successfully created!');
res.redirect(req.getIntendedUrl() || route('home')); res.redirect(req.getIntendedUrl() || route('home'));
} }