diff --git a/src/auth/AuthController.ts b/src/auth/AuthController.ts index 262e5ac..2535b5b 100644 --- a/src/auth/AuthController.ts +++ b/src/auth/AuthController.ts @@ -12,6 +12,15 @@ import UserNameComponent from "./models/UserNameComponent.js"; import UserPasswordComponent from "./password/UserPasswordComponent.js"; 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 { return '/auth'; } diff --git a/src/auth/magic_link/MagicLinkController.ts b/src/auth/magic_link/MagicLinkController.ts index 402662f..4a9a8c7 100644 --- a/src/auth/magic_link/MagicLinkController.ts +++ b/src/auth/magic_link/MagicLinkController.ts @@ -12,6 +12,7 @@ import Mail from "../../mail/Mail.js"; import MailTemplate from "../../mail/MailTemplate.js"; import Throttler from "../../Throttler.js"; import AuthComponent, {AuthMiddleware} from "../AuthComponent.js"; +import AuthController from "../AuthController.js"; import {AuthError, PendingApprovalAuthError, RegisterCallback} from "../AuthGuard.js"; import MagicLink from "../models/MagicLink.js"; import MagicLinkUserNameComponent from "../models/MagicLinkUserNameComponent.js"; @@ -198,8 +199,7 @@ export default class MagicLinkController extends Controll if (!res.headersSent && user) { // Auth success - const name = user.asOptional(UserNameComponent)?.getName(); - req.flash('success', `Authentication success. Welcome${name ? `, ${name}` : ''}`); + AuthController.flashSuccessfulAuthenticationWelcomeMessage(user, req, 'Authentication success.'); res.redirect(req.getIntendedUrl() || route('home')); } break; diff --git a/src/auth/password/PasswordAuthMethod.ts b/src/auth/password/PasswordAuthMethod.ts index 7029216..bd70d61 100644 --- a/src/auth/password/PasswordAuthMethod.ts +++ b/src/auth/password/PasswordAuthMethod.ts @@ -8,6 +8,7 @@ import Validator, {InvalidFormatValidationError} from "../../db/Validator.js"; import {ServerError} from "../../HttpError.js"; import Throttler from "../../Throttler.js"; import AuthComponent from "../AuthComponent.js"; +import AuthController from "../AuthController.js"; import {AuthError, PendingApprovalAuthError, RegisterCallback} from "../AuthGuard.js"; import AuthMethod from "../AuthMethod.js"; import User from "../models/User.js"; @@ -89,7 +90,7 @@ export default class PasswordAuthMethod implements AuthMethod } } - req.flash('success', `Welcome, ${user.name}.`); + AuthController.flashSuccessfulAuthenticationWelcomeMessage(user, req, 'Authentication success.'); res.redirect(req.getIntendedUrl() || route('home')); } @@ -136,8 +137,9 @@ export default class PasswordAuthMethod implements AuthMethod } 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')); }