Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Sep 1, 2023
1 parent eca431e commit 77dd1bf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
17 changes: 7 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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"
Expand All @@ -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(
Expand Down
1 change: 0 additions & 1 deletion src/pkg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/pkg/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -11,6 +13,7 @@ import { NextFunction, Request, Response } from "express"
* or returns false.
*
* @internal
* @param req
* @param res
* @param apiBaseUrl
*/
Expand Down Expand Up @@ -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()
}

0 comments on commit 77dd1bf

Please sign in to comment.