I18n auth (#23)

* New translation keys in en-US locale

* New translation keys in de-DE locale

* New translation keys in fr-FR locale

* New translation keys in it-IT locale

* New translation keys in pl-PL locale

* New translation keys in pt-PT locale

* New translation keys in tr-TR locale

* Add translation keys in app/auth

* Fix build

---------

Co-authored-by: Lokowitz <marvinlokowitz@gmail.com>
This commit is contained in:
vlalx
2025-05-17 19:11:56 +03:00
committed by GitHub
parent d2d84be99a
commit b8ed5ac1c5
23 changed files with 727 additions and 115 deletions

View File

@@ -13,6 +13,7 @@ import { AuthWithAccessTokenResponse } from "@server/routers/resource";
import { AxiosResponse } from "axios";
import Link from "next/link";
import { useEffect, useState } from "react";
import { useTranslations } from "next-intl";
type AccessTokenProps = {
token: string;
@@ -29,6 +30,8 @@ export default function AccessToken({
const { env } = useEnvContext();
const api = createApiClient({ env });
const t = useTranslations();
function appendRequestToken(url: string, token: string) {
const fullUrl = new URL(url);
fullUrl.searchParams.append(
@@ -76,7 +79,7 @@ export default function AccessToken({
);
}
} catch (e) {
console.error("Error checking access token", e);
console.error(t('accessTokenError'), e);
} finally {
setLoading(false);
}
@@ -115,9 +118,9 @@ export default function AccessToken({
function renderTitle() {
if (isValid) {
return "Access Granted";
return t('accessGranted');
} else {
return "Access URL Invalid";
return t('accessUrlInvalid');
}
}
@@ -125,18 +128,16 @@ export default function AccessToken({
if (isValid) {
return (
<div>
You have been granted access to this resource. Redirecting
you...
{t('accessGrantedDescription')}
</div>
);
} else {
return (
<div>
This shared access URL is invalid. Please contact the
resource owner for a new URL.
{t('accessUrlInvalidDescription')}
<div className="text-center mt-4">
<Button>
<Link href="/">Go Home</Link>
<Link href="/">{t('goHome')}</Link>
</Button>
</div>
</div>

View File

@@ -9,21 +9,23 @@ import {
CardTitle,
} from "@app/components/ui/card";
import Link from "next/link";
import { useTranslations } from "next-intl";
export default function ResourceAccessDenied() {
const t = useTranslations();
return (
<Card className="w-full max-w-md">
<CardHeader>
<CardTitle className="text-center text-2xl font-bold">
Access Denied
{t('accessDenied')}
</CardTitle>
</CardHeader>
<CardContent>
You're not allowed to access this resource. If this is a mistake,
please contact the administrator.
{t('accessDeniedDescription')}
<div className="text-center mt-4">
<Button>
<Link href="/">Go Home</Link>
<Link href="/">{t('goHome')}</Link>
</Button>
</div>
</CardContent>

View File

@@ -44,6 +44,7 @@ import { useEnvContext } from "@app/hooks/useEnvContext";
import { toast } from "@app/hooks/useToast";
import Link from "next/link";
import { useSupporterStatusContext } from "@app/hooks/useSupporterStatusContext";
import { useTranslations } from "next-intl";
const pinSchema = z.object({
pin: z
@@ -170,6 +171,8 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
return fullUrl.toString();
}
const t = useTranslations();
const onWhitelistSubmit = (values: any) => {
setLoadingLogin(true);
api.post<AxiosResponse<AuthWithWhitelistResponse>>(
@@ -183,8 +186,8 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
setOtpState("otp_sent");
submitOtpForm.setValue("email", values.email);
toast({
title: "OTP Sent",
description: "An OTP has been sent to your email"
title: t('otpEmailSent'),
description: t('otpEmailSentDescription')
});
return;
}
@@ -200,7 +203,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
.catch((e) => {
console.error(e);
setWhitelistError(
formatAxiosError(e, "Failed to authenticate with email")
formatAxiosError(e, t('otpEmailErrorAuthenticate'))
);
})
.then(() => setLoadingLogin(false));
@@ -225,7 +228,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
.catch((e) => {
console.error(e);
setPincodeError(
formatAxiosError(e, "Failed to authenticate with pincode")
formatAxiosError(e, t('pincodeErrorAuthenticate'))
);
})
.then(() => setLoadingLogin(false));
@@ -253,7 +256,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
.catch((e) => {
console.error(e);
setPasswordError(
formatAxiosError(e, "Failed to authenticate with password")
formatAxiosError(e, t('passwordErrorAuthenticate'))
);
})
.finally(() => setLoadingLogin(false));
@@ -280,7 +283,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
<div>
<div className="text-center mb-2">
<span className="text-sm text-muted-foreground">
Powered by{" "}
{t('poweredBy')}{" "}
<Link
href="https://github.com/fosrl/pangolin"
target="_blank"
@@ -293,11 +296,11 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
</div>
<Card>
<CardHeader>
<CardTitle>Authentication Required</CardTitle>
<CardTitle>{t('authenticationRequired')}</CardTitle>
<CardDescription>
{numMethods > 1
? `Choose your preferred method to access ${props.resource.name}`
: `You must authenticate to access ${props.resource.name}`}
? t('authenticationMethodChoose', {name: props.resource.name})
: t('authenticationRequest', {name: props.resource.name})}
</CardDescription>
</CardHeader>
<CardContent>
@@ -327,19 +330,19 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
{props.methods.password && (
<TabsTrigger value="password">
<Key className="w-4 h-4 mr-1" />{" "}
Password
{t('password')}
</TabsTrigger>
)}
{props.methods.sso && (
<TabsTrigger value="sso">
<User className="w-4 h-4 mr-1" />{" "}
User
{t('user')}
</TabsTrigger>
)}
{props.methods.whitelist && (
<TabsTrigger value="whitelist">
<AtSign className="w-4 h-4 mr-1" />{" "}
Email
{t('email')}
</TabsTrigger>
)}
</TabsList>
@@ -362,7 +365,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
render={({ field }) => (
<FormItem>
<FormLabel>
6-digit PIN Code
{t('pincodeInput')}
</FormLabel>
<FormControl>
<div className="flex justify-center">
@@ -431,7 +434,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
disabled={loadingLogin}
>
<LockIcon className="w-4 h-4 mr-2" />
Log in with PIN
{t('pincodeSubmit')}
</Button>
</form>
</Form>
@@ -457,7 +460,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
render={({ field }) => (
<FormItem>
<FormLabel>
Password
{t('password')}
</FormLabel>
<FormControl>
<Input
@@ -485,7 +488,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
disabled={loadingLogin}
>
<LockIcon className="w-4 h-4 mr-2" />
Log In with Password
{t('passwordSubmit')}
</Button>
</form>
</Form>
@@ -526,7 +529,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
render={({ field }) => (
<FormItem>
<FormLabel>
Email
{t('email')}
</FormLabel>
<FormControl>
<Input
@@ -535,10 +538,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
/>
</FormControl>
<FormDescription>
A one-time
code will be
sent to this
email.
{t('otpEmailDescription')}
</FormDescription>
<FormMessage />
</FormItem>
@@ -560,7 +560,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
disabled={loadingLogin}
>
<Send className="w-4 h-4 mr-2" />
Send One-time Code
{t('otpEmailSend')}
</Button>
</form>
</Form>
@@ -582,9 +582,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
render={({ field }) => (
<FormItem>
<FormLabel>
One-Time
Password
(OTP)
{t('otpEmail')}
</FormLabel>
<FormControl>
<Input
@@ -612,7 +610,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
disabled={loadingLogin}
>
<LockIcon className="w-4 h-4 mr-2" />
Submit OTP
{t('otpEmailSubmit')}
</Button>
<Button
@@ -624,7 +622,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
submitOtpForm.reset();
}}
>
Back to Email
{t('backToEmail')}
</Button>
</form>
</Form>
@@ -637,9 +635,7 @@ export default function ResourceAuthPortal(props: ResourceAuthPortalProps) {
{supporterStatus?.visible && (
<div className="text-center mt-2">
<span className="text-sm text-muted-foreground opacity-50">
Server is running without a supporter key.
<br />
Consider supporting the project!
{t('noSupportKey')}
</span>
</div>
)}

View File

@@ -7,20 +7,23 @@ import {
CardTitle,
} from "@app/components/ui/card";
import Link from "next/link";
import { useTranslations } from "next-intl";
export default async function ResourceNotFound() {
const t = useTranslations();
return (
<Card className="w-full max-w-md">
<CardHeader>
<CardTitle className="text-center text-2xl font-bold">
Resource Not Found
{t('resourceNotFound')}
</CardTitle>
</CardHeader>
<CardContent>
The resource you're trying to access does not exist.
{t('resourceNotFoundDescription')}
<div className="text-center mt-4">
<Button>
<Link href="/">Go Home</Link>
<Link href="/">{t('goHome')}</Link>
</Button>
</div>
</CardContent>