All files / apps/web/src/frontoffice/pages GoogleCallback.jsx

0% Statements 0/42
0% Branches 0/19
0% Functions 0/6
0% Lines 0/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                                                                                                                                                                                                                                       
import React, { useEffect, useState } from "react";
import { useNavigate, useSearchParams } from "react-router-dom";
import api from "../../services/api";
import toast from "react-hot-toast";
 
// Décode le payload d'un JWT sans vérifier la signature (usage client uniquement)
function decodeTokenPayload(token) {
  try {
    const payload = token.split(".")[1];
    return JSON.parse(atob(payload.replace(/-/g, "+").replace(/_/g, "/")));
  } catch {
    return null;
  }
}
 
const GoogleCallback = () => {
  const [error, setError] = useState("");
  const [loading, setLoading] = useState(true);
  const navigate = useNavigate();
  const [searchParams] = useSearchParams();
 
  useEffect(() => {
    const run = async () => {
      const token = searchParams.get("token");
      const userId = searchParams.get("userId");
      const errorParam = searchParams.get("error");
 
      if (errorParam) {
        setError("Authentication denied or cancelled");
        setLoading(false);
        setTimeout(() => navigate("/login"), 3000);
        return;
      }
 
      if (token && userId) {
        localStorage.setItem("accessToken", token);
        localStorage.setItem("userId", userId);
 
      const pendingPlanCode = searchParams.get("plan");
      const payload = decodeTokenPayload(token);
      const orgId = payload?.organizationId;
 
      if (pendingPlanCode && orgId && pendingPlanCode !== "free") {
        try {
          const res = await api.post(
            `/organizations/${orgId}/subscriptions`,
            { planCode: pendingPlanCode },
          );
            if (res.data.data?.url) {
              window.location.href = res.data.data.url;
              return;
            }
          } catch (err) {
            console.error("Pending plan checkout failed:", err);
            toast.error(
              err.response?.data?.message || "Erreur lors de la souscription",
            );
          }
        }
        navigate("/");
        return;
      }
 
      setError("No authorization code received");
      setLoading(false);
      setTimeout(() => navigate("/login"), 3000);
    };
    run();
  }, [searchParams, navigate]);
 
  return (
    <div className="min-h-screen flex items-center justify-center bg-gray-50">
      <div className="max-w-md w-full space-y-8 text-center">
        {loading ? (
          <>
            <div className="animate-spin mx-auto h-12 w-12 border-4 border-indigo-500 border-t-transparent rounded-full"></div>
            <h2 className="mt-6 text-2xl font-bold text-gray-900">
              Authentication in progress...
            </h2>
            <p className="text-gray-600">
              Please wait while we complete the sign-in process.
            </p>
          </>
        ) : error ? (
          <>
            <div className="mx-auto h-12 w-12 text-red-500">
              <svg
                className="h-12 w-12"
                fill="none"
                viewBox="0 0 24 24"
                stroke="currentColor"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={2}
                  d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
                />
              </svg>
            </div>
            <h2 className="mt-6 text-2xl font-bold text-gray-900">
              Authentication Failed
            </h2>
            <p className="text-red-600">{error}</p>
            <p className="text-gray-500 text-sm mt-4">
              Redirecting to login...
            </p>
          </>
        ) : null}
      </div>
    </div>
  );
};
 
export default GoogleCallback;