summaryrefslogtreecommitdiff
path: root/lib/google-auth.ts
blob: 769da55716656d2b6c619babf99654cb94c82caf (plain)
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 { ResponseType } from "expo-auth-session";
import * as Google from "expo-auth-session/providers/google";
import Constants from "expo-constants";
import * as WebBrowser from "expo-web-browser";
import { useEffect, useMemo, useState } from "react";
import { useAuth } from "./auth";
import { t } from "./locales";

WebBrowser.maybeCompleteAuthSession();

type GoogleClientIds = {
  iosClientId?: string;
  androidClientId?: string;
  webClientId?: string;
};

function getGoogleClientIds(): GoogleClientIds {
  const env = (process as any)?.env || {};
  const extra =
    (Constants.manifest as any)?.extra ||
    (Constants.expoConfig as any)?.extra ||
    {};

  return {
    iosClientId:
      env.EXPO_PUBLIC_GOOGLE_IOS_CLIENT_ID || extra.GOOGLE_IOS_CLIENT_ID,
    androidClientId:
      env.EXPO_PUBLIC_GOOGLE_ANDROID_CLIENT_ID ||
      extra.GOOGLE_ANDROID_CLIENT_ID,
    webClientId:
      env.EXPO_PUBLIC_GOOGLE_WEB_CLIENT_ID ||
      extra.GOOGLE_WEB_CLIENT_ID ||
      env.EXPO_PUBLIC_GOOGLE_EXPO_CLIENT_ID ||
      extra.GOOGLE_EXPO_CLIENT_ID,
  };
}

export function useGoogleAuth() {
  const { signInWithGoogle } = useAuth();
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState("");

  const clientIds = useMemo(() => getGoogleClientIds(), []);

  const [request, response, promptAsync] = Google.useAuthRequest({
    iosClientId: clientIds.iosClientId,
    androidClientId: clientIds.androidClientId,
    webClientId: clientIds.webClientId,
    responseType: ResponseType.IdToken,
    scopes: ["openid", "profile", "email"],
    selectAccount: true,
  });

  useEffect(() => {
    const run = async () => {
      if (!response) return;

      if (response.type === "success") {
        const idToken =
          response.params?.id_token || response.authentication?.idToken;

        if (!idToken) {
          setError(t("googleMissingIdToken"));
          setIsLoading(false);
          return;
        }

        const result = await signInWithGoogle(idToken);
        if (!result.success) {
          setError(result.reason || t("googleSignInError"));
        }
        setIsLoading(false);
        return;
      }

      if (response.type === "error") {
        setError(response.error?.message || t("googleSignInError"));
      }

      // On cancel/dismissed/locked, just reset loading and keep the current screen state.
      setIsLoading(false);
    };

    run().catch(() => {
      setError(t("googleSignInError"));
      setIsLoading(false);
    });
  }, [response, signInWithGoogle]);

  const continueWithGoogle = async () => {
    setError("");

    if (!request) {
      setError(t("googleConfigMissing"));
      return;
    }

    setIsLoading(true);

    try {
      const result = await promptAsync();
      if (result.type === "cancel" || result.type === "dismiss") {
        setIsLoading(false);
      }
    } catch {
      setError(t("googleSignInError"));
      setIsLoading(false);
    }
  };

  return {
    continueWithGoogle,
    isLoading,
    error,
  };
}