-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: login with google now use new SDK and flow #1108
Changes from 3 commits
2f863ec
7dd2b33
44450a5
242cb1f
b2f83b4
43c5ae4
54f022b
906ad7e
c8d6333
51f8fb8
029025f
27d70e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useContext } from 'react'; | ||
import GoogleContext from 'contexts/GoogleContext'; | ||
import { useIsLoggedIn } from 'hooks/auth'; | ||
|
||
const GoogleLoginButton = ({ onSuccess }) => { | ||
const googleAuth = useContext(GoogleContext); | ||
const ref = useRef(null); | ||
|
||
// use ref in case onSuccess is mutable | ||
const onSuccessRef = useRef(onSuccess); | ||
const isLoggedIn = useIsLoggedIn(); | ||
|
||
useEffect(() => { | ||
if (googleAuth && ref.current) { | ||
googleAuth.renderButton(ref.current, {}); | ||
} | ||
}, [googleAuth]); | ||
|
||
useEffect(() => { | ||
if (isLoggedIn) { | ||
onSuccessRef.current(); | ||
} | ||
}, [isLoggedIn]); | ||
|
||
return <div ref={ref}>Google 登入</div>; | ||
}; | ||
|
||
GoogleLoginButton.propTypes = { | ||
onSuccess: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default GoogleLoginButton; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import GoogleContext from 'contexts/GoogleContext'; | ||
import { GOOGLE_APP_ID } from '../../../config'; | ||
import { loginWithGoogle } from '../../../actions/auth'; | ||
|
||
const GoogleContextProvider = ({ children }) => { | ||
// initialize google sdk | ||
// when login status change, google will call callback | ||
const dispatch = useDispatch(); | ||
const [googleAuth, setGoogleAuth] = useState(null); | ||
|
||
useEffect(() => { | ||
if (!window || window.gapi) return; | ||
window.initGoogle = () => { | ||
const gapi = window.gapi; | ||
gapi.load('auth2', () => { | ||
gapi.auth2.init({ client_id: GOOGLE_APP_ID }); | ||
const googleAuth = gapi.auth2.getAuthInstance(); | ||
setGoogleAuth(googleAuth); | ||
window.onload = function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 需要檢查原本的 onload 嗎? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 需要...我想想怎麼改,或有建議怎麼做嗎? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 初步直覺想是這樣 let prevOnload = window.onload;
window.onload = function() {
if (prevOnload) prevOnload();
// ...
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 有 addEventListener 可以用,這樣就不用猜 on 的意思了 |
||
window.google.accounts.id.initialize({ | ||
client_id: GOOGLE_APP_ID, | ||
callback: async response => { | ||
await dispatch(loginWithGoogle(response)); | ||
}, | ||
}); | ||
|
||
setGoogleAuth(window.google.accounts.id); | ||
}; | ||
const script = document.createElement('script'); | ||
script.src = 'https://apis.google.com/js/platform.js?onload=initGoogle'; | ||
document.body.appendChild(script); | ||
}, []); | ||
}, [dispatch]); | ||
|
||
return ( | ||
<GoogleContext.Provider value={googleAuth}> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useFacebookLogin, useGoogleLogin } from 'hooks/login'; | ||
import { useFacebookLogin } from 'hooks/login'; | ||
import Modal from 'common/Modal.js'; | ||
import GoogleLoginButton from 'common/Auth/GoogleLoginButton'; | ||
import authStatus from '../../../constants/authStatus'; | ||
import styles from './LoginModal.module.css'; | ||
|
||
const LoginModal = ({ isOpen, close }) => { | ||
const fbLogin = useFacebookLogin(); | ||
const googleLogin = useGoogleLogin(); | ||
|
||
return ( | ||
<Modal isOpen={isOpen} hasColose close={close} closableOnClickOutside> | ||
|
@@ -24,16 +24,13 @@ const LoginModal = ({ isOpen, close }) => { | |
> | ||
Facebook 登入 | ||
</button> | ||
<button | ||
className={styles['btn-google']} | ||
onClick={async () => { | ||
if (await googleLogin()) { | ||
<div className={styles['btn-google']}> | ||
<GoogleLoginButton | ||
onSuccess={() => { | ||
close(); | ||
} | ||
}} | ||
> | ||
Google 登入 | ||
</button> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 發現有現成的 button 可用 |
||
}} | ||
/> | ||
</div> | ||
<p className={styles['login-tips']}> | ||
為了避免使用者大量輸入假資訊,我們會以你的帳戶做驗證。但別擔心!您的帳戶資訊不會以任何形式被揭露、顯示。 | ||
</p> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,8 @@ import PropTypes from 'prop-types'; | |
import cn from 'classnames'; | ||
import { Link } from 'react-router-dom'; | ||
import { useIsLoggedIn } from 'hooks/auth'; | ||
import { useFacebookLogin, useGoogleLogin } from 'hooks/login'; | ||
import { useFacebookLogin } from 'hooks/login'; | ||
import GoogleLoginButton from 'common/Auth/GoogleLoginButton'; | ||
import styles from './PermissionBlock.module.css'; | ||
|
||
const AuthenticatedButton = ({ to, onClick, children }) => ( | ||
|
@@ -24,7 +25,6 @@ AuthenticatedButton.propTypes = { | |
|
||
const UnauthenticatedButton = () => { | ||
const fbLogin = useFacebookLogin(); | ||
const googleLogin = useGoogleLogin(); | ||
|
||
return ( | ||
<div className={styles.loginBtnContainer}> | ||
|
@@ -38,11 +38,8 @@ const UnauthenticatedButton = () => { | |
</button> | ||
<button | ||
className={`${cn('buttonCircleM')} ${styles.btn} ${styles.btnGoogle}`} | ||
onClick={async () => { | ||
await googleLogin(); | ||
}} | ||
> | ||
<pre>Google 登入</pre> | ||
<GoogleLoginButton onSuccess={() => {}} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可以讓 onSuccess optional 或是 default to |
||
</button> | ||
</div> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import useFacebookLogin from './useFacebookLogin'; | ||
import useGoogleLogin from './useGoogleLogin'; | ||
import useLogin from './useLogin'; | ||
import useLogout from './useLogout'; | ||
|
||
export { useFacebookLogin, useGoogleLogin, useLogin, useLogout }; | ||
export { useFacebookLogin, useLogin, useLogout }; |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要沿用之前的 autStatus了嗎
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要處理,列為之後 TODO 來解