mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-10-30 21:55:43 +00:00 
			
		
		
		
	 84d4c84ed2
			
		
	
	84d4c84ed2
	
	
	
		
			
			* feat: allow or block an ip/range of ips using labels * refactor: redirect to root page when no username or ip is provided in the unauthorized page
		
			
				
	
	
		
			76 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			76 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { Button } from "@/components/ui/button";
 | |
| import {
 | |
|   Card,
 | |
|   CardDescription,
 | |
|   CardFooter,
 | |
|   CardHeader,
 | |
|   CardTitle,
 | |
| } from "@/components/ui/card";
 | |
| import { useState } from "react";
 | |
| import { Trans, useTranslation } from "react-i18next";
 | |
| import { Navigate, useLocation, useNavigate } from "react-router";
 | |
| 
 | |
| export const UnauthorizedPage = () => {
 | |
|   const { search } = useLocation();
 | |
| 
 | |
|   const searchParams = new URLSearchParams(search);
 | |
|   const username = searchParams.get("username");
 | |
|   const resource = searchParams.get("resource");
 | |
|   const groupErr = searchParams.get("groupErr");
 | |
|   const ip = searchParams.get("ip");
 | |
| 
 | |
|   if (!username && !ip) {
 | |
|     return <Navigate to="/" />;
 | |
|   }
 | |
| 
 | |
|   const { t } = useTranslation();
 | |
|   const navigate = useNavigate();
 | |
|   const [loading, setLoading] = useState(false);
 | |
| 
 | |
|   const handleRedirect = () => {
 | |
|     setLoading(true);
 | |
|     navigate("/login");
 | |
|   };
 | |
| 
 | |
|   let i18nKey = "unauthorizedLoginSubtitle";
 | |
| 
 | |
|   if (resource) {
 | |
|     i18nKey = "unauthorizedResourceSubtitle";
 | |
|   }
 | |
| 
 | |
|   if (groupErr === "true") {
 | |
|     i18nKey = "unauthorizedGroupsSubtitle";
 | |
|   }
 | |
| 
 | |
|   if (ip) {
 | |
|     i18nKey = "unauthorizedIpSubtitle";
 | |
|   }
 | |
| 
 | |
|   return (
 | |
|     <Card className="min-w-xs sm:min-w-sm">
 | |
|       <CardHeader>
 | |
|         <CardTitle className="text-3xl">{t("unauthorizedTitle")}</CardTitle>
 | |
|         <CardDescription>
 | |
|           <Trans
 | |
|             i18nKey={i18nKey}
 | |
|             t={t}
 | |
|             components={{
 | |
|               code: <code />,
 | |
|             }}
 | |
|             values={{
 | |
|               username,
 | |
|               resource,
 | |
|               ip,
 | |
|             }}
 | |
|           />
 | |
|         </CardDescription>
 | |
|       </CardHeader>
 | |
|       <CardFooter className="flex flex-col items-stretch">
 | |
|         <Button onClick={handleRedirect} loading={loading}>
 | |
|           {t("unauthorizedButton")}
 | |
|         </Button>
 | |
|       </CardFooter>
 | |
|     </Card>
 | |
|   );
 | |
| };
 |