feat(auth): improve error handling and user feedback in Google sign-in flow
This commit is contained in:
parent
0f3ee5d537
commit
6745181a4b
@ -77,16 +77,23 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const provider = new GoogleAuthProvider();
|
||||
try {
|
||||
await signInWithPopup(auth, provider);
|
||||
} catch (err: unknown) {
|
||||
// Fallback to redirect when popups are blocked.
|
||||
const code = (err as { code?: string }).code || "";
|
||||
if (code === "auth/popup-blocked" || code === "auth/operation-not-supported-in-this-environment") {
|
||||
await import("firebase/auth").then(({ signInWithRedirect }) => signInWithRedirect(auth, provider));
|
||||
} else {
|
||||
const msg = err instanceof Error ? err.message : "Sign-in failed";
|
||||
setError(msg);
|
||||
throw err;
|
||||
} catch (err: any) {
|
||||
const code = err?.code ?? "";
|
||||
// Common cases where redirect works better
|
||||
const redirectable = [
|
||||
"auth/popup-blocked",
|
||||
"auth/operation-not-supported-in-this-environment",
|
||||
"auth/unauthorized-domain",
|
||||
];
|
||||
if (redirectable.includes(code)) {
|
||||
const { signInWithRedirect } = await import("firebase/auth");
|
||||
await signInWithRedirect(auth, provider);
|
||||
return; // navigation will happen
|
||||
}
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
setError(msg); // keep the message for the UI
|
||||
console.error("signin error:", err);
|
||||
throw err; // let the caller catch if needed
|
||||
}
|
||||
} finally {
|
||||
setSigningIn(false);
|
||||
|
||||
@ -11,10 +11,13 @@ export default function AuthStatus() {
|
||||
}
|
||||
if (!user) {
|
||||
return (
|
||||
<div>
|
||||
<button className="auth-btn" onClick={() => signInWithGoogle().catch(() => { })} disabled={signingIn}>
|
||||
<span className="auth-icon">G</span>
|
||||
<span>{signingIn ? "開いています…" : "Google でサインイン"}</span>
|
||||
</button>
|
||||
{error && <div className="auth-error" style={{ marginTop: 8 }}>{error}</div>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user