
ReactNative Google SingIn อันนี้ต่อเนื่องจาก ReactNative Facebook login ด้วย firebase ใครยังไม่ได้ สร้าง project firebase และ โหลดไฟล์ google-services.json (สำหรับ android) และ GoogleService-Info.plist (สำหรับ ios) ไปทำตามอันเดิมก่อนนะครับ
1.set keystore’s SHA1 hash ที่ firebase
1 2 |
//terminal keytool -exportcert -keystore ~/.android/debug.keystore -list -v |
2. install lib react-native-google-signin
1 |
npm install react-native-google-signin --save |
1 |
react-native link react-native-google-signin |
=> ฝั่ง android
อย่าลืมเปลี่ยน compile เป็น implementation นะครับ (android/app/build.gradle)
=> ฝั่ง ios
(https://developers.google.com/identity/sign-in/ios/start-integrating)
GoogleService-Info.plist จะมีค่า REVERSED_CLIENT_ID ให้ copy url เพื่อเอาไปใส่ ใน xcode ตามภาพด้านล่างครับ
หลังจาก link จะมีการเพิ่มที่ pod ไฟล์ เข้าไป
pod ‘RNGoogleSignin’, :path => ‘../node_modules/react-native-google-signin’
อย่าลืมสั่ง pod update (เข้าไป cd ios =>สั่ง pod update)
3. เพิ่ม googleLogin method เข้าไป ส่วน ui ดูที่ LoginView
(https://rnfirebase.io/docs/v5.x.x/auth/social-auth)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
googleLogin = async () => { try { // Add any configuration settings here: await GoogleSignin.configure(); const data = await GoogleSignin.signIn(); // create a new firebase credential with the token const credential = firebase.auth.GoogleAuthProvider.credential(data.idToken, data.accessToken) // login with credential // const currentUser = await firebase.auth().signInAndRetrieveDataWithCredential(credential); const currentUser = await firebase.auth().signInWithCredential(credential); // console.info(JSON.stringify(currentUser.user.toJSON())); //Todo: add user data console.log('====googleLogin===='); this.addUser(currentUser); } catch (e) { // console.error(e); } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
addUser(currentUser) { // console.log('=====currentUser'); let userData = { uid : currentUser.user.uid, displayName : currentUser.user.displayName, email : currentUser.user.email, photoUrl : currentUser.user.photoURL, providerId: currentUser.user.providerData[0].providerId, userType: this.props.userType, } console.log(userData); } |
4. ผลลัพธ์ที่ได้ครับ

ส่วนใครจะทำ login facebook อ่านที่บทความ ReactNative Facebook login ด้วย firebase
จบครับ ผิดพลาดขออภัยไว้ ณ ที่นี้ด้วยนะครับ