-
Notifications
You must be signed in to change notification settings - Fork 6
/
useGoogleOneTap.ts
94 lines (82 loc) · 2.57 KB
/
useGoogleOneTap.ts
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
import { useState, useContext, useEffect } from "react"
import { GlobalContext } from "./pages/_app"
import { fb } from "./firebase"
declare var google: any
export const useGoogleOneTap = () => {
const [id_token, setId_token] = useState(null)
const [shouldShowFallbackButton, setShouldShowFallbackButton] = useState(
false
)
const { loggedInUser, authLoading } = useContext(GlobalContext)
useEffect(() => {
console.log("authLoading", authLoading)
}, [authLoading])
useEffect(() => {
if (!loggedInUser) {
const handleCredentialResponse = (response) => {
setId_token(response.credential)
}
const nativeCallback = (obj) => alert("native_callback!")
const client_id =
"812622916919-k75gcjkkqs04s57s5ks2f16q74qui6b2.apps.googleusercontent.com"
google.accounts.id.initialize({
client_id,
callback: handleCredentialResponse,
auto_select: true,
context: "use",
native_callback: nativeCallback,
prompt_parent_id: "put-google-one-tap-here-plz",
})
google.accounts.id.prompt((notification) => {
console.log("notification is: ", notification.getMomentType())
if (notification.isDisplayMoment()) {
console.log("IS DISPLAY MOMENT")
}
if (notification.isNotDisplayed()) {
console.warn(
"one-tap did not show because:",
notification.getNotDisplayedReason()
)
setShouldShowFallbackButton(true)
}
if (notification.isSkippedMoment()) {
console.warn(
"one-tap skipped because:",
notification.getSkippedReason()
)
setShouldShowFallbackButton(true)
}
if (notification.isDismissedMoment()) {
console.warn(
"one-tap dismissed because:",
notification.getDismissedReason()
)
if (notification.getDismissedReason() !== "credential_returned") {
setShouldShowFallbackButton(true)
}
}
})
} else {
google.accounts.id.cancel()
}
}, [loggedInUser])
useEffect(() => {
console.log(loggedInUser)
}, [loggedInUser])
useEffect(() => {
if (id_token) {
// Sign in with credential from the Google user.
fb.auth()
.signInWithCredential(fb.auth.GoogleAuthProvider.credential(id_token))
.catch(function (error) {
console.error("bruno says", error)
})
}
}, [id_token])
return {
shouldShowFallbackButton,
setShouldShowFallbackButton,
loggedInUser,
authLoading,
}
}