|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useOidc } from "@axa-fr/react-oidc"; |
| 4 | +import Button from "@components/Button"; |
| 5 | +import Paragraph from "@components/Paragraph"; |
| 6 | +import loadConfig from "@utils/config"; |
| 7 | +import { ArrowRightIcon, RefreshCw } from "lucide-react"; |
| 8 | +import { useRouter, useSearchParams } from "next/navigation"; |
| 9 | +import { useEffect, useState } from "react"; |
| 10 | +import NetBirdIcon from "@/assets/icons/NetBirdIcon"; |
| 11 | + |
| 12 | +const config = loadConfig(); |
| 13 | + |
| 14 | +export default function ErrorPage() { |
| 15 | + const { logout, isAuthenticated } = useOidc(); |
| 16 | + const router = useRouter(); |
| 17 | + const searchParams = useSearchParams(); |
| 18 | + const [error, setError] = useState<{ |
| 19 | + code: number; |
| 20 | + message: string; |
| 21 | + type: string; |
| 22 | + } | null>(null); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + // Get error details from URL params |
| 26 | + const code = searchParams.get("code"); |
| 27 | + const message = searchParams.get("message"); |
| 28 | + const type = searchParams.get("type"); |
| 29 | + |
| 30 | + if (code && message) { |
| 31 | + setError({ |
| 32 | + code: parseInt(code), |
| 33 | + message: decodeURIComponent(message), |
| 34 | + type: type || "error", |
| 35 | + }); |
| 36 | + } |
| 37 | + }, [searchParams]); |
| 38 | + |
| 39 | + const handleLogout = () => { |
| 40 | + // Use the same logout pattern as OIDCError |
| 41 | + logout("/", { client_id: config.clientId }); |
| 42 | + }; |
| 43 | + |
| 44 | + const handleRetry = () => { |
| 45 | + router.push("/"); |
| 46 | + }; |
| 47 | + |
| 48 | + if (!isAuthenticated) { |
| 49 | + // If not authenticated, redirect to home |
| 50 | + router.push("/"); |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + const isBlockedUser = |
| 55 | + error?.code === 403 && error?.message?.toLowerCase().includes("blocked"); |
| 56 | + const isPendingApproval = |
| 57 | + error?.code === 403 && |
| 58 | + error?.message?.toLowerCase().includes("pending approval"); |
| 59 | + |
| 60 | + const getTitle = () => { |
| 61 | + if (isBlockedUser) return "User Account Blocked"; |
| 62 | + if (isPendingApproval) return "User Approval Pending"; |
| 63 | + return "Access Error"; |
| 64 | + }; |
| 65 | + |
| 66 | + const getDescription = () => { |
| 67 | + if (isBlockedUser) { |
| 68 | + return "Your access has been blocked by the NetBird account administrator, possibly due to new user approval requirements or security policies. Please contact your administrator to regain access."; |
| 69 | + } |
| 70 | + if (isPendingApproval) { |
| 71 | + return "Your account is pending approval from an administrator. Please wait for approval before accessing the dashboard."; |
| 72 | + } |
| 73 | + return "An error occurred while trying to access the dashboard. Please try again or contact your administrator."; |
| 74 | + }; |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className="flex items-center justify-center flex-col h-screen max-w-xl mx-auto"> |
| 78 | + <div className="bg-nb-gray-930 mb-3 border border-nb-gray-900 h-12 w-12 rounded-md flex items-center justify-center"> |
| 79 | + <NetBirdIcon size={23} /> |
| 80 | + </div> |
| 81 | + |
| 82 | + <h1 className="text-center mt-2">{getTitle()}</h1> |
| 83 | + |
| 84 | + <Paragraph className="text-center mt-2 block"> |
| 85 | + {getDescription()} |
| 86 | + </Paragraph> |
| 87 | + |
| 88 | + {error && ( |
| 89 | + <div className="bg-nb-gray-930 border border-nb-gray-800 rounded-md p-4 mt-4 max-w-md font-mono mb-2"> |
| 90 | + <div className="text-center text-sm text-netbird"> |
| 91 | + <div>response_message: {error.message}</div> |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + )} |
| 95 | + |
| 96 | + <Paragraph className="text-center mt-2 text-sm"> |
| 97 | + If you believe this is an error, please contact your administrator. |
| 98 | + </Paragraph> |
| 99 | + |
| 100 | + <div className="mt-5 space-y-3"> |
| 101 | + {!isBlockedUser && !isPendingApproval && ( |
| 102 | + <Button variant="default-outline" size="sm" onClick={handleRetry}> |
| 103 | + <RefreshCw size={16} className="mr-2" /> |
| 104 | + Try Again |
| 105 | + </Button> |
| 106 | + )} |
| 107 | + |
| 108 | + <Button variant="primary" size="sm" onClick={handleLogout}> |
| 109 | + {isBlockedUser || isPendingApproval ? "Sign Out" : "Logout"} |
| 110 | + <ArrowRightIcon size={16} /> |
| 111 | + </Button> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + ); |
| 115 | +} |
0 commit comments