From 77dd1bfe61a037f51422052479bfdd9ff6dd9cae Mon Sep 17 00:00:00 2001 From: Patrik Date: Fri, 1 Sep 2023 13:17:14 +0200 Subject: [PATCH] chore: cleanup --- src/index.ts | 17 +++++++---------- src/pkg/index.ts | 1 - src/pkg/middleware.ts | 23 +++++++++++++++++++++++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/index.ts b/src/index.ts index 13211059..18a5c366 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,11 @@ // Copyright © 2022 Ory Corp // SPDX-License-Identifier: Apache-2.0 -import { addFavicon, defaultConfig, handlebarsHelpers } from "./pkg" +import { + addFavicon, + defaultConfig, + detectLanguage, + handlebarsHelpers, +} from "./pkg" import { middleware as middlewareLogger } from "./pkg/logger" import { register404Route, @@ -18,8 +23,6 @@ import { registerVerificationRoute, registerWelcomeRoute, } from "./routes" -import { locales } from "@ory/elements-markup" -import { pick as pickLanguage } from "accept-language-parser" import cookieParser from "cookie-parser" import express, { Request, Response } from "express" import { engine } from "express-handlebars" @@ -34,13 +37,7 @@ const router = express.Router() app.use(middlewareLogger) app.use(cookieParser()) app.use(addFavicon(defaultConfig)) -app.use((req, res, next) => { - res.locals.lang = pickLanguage( - Object.keys(locales), - req.header("Accept-Language") || "en", - ) - next() -}) +app.use(detectLanguage) app.set("view engine", "hbs") app.engine( diff --git a/src/pkg/index.ts b/src/pkg/index.ts index 75f88338..491c6761 100644 --- a/src/pkg/index.ts +++ b/src/pkg/index.ts @@ -55,7 +55,6 @@ const isErrorAuthenticatorAssuranceLevel = ( export const redirectOnSoftError = (res: Response, next: NextFunction, redirectTo: string) => (err: AxiosError) => { - console.log("redirectOnSoftError", err) if (!err.response) { next(err) return diff --git a/src/pkg/middleware.ts b/src/pkg/middleware.ts index ea162a77..a4fdd259 100644 --- a/src/pkg/middleware.ts +++ b/src/pkg/middleware.ts @@ -3,6 +3,8 @@ import { getUrlForFlow, isUUID } from "./index" import { RouteOptionsCreator } from "./route" import { Session } from "@ory/client" +import { locales } from "@ory/elements-markup" +import { pick as pickLanguage } from "accept-language-parser" import { AxiosError } from "axios" import { NextFunction, Request, Response } from "express" @@ -11,6 +13,7 @@ import { NextFunction, Request, Response } from "express" * or returns false. * * @internal + * @param req * @param res * @param apiBaseUrl */ @@ -125,3 +128,23 @@ export const requireNoAuth = next() }) } + +/** + * Detects the language of the user and sets it in the response locals. + * + * @param req + * @param res + * @param next + */ + +export const detectLanguage = ( + req: Request, + res: Response, + next: NextFunction, +) => { + res.locals.lang = pickLanguage( + Object.keys(locales), + req.header("Accept-Language") || "en", + ) + next() +}