MCPcopy Create free account
hub / github.com/dailydotdev/apps / useGoogleOneTap

Function useGoogleOneTap

packages/shared/src/hooks/auth/useGoogleOneTap.ts:121–194  ·  view source on GitHub ↗
({
  enabled,
  clientId = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
  onCredential,
}: UseGoogleOneTapProps)

Source from the content-addressed store, hash-verified

119};
120
121export const useGoogleOneTap = ({
122 enabled,
123 clientId = process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
124 onCredential,
125}: UseGoogleOneTapProps): void => {
126 const { logEvent } = useLogContext();
127 const onCredentialRef = useRef(onCredential);
128 onCredentialRef.current = onCredential;
129 const logEventRef = useRef(logEvent);
130 logEventRef.current = logEvent;
131 // Prompt at most once per mount (one pop-up session). Once it was closed,
132 // e.g. because the visitor moved on to email, going back to the provider
133 // buttons doesn't bring it back or log another prompt.
134 const hasPromptedRef = useRef(false);
135
136 useEffect(() => {
137 if (!enabled || !clientId || hasPromptedRef.current) {
138 return undefined;
139 }
140
141 let cancelled = false;
142 const handleCredential = (credential: string) => {
143 onCredentialRef.current(credential);
144 };
145
146 const init = async () => {
147 try {
148 await loadGsiScript();
149 } catch (error) {
150 // The GSI script is commonly blocked by ad blockers, privacy browsers
151 // and some networks. One Tap is optional, so this is expected and is
152 // logged as its own event instead of a `global error`.
153 logEventRef.current({
154 event_name: LogEvent.GoogleOneTapLoadError,
155 extra: JSON.stringify({
156 msg: error instanceof Error ? error.message : 'unknown',
157 }),
158 });
159 return;
160 }
161
162 const googleId = getGoogleId();
163 if (cancelled || !googleId) {
164 return;
165 }
166
167 initializeOnce(googleId, clientId);
168 activeCredentialHandler = handleCredential;
169 hasPromptedRef.current = true;
170 googleId.prompt((notification) => {
171 if (!notification) {
172 return;
173 }
174 logEventRef.current({
175 event_name: LogEvent.GoogleOneTapPrompt,
176 extra: JSON.stringify({
177 momentType: notification.getMomentType?.(),
178 reason: getMomentReason(notification),

Callers 2

AuthOptionsInnerFunction · 0.90

Calls 3

useLogContextFunction · 0.90
initFunction · 0.85
getGoogleIdFunction · 0.85

Tested by

no test coverage detected