This commit is contained in:
Stavros
2025-04-29 22:36:48 +03:00
parent f824b84787
commit fd96f39d3a
64 changed files with 3364 additions and 3518 deletions

View File

@@ -1,134 +1,120 @@
import { Button, Code, Paper, Text } from "@mantine/core";
import { notifications } from "@mantine/notifications";
import { Navigate } from "react-router";
import { useUserContext } from "../context/user-context";
import { Layout } from "../components/layouts/layout";
import { ReactNode } from "react";
import { escapeRegex, isQueryValid } from "../utils/utils";
import { useAppContext } from "../context/app-context";
import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { isValidUrl } from "@/lib/utils";
import { Trans, useTranslation } from "react-i18next";
import { Navigate, useNavigate } from "react-router";
export const ContinuePage = () => {
const queryString = window.location.search;
const params = new URLSearchParams(queryString);
const redirectUri = params.get("redirect_uri") ?? "";
const { isLoggedIn } = useUserContext();
const { disableContinue, domain } = useAppContext();
const { t } = useTranslation();
const navigate = useNavigate();
const params = new URLSearchParams(window.location.search);
if (!isLoggedIn) {
return <Navigate to={`/login?redirect_uri=${redirectUri}`} />;
}
const redirectURI = params.get("redirect_uri") ?? "";
if (!isQueryValid(redirectUri)) {
//psuedo
const domain = "127.0.0.1";
const disableContinue = false;
if (redirectURI === "") {
return <Navigate to="/" />;
}
const redirect = () => {
notifications.show({
title: t("continueRedirectingTitle"),
message: t("continueRedirectingSubtitle"),
color: "blue",
});
setTimeout(() => {
window.location.href = redirectUri;
}, 500);
};
let uri;
try {
uri = new URL(redirectUri);
} catch {
return (
<ContinuePageLayout>
<Text size="xl" fw={700}>
{t("Invalid redirect")}
</Text>
<Text>{t("The redirect URL is invalid")}</Text>
</ContinuePageLayout>
);
}
const regex = new RegExp(`^.*${escapeRegex(domain)}$`)
if (!regex.test(uri.hostname)) {
return (
<ContinuePageLayout>
<Text size="xl" fw={700}>
{t("untrustedRedirectTitle")}
</Text>
<Trans
i18nKey="untrustedRedirectSubtitle"
t={t}
components={{ Code: <Code /> }}
values={{ domain: domain }}
/>
<Button fullWidth mt="xl" color="red" onClick={redirect}>
{t('continueTitle')}
</Button>
<Button fullWidth mt="sm" color="gray" onClick={() => window.location.href = "/"}>
{t('cancelTitle')}
</Button>
</ContinuePageLayout>
)
if (!isValidUrl(redirectURI)) {
return <Navigate to="/" />;
}
if (disableContinue) {
window.location.href = redirectUri;
window.location.href = redirectURI;
}
const url = new URL(redirectURI);
if (!url.hostname.includes(domain)) {
return (
<ContinuePageLayout>
<Text size="xl" fw={700}>
{t("continueRedirectingTitle")}
</Text>
<Text>{t("continueRedirectingSubtitle")}</Text>
</ContinuePageLayout>
<Card className="min-w-xs md:max-w-sm">
<CardHeader>
<CardTitle className="text-3xl">
{t("untrustedRedirectTitle")}
</CardTitle>
<CardDescription>
<Trans
i18nKey="untrustedRedirectSubtitle"
t={t}
components={{
code: (
<code className="relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-mono text-sm font-semibold" />
),
}}
values={{ domain }}
/>
</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-2 items-stretch">
<Button
onClick={() => window.location.replace(redirectURI)}
variant="destructive"
>
{t("continueTitle")}
</Button>
<Button onClick={() => navigate("/")} variant="outline">
{t("cancelTitle")}
</Button>
</CardContent>
</Card>
);
}
if (window.location.protocol === "https:" && uri.protocol === "http:") {
if (url.protocol === "http:" && window.location.protocol === "https:") {
return (
<ContinuePageLayout>
<Text size="xl" fw={700}>
{t("continueInsecureRedirectTitle")}
</Text>
<Text>
<Trans
i18nKey="continueInsecureRedirectSubtitle"
t={t}
components={{ Code: <Code /> }}
/>
</Text>
<Button fullWidth mt="xl" color="yellow" onClick={redirect}>
<Card className="min-w-xs md:max-w-sm">
<CardHeader>
<CardTitle className="text-3xl">
{t("continueInsecureRedirectTitle")}
</CardTitle>
<CardDescription>
<Trans
i18nKey="continueInsecureRedirectSubtitle"
t={t}
components={{
code: (
<code className="relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-mono text-sm font-semibold" />
),
}}
/>
</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-2 items-stretch">
<Button
onClick={() => window.location.replace(redirectURI)}
variant="warning"
>
{t("continueTitle")}
</Button>
<Button onClick={() => navigate("/")} variant="outline">
{t("cancelTitle")}
</Button>
</CardContent>
</Card>
);
}
return (
<Card className="min-w-xs md:max-w-sm">
<CardHeader>
<CardTitle className="text-3xl">{t("continueTitle")}</CardTitle>
<CardDescription>{t("continueSubtitle")}</CardDescription>
</CardHeader>
<CardContent className="flex flex-col items-stretch">
<Button onClick={() => window.location.replace(redirectURI)}>
{t("continueTitle")}
</Button>
<Button fullWidth mt="sm" color="gray" onClick={() => window.location.href = "/"}>
{t('cancelTitle')}
</Button>
</ContinuePageLayout>
);
}
return (
<ContinuePageLayout>
<Text size="xl" fw={700}>
{t("continueTitle")}
</Text>
<Text>{t("continueSubtitle")}</Text>
<Button fullWidth mt="xl" onClick={redirect}>
{t("continueTitle")}
</Button>
</ContinuePageLayout>
);
};
export const ContinuePageLayout = ({ children }: { children: ReactNode }) => {
return (
<Layout>
<Paper shadow="md" p={30} mt={30} radius="md" withBorder>
{children}
</Paper>
</Layout>
</CardContent>
</Card>
);
};

View File

@@ -0,0 +1,20 @@
import {
Card,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { useTranslation } from "react-i18next";
export const ErrorPage = () => {
const { t } = useTranslation();
return (
<Card className="min-w-xs md:max-w-sm">
<CardHeader>
<CardTitle className="text-3xl">{t("errorTitle")}</CardTitle>
<CardDescription>{t("errorSubtitle")}</CardDescription>
</CardHeader>
</Card>
);
};

View File

@@ -1,25 +0,0 @@
import { Paper, Text, TypographyStylesProvider } from "@mantine/core";
import { Layout } from "../components/layouts/layout";
import { useTranslation } from "react-i18next";
import { useAppContext } from "../context/app-context";
import Markdown from 'react-markdown'
export const ForgotPasswordPage = () => {
const { t } = useTranslation();
const { forgotPasswordMessage } = useAppContext();
return (
<Layout>
<Paper shadow="md" p={30} mt={30} radius="md" withBorder>
<Text size="xl" fw={700}>
{t("forgotPasswordTitle")}
</Text>
<TypographyStylesProvider>
<Markdown>
{forgotPasswordMessage}
</Markdown>
</TypographyStylesProvider>
</Paper>
</Layout>
);
};

View File

@@ -1,20 +0,0 @@
import { Button, Paper, Text } from "@mantine/core";
import { Layout } from "../components/layouts/layout";
import { useTranslation } from "react-i18next";
export const InternalServerError = () => {
const { t } = useTranslation();
return (
<Layout>
<Paper shadow="md" p={30} mt={30} radius="md" withBorder>
<Text size="xl" fw={700}>
{t("internalErrorTitle")}
</Text>
<Text>{t("internalErrorSubtitle")}</Text>
<Button fullWidth mt="xl" onClick={() => window.location.replace("/")}>
{t("internalErrorButton")}
</Button>
</Paper>
</Layout>
);
};

View File

@@ -1,138 +1,100 @@
import { Paper, Title, Text, Divider } from "@mantine/core";
import { notifications } from "@mantine/notifications";
import { useMutation } from "@tanstack/react-query";
import axios, { type AxiosError } from "axios";
import { useUserContext } from "../context/user-context";
import { Navigate } from "react-router";
import { Layout } from "../components/layouts/layout";
import { OAuthButtons } from "../components/auth/oauth-buttons";
import { LoginFormValues } from "../schemas/login-schema";
import { LoginForm } from "../components/auth/login-forn";
import { isQueryValid } from "../utils/utils";
import { useAppContext } from "../context/app-context";
import { OAuthButton } from "@/components/auth/oauth-button";
import { GenericIcon } from "@/components/icons/generic";
import { GithubIcon } from "@/components/icons/github";
import { GoogleIcon } from "@/components/icons/google";
import { Button } from "@/components/ui/button";
import {
Card,
CardHeader,
CardTitle,
CardDescription,
CardContent,
} from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Separator } from "@/components/ui/separator";
import { useTranslation } from "react-i18next";
export const LoginPage = () => {
const queryString = window.location.search;
const params = new URLSearchParams(queryString);
const redirectUri = params.get("redirect_uri") ?? "";
const { isLoggedIn } = useUserContext();
const { configuredProviders, title, genericName } = useAppContext();
const { t } = useTranslation();
const configuredProviders = ["google", "github", "generic", "username"];
const title = "Tinyauth";
const oauthProviders = configuredProviders.filter(
(value) => value !== "username",
);
if (isLoggedIn) {
return <Navigate to="/logout" />;
}
const loginMutation = useMutation({
mutationFn: (login: LoginFormValues) => {
return axios.post("/api/login", login);
},
onError: (data: AxiosError) => {
if (data.response) {
if (data.response.status === 429) {
notifications.show({
title: t("loginFailTitle"),
message: t("loginFailRateLimit"),
color: "red",
});
return;
}
}
notifications.show({
title: t("loginFailTitle"),
message: t("loginFailSubtitle"),
color: "red",
});
},
onSuccess: async (data) => {
if (data.data.totpPending) {
window.location.replace(`/totp?redirect_uri=${redirectUri}`);
return;
}
notifications.show({
title: t("loginSuccessTitle"),
message: t("loginSuccessSubtitle"),
color: "green",
});
setTimeout(() => {
if (!isQueryValid(redirectUri)) {
window.location.replace("/");
return;
}
window.location.replace(`/continue?redirect_uri=${redirectUri}`);
}, 500);
},
});
const loginOAuthMutation = useMutation({
mutationFn: (provider: string) => {
return axios.get(
`/api/oauth/url/${provider}?redirect_uri=${redirectUri}`,
);
},
onError: () => {
notifications.show({
title: t("loginOauthFailTitle"),
message: t("loginOauthFailSubtitle"),
color: "red",
});
},
onSuccess: (data) => {
notifications.show({
title: t("loginOauthSuccessTitle"),
message: t("loginOauthSuccessSubtitle"),
color: "blue",
});
setTimeout(() => {
window.location.href = data.data.url;
}, 500);
},
});
const handleSubmit = (values: LoginFormValues) => {
loginMutation.mutate(values);
};
const oauthConfigured =
configuredProviders.filter((provider) => provider !== "username").length >
0;
const userAuthConfigured = configuredProviders.includes("username");
return (
<Layout>
<Title ta="center">{title}</Title>
<Paper shadow="md" p="xl" mt={30} radius="md" withBorder>
{oauthProviders.length > 0 && (
<>
<Text size="lg" fw={500} ta="center">
{t("loginTitle")}
</Text>
<OAuthButtons
oauthProviders={oauthProviders}
isPending={loginOAuthMutation.isPending}
mutate={loginOAuthMutation.mutate}
genericName={genericName}
/>
{configuredProviders.includes("username") && (
<Divider
label={t("loginDivider")}
labelPosition="center"
my="lg"
/>
<Card className="max-w-xs md:max-w-sm">
<CardHeader>
<CardTitle className="text-center text-3xl">{title}</CardTitle>
{configuredProviders.length > 0 && (
<CardDescription className="text-center">
{oauthConfigured ? t("loginTitle") : t("loginTitleSimple")}
</CardDescription>
)}
</CardHeader>
<CardContent className="flex flex-col gap-4">
{oauthConfigured && (
<div className="flex flex-row gap-3 flex-wrap items-center justify-center">
{configuredProviders.includes("google") && (
<OAuthButton title="Google" icon={<GoogleIcon />} />
)}
</>
{configuredProviders.includes("github") && (
<OAuthButton title="Github" icon={<GithubIcon />} />
)}
{configuredProviders.includes("generic") && (
<OAuthButton title="Generic" icon={<GenericIcon />} />
)}
</div>
)}
{configuredProviders.includes("username") && (
<LoginForm
isPending={loginMutation.isPending}
onSubmit={handleSubmit}
/>
{userAuthConfigured && oauthConfigured && (
<div className="flex items-center gap-4">
<Separator className="flex-1" />
<span className="text-sm text-muted-foreground">
{t("loginDivider")}
</span>
<Separator className="flex-1" />
</div>
)}
</Paper>
</Layout>
{userAuthConfigured && (
<div className="flex flex-col gap-4">
<div>
<Label htmlFor="#username">{t("loginUsername")}</Label>
<Input
id="username"
placeholder={t("loginUsername")}
className="mt-2"
/>
</div>
<div>
<Label htmlFor="#password">
<div className="flex flex-row min-w-full items-center justify-between">
<span>{t("loginPassword")}</span>
<a
href="/forgot"
className="text-muted-foreground font-normal"
>
{t("forgotPasswordTitle")}
</a>
</div>
</Label>
<Input
id="password"
placeholder={t("loginPassword")}
className="mt-2"
/>
</div>
<Button>{t("loginSubmit")}</Button>
</div>
)}
{configuredProviders.length == 0 && (
<h3 className="text-center text-xl text-red-600">
{t("failedToFetchProvidersTitle")}
</h3>
)}
</CardContent>
</Card>
);
};

View File

@@ -1,84 +0,0 @@
import { Button, Code, Paper, Text } from "@mantine/core";
import { notifications } from "@mantine/notifications";
import { useMutation } from "@tanstack/react-query";
import axios from "axios";
import { useUserContext } from "../context/user-context";
import { Navigate } from "react-router";
import { Layout } from "../components/layouts/layout";
import { capitalize } from "../utils/utils";
import { useAppContext } from "../context/app-context";
import { Trans, useTranslation } from "react-i18next";
export const LogoutPage = () => {
const { isLoggedIn, username, oauth, provider } = useUserContext();
const { genericName } = useAppContext();
const { t } = useTranslation();
if (!isLoggedIn) {
return <Navigate to="/login" />;
}
const logoutMutation = useMutation({
mutationFn: () => {
return axios.post("/api/logout");
},
onError: () => {
notifications.show({
title: t("logoutFailTitle"),
message: t("logoutFailSubtitle"),
color: "red",
});
},
onSuccess: () => {
notifications.show({
title: t("logoutSuccessTitle"),
message: t("logoutSuccessSubtitle"),
color: "green",
});
setTimeout(() => {
window.location.replace("/login");
}, 500);
},
});
return (
<Layout>
<Paper shadow="md" p={30} mt={30} radius="md" withBorder>
<Text size="xl" fw={700}>
{t("logoutTitle")}
</Text>
<Text>
{oauth ? (
<Trans
i18nKey="logoutOauthSubtitle"
t={t}
components={{ Code: <Code /> }}
values={{
provider:
provider === "generic" ? genericName : capitalize(provider),
username: username,
}}
/>
) : (
<Trans
i18nKey="logoutUsernameSubtitle"
t={t}
components={{ Code: <Code /> }}
values={{
username: username,
}}
/>
)}
</Text>
<Button
fullWidth
mt="xl"
onClick={() => logoutMutation.mutate()}
loading={logoutMutation.isPending}
>
{t("logoutTitle")}
</Button>
</Paper>
</Layout>
);
};

View File

@@ -1,20 +1,27 @@
import { Button, Paper, Text } from "@mantine/core";
import { Layout } from "../components/layouts/layout";
import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router";
export const NotFoundPage = () => {
const { t } = useTranslation();
const navigate = useNavigate();
return (
<Layout>
<Paper shadow="md" p={30} mt={30} radius="md" withBorder>
<Text size="xl" fw={700}>
{t("notFoundTitle")}
</Text>
<Text>{t("notFoundSubtitle")}</Text>
<Button fullWidth mt="xl" onClick={() => window.location.replace("/")}>
{t("notFoundButton")}
</Button>
</Paper>
</Layout>
<Card className="min-w-xs md:max-w-sm">
<CardHeader>
<CardTitle className="text-3xl">{t("notFoundTitle")}</CardTitle>
<CardDescription>{t("notFoundSubtitle")}</CardDescription>
</CardHeader>
<CardContent className="flex flex-col items-stretch">
<Button onClick={() => navigate("/")}>{t("notFoundButton")}</Button>
</CardContent>
</Card>
);
};

View File

@@ -1,66 +1,44 @@
import { Navigate } from "react-router";
import { useUserContext } from "../context/user-context";
import { Title, Paper, Text } from "@mantine/core";
import { Layout } from "../components/layouts/layout";
import { TotpForm } from "../components/auth/totp-form";
import { useMutation } from "@tanstack/react-query";
import axios from "axios";
import { notifications } from "@mantine/notifications";
import { useAppContext } from "../context/app-context";
import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import {
InputOTP,
InputOTPGroup,
InputOTPSeparator,
InputOTPSlot,
} from "@/components/ui/input-otp";
import { useTranslation } from "react-i18next";
export const TotpPage = () => {
const queryString = window.location.search;
const params = new URLSearchParams(queryString);
const redirectUri = params.get("redirect_uri") ?? "";
const { totpPending, isLoggedIn } = useUserContext();
const { title } = useAppContext();
const { t } = useTranslation();
if (isLoggedIn) {
return <Navigate to={`/logout`} />;
}
if (!totpPending) {
return <Navigate to={`/login?redirect_uri=${redirectUri}`} />;
}
const totpMutation = useMutation({
mutationFn: async (totp: { code: string }) => {
await axios.post("/api/totp", totp);
},
onError: () => {
notifications.show({
title: t("totpFailTitle"),
message: t("totpFailSubtitle"),
color: "red",
});
},
onSuccess: () => {
notifications.show({
title: t("totpSuccessTitle"),
message: t("totpSuccessSubtitle"),
color: "green",
});
setTimeout(() => {
window.location.replace(`/continue?redirect_uri=${redirectUri}`);
}, 500);
},
});
return (
<Layout>
<Title ta="center">{title}</Title>
<Paper shadow="md" p="xl" mt={30} radius="md" withBorder>
<Text size="lg" fw={500} mb="md" ta="center">
{t("totpTitle")}
</Text>
<TotpForm
isPending={totpMutation.isPending}
onSubmit={(values) => totpMutation.mutate(values)}
/>
</Paper>
</Layout>
<Card className="min-w-xs md:max-w-sm">
<CardHeader>
<CardTitle className="text-3xl">{t("totpTitle")}</CardTitle>
<CardDescription>{t("totpSubtitle")}</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-6 items-stretch">
<InputOTP maxLength={6}>
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
</InputOTPGroup>
<InputOTPSeparator />
<InputOTPGroup>
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>
<Button>{t("continueTitle")}</Button>
</CardContent>
</Card>
);
};

View File

@@ -1,56 +0,0 @@
import { Button, Code, Paper, Text } from "@mantine/core";
import { Layout } from "../components/layouts/layout";
import { Navigate } from "react-router";
import { isQueryValid } from "../utils/utils";
import { Trans, useTranslation } from "react-i18next";
export const UnauthorizedPage = () => {
const queryString = window.location.search;
const params = new URLSearchParams(queryString);
const username = params.get("username") ?? "";
const resource = params.get("resource") ?? "";
const { t } = useTranslation();
if (!isQueryValid(username)) {
return <Navigate to="/" />;
}
return (
<Layout>
<Paper shadow="md" p={30} mt={30} radius="md" withBorder>
<Text size="xl" fw={700}>
{t("Unauthorized")}
</Text>
<Text>
{isQueryValid(resource) ? (
<Text>
<Trans
i18nKey="unauthorizedResourceSubtitle"
t={t}
components={{ Code: <Code /> }}
values={{ resource, username }}
/>
</Text>
) : (
<Text>
<Trans
i18nKey="unaothorizedLoginSubtitle"
t={t}
components={{ Code: <Code /> }}
values={{ username }}
/>
</Text>
)}
</Text>
<Button
fullWidth
mt="xl"
onClick={() => window.location.replace("/login")}
>
{t("unauthorizedButton")}
</Button>
</Paper>
</Layout>
);
};