From 16e80e63169c187979716558b5303cb5f3df77dc Mon Sep 17 00:00:00 2001 From: rharkor Date: Sun, 10 Dec 2023 21:33:33 +0100 Subject: [PATCH] feat: store last locale used --- .../migration.sql | 1 + .../migration.sql | 0 packages/app/prisma/schema.prisma | 1 + packages/app/src/api/auth/mutations.ts | 3 ++ packages/app/src/api/me/email/mutation.ts | 1 + .../app/src/app/[lang]/(protected)/layout.tsx | 35 ++++++++++++++++++- .../(sys-auth)/sign-up/credentials/page.tsx | 2 +- .../app/[lang]/(sys-auth)/sign-up/page.tsx | 2 +- .../auth/register-user-auth-form.tsx | 4 ++- packages/app/src/lib/schemas/auth.ts | 1 + 10 files changed, 46 insertions(+), 4 deletions(-) rename packages/app/prisma/migrations/{20231120113500_init => 20231210183722_init}/migration.sql (99%) rename packages/app/prisma/migrations/{20231120113500_sp => 20231210183723_sp}/migration.sql (100%) diff --git a/packages/app/prisma/migrations/20231120113500_init/migration.sql b/packages/app/prisma/migrations/20231210183722_init/migration.sql similarity index 99% rename from packages/app/prisma/migrations/20231120113500_init/migration.sql rename to packages/app/prisma/migrations/20231210183722_init/migration.sql index e70ca034..6de2c75b 100644 --- a/packages/app/prisma/migrations/20231120113500_init/migration.sql +++ b/packages/app/prisma/migrations/20231210183722_init/migration.sql @@ -27,6 +27,7 @@ CREATE TABLE "User" ( "role" TEXT NOT NULL DEFAULT 'user', "password" TEXT, "hasPassword" BOOLEAN NOT NULL DEFAULT false, + "lastLocale" TEXT, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP(3) NOT NULL, diff --git a/packages/app/prisma/migrations/20231120113500_sp/migration.sql b/packages/app/prisma/migrations/20231210183723_sp/migration.sql similarity index 100% rename from packages/app/prisma/migrations/20231120113500_sp/migration.sql rename to packages/app/prisma/migrations/20231210183723_sp/migration.sql diff --git a/packages/app/prisma/schema.prisma b/packages/app/prisma/schema.prisma index fb6d47b4..6233542a 100644 --- a/packages/app/prisma/schema.prisma +++ b/packages/app/prisma/schema.prisma @@ -45,6 +45,7 @@ model User { hasPassword Boolean @default(false) resetPasswordToken ResetPassordToken? userEmailVerificationToken UserEmailVerificationToken? + lastLocale String? // Timestamps createdAt DateTime @default(now()) diff --git a/packages/app/src/api/auth/mutations.ts b/packages/app/src/api/auth/mutations.ts index 9cb15ff2..cc70dbaf 100644 --- a/packages/app/src/api/auth/mutations.ts +++ b/packages/app/src/api/auth/mutations.ts @@ -5,6 +5,7 @@ import { hash } from "@/lib/bcrypt" import { logger } from "@/lib/logger" import { sendMail } from "@/lib/mailer" import { prisma } from "@/lib/prisma" +import { redis } from "@/lib/redis" import { signUpSchema } from "@/lib/schemas/auth" import { html, plainText, subject } from "@/lib/templates/mail/verify-email" import { ApiError, handleApiError, throwableErrorsMessages } from "@/lib/utils/server-utils" @@ -25,8 +26,10 @@ export const register = async ({ input }: apiInputFromSchema { + const lastLocale = await redis.get(`lastLocale:${session.user.id}`) + if (lastLocale) { + return lastLocale + } + const lastLocaleFromDb = await prisma.user.findUnique({ + where: { id: session.user.id }, + select: { lastLocale: true }, + }) + if (lastLocaleFromDb && lastLocaleFromDb.lastLocale) { + await redis.set(`lastLocale:${session.user.id}`, lastLocaleFromDb.lastLocale) + return lastLocaleFromDb.lastLocale + } + return null + } + // Set last locale in redis and db + const setLastLocale = async (locale: Locale) => { + await redis.set(`lastLocale:${session.user.id}`, locale) + await prisma.user.update({ + where: { id: session.user.id }, + data: { lastLocale: locale }, + }) + } + + const lastLocale = await getLastLocale() + if (lastLocale !== lang) { + await setLastLocale(lang) + } return ( <> diff --git a/packages/app/src/app/[lang]/(sys-auth)/sign-up/credentials/page.tsx b/packages/app/src/app/[lang]/(sys-auth)/sign-up/credentials/page.tsx index ee807fae..0828f35f 100644 --- a/packages/app/src/app/[lang]/(sys-auth)/sign-up/credentials/page.tsx +++ b/packages/app/src/app/[lang]/(sys-auth)/sign-up/credentials/page.tsx @@ -37,7 +37,7 @@ export default async function SignupByCredentials({ {dictionary.signUpPage.createAnAccount} - + diff --git a/packages/app/src/app/[lang]/(sys-auth)/sign-up/page.tsx b/packages/app/src/app/[lang]/(sys-auth)/sign-up/page.tsx index e25b4abf..e0eca5e3 100644 --- a/packages/app/src/app/[lang]/(sys-auth)/sign-up/page.tsx +++ b/packages/app/src/app/[lang]/(sys-auth)/sign-up/page.tsx @@ -39,7 +39,7 @@ export default async function SignUpPage({

{dictionary.signUpPage.enterEmail}

- +
diff --git a/packages/app/src/components/auth/register-user-auth-form.tsx b/packages/app/src/components/auth/register-user-auth-form.tsx index 8d647905..fbe40fff 100644 --- a/packages/app/src/components/auth/register-user-auth-form.tsx +++ b/packages/app/src/components/auth/register-user-auth-form.tsx @@ -23,6 +23,7 @@ type UserAuthFormProps = React.HTMLAttributes & { dictionary: TDictionary isMinimized?: boolean searchParams?: { [key: string]: string | string[] | undefined } + locale: string } export const formSchema = (dictionary: TDictionary) => @@ -52,7 +53,7 @@ export const getFormSchema = ({ dictionary, isMinimized }: { dictionary: TDictio export type IForm = z.infer> export type IFormMinimized = z.infer> -export function RegisterUserAuthForm({ dictionary, isMinimized, searchParams, ...props }: UserAuthFormProps) { +export function RegisterUserAuthForm({ dictionary, isMinimized, searchParams, locale, ...props }: UserAuthFormProps) { const router = useRouter() const registerMutation = trpc.auth.register.useMutation({ @@ -102,6 +103,7 @@ export function RegisterUserAuthForm({ dictionary, isMinimized, searchParams, .. username: "", password: "", confirmPassword: "", + locale, }, }) diff --git a/packages/app/src/lib/schemas/auth.ts b/packages/app/src/lib/schemas/auth.ts index dfed1de3..69209173 100644 --- a/packages/app/src/lib/schemas/auth.ts +++ b/packages/app/src/lib/schemas/auth.ts @@ -43,6 +43,7 @@ export const signUpSchema = (dictionary?: TDictionary) => signInSchema(dictionary).extend({ username: usernameSchema(dictionary), password: passwordSchemaWithRegex(dictionary), + locale: z.string(), }) export const signUpResponseSchema = (dictionary?: TDictionary) =>