1
- import React , { useState , useCallback } from 'react'
1
+ import React , { useState } from 'react'
2
2
import cc from 'classcat'
3
- import GoogleLogin from 'react-google-login'
4
- import { postGoogleLoginCallback } from '../../../api/auth/google'
5
- import { useRouter } from '../../../lib/router'
3
+ import { stringify } from 'querystring'
6
4
import { mdiGoogle } from '@mdi/js'
5
+ import { boostHubBaseUrl } from '../../../lib/consts'
7
6
import styled from '../../../../design/lib/styled'
8
7
import Icon from '../../../../design/components/atoms/Icon'
9
8
@@ -15,7 +14,6 @@ interface GoogleLoginButtonProps {
15
14
query ?: any
16
15
disabled : boolean
17
16
setDisabled : ( val : boolean ) => void
18
- setError : ( err : unknown ) => void
19
17
}
20
18
21
19
const GoogleLoginButton = ( {
@@ -24,91 +22,32 @@ const GoogleLoginButton = ({
24
22
query,
25
23
disabled,
26
24
setDisabled,
27
- setError,
28
25
} : GoogleLoginButtonProps ) => {
29
26
const [ sending , setSending ] = useState < boolean > ( false )
30
- const [ googleAuthIsInitialized , setGoogleAuthIsInitialized ] = useState <
31
- boolean
32
- > ( true )
33
- const { push } = useRouter ( )
34
-
35
- const responseGoogle = useCallback (
36
- async ( response : any ) => {
37
- setSending ( true )
38
- setDisabled ( true )
39
- setError ( undefined )
40
- try {
41
- const data = await postGoogleLoginCallback ( {
42
- ...query ,
43
- tokenId : response . tokenId ,
44
- accessToken : response . accessToken ,
45
- googleId : response . googleId ,
46
- scope : response . tokenObj . scope ,
47
- } )
48
- push ( data . redirectTo )
49
- } catch ( error ) {
50
- setError ( error )
51
- setDisabled ( false )
52
- setSending ( false )
53
- }
54
- } ,
55
- [ query , setError , push , setDisabled ]
56
- )
57
-
58
- const errorHandler = ( response : any ) => {
59
- console . log ( response . error )
60
- if ( response . error === 'popup_closed_by_user' ) {
61
- return
62
- }
63
-
64
- if ( response . error === 'idpiframe_initialization_failed' ) {
65
- setGoogleAuthIsInitialized ( false )
66
- setError (
67
- `Third-party cookies and site data need to be enabled for Google Auth to work properly. Please adjust your browser settings.`
68
- )
69
- return
70
- }
71
-
72
- setError ( response . error )
73
- }
74
-
75
- if ( process . env . GOOGLE_CLIENT_ID == null ) {
76
- return null
77
- }
27
+ const loginHref = `${ boostHubBaseUrl } /api/oauth/google${
28
+ query != null ? `?${ stringify ( query ) } ` : ''
29
+ } `
78
30
79
31
return (
80
- < GoogleLogin
81
- clientId = { process . env . GOOGLE_CLIENT_ID }
82
- render = { ( renderProps ) => (
83
- < StyledGoogleButton
84
- onClick = { renderProps . onClick }
85
- disabled = {
86
- renderProps . disabled ||
87
- sending ||
88
- disabled ||
89
- ! googleAuthIsInitialized
90
- }
91
- style = { style }
92
- className = { cc ( [ 'login-google-btn g-signin2' , className ] ) }
93
- >
94
- < Icon path = { mdiGoogle } />
95
- { sending ? (
96
- < span > Signing in...</ span >
97
- ) : (
98
- < span > Continue with Google</ span >
99
- ) }
100
- </ StyledGoogleButton >
101
- ) }
102
- onSuccess = { responseGoogle }
103
- onFailure = { errorHandler }
104
- cookiePolicy = { 'single_host_origin' }
105
- />
32
+ < StyledGoogleButton
33
+ onClick = { ( ) => {
34
+ setDisabled ( true )
35
+ setSending ( true )
36
+ } }
37
+ href = { loginHref }
38
+ disabled = { disabled }
39
+ style = { style }
40
+ className = { cc ( [ 'login-google-btn' , disabled && 'disabled' , className ] ) }
41
+ >
42
+ < Icon path = { mdiGoogle } />
43
+ { sending ? < span > Signing in...</ span > : < span > Continue with Google</ span > }
44
+ </ StyledGoogleButton >
106
45
)
107
46
}
108
47
109
48
export default GoogleLoginButton
110
49
111
- const StyledGoogleButton = styled . button `
50
+ const StyledGoogleButton = styled . a `
112
51
text-decoration: none;
113
52
width: 100%;
114
53
height: 40px;
0 commit comments