forked from brunocrosier/google-one-tap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseGoogleOneTap.ts
99 lines (88 loc) · 2.7 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
95
96
97
98
99
import { useContext, useEffect, useState } from "react"
// import { fb } from "./firebase"
import { GlobalContext } from "./pages/_app"
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) => {
console.log(response)
setId_token(response.credential)
}
const nativeCallback = (obj) => {
console.log("hi!")
console.log(obj)
}
const client_id = ""
google.accounts.id.initialize({
client_id,
// callback: handleCredentialResponse,
auto_select: false,
context: "use",
nonce: "his!",
native_callback: nativeCallback,
ux_mode: "popup",
login_uri: "http://localhost:8080",
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,
}
}