如何使用’expo-auth-session/providers/google’将signInWithCredential推送到Firebase。

huangapple go评论52阅读模式
英文:

How to push signInWithCredential to Firebase, using 'expo-auth-session/providers/google'

问题

以下是您要翻译的代码部分:

export const AuthProvider = ({children}) => {
    
    const [accessToken, setAccessToken] = React.useState();

    const [user, setUser] = React.useState(null);
    const [request, response, promptAsync] = google.useAuthRequest({

        androidClientId: '***',
        iosClientId: '***',
        clientId: '***',

        scopes: ["profile", "email"],
        permissions: ["public_profile", "email", "gender", "location"],

    });

    React.useEffect(() => {

        if (response?.type === "success") {

            const accessToken = response.authentication.accessToken;

            const idToken = response.authentication.accessToken;

            const credential = GoogleAuthProvider.credential(idToken, accessToken);

            console.log(accessToken);

            signInWithCredential(auth, credential);

        }
    }, [response]);
}

export const AuthProvider = ({children}) => {
    
    const [accessToken, setAccessToken] = React.useState();

    const [user, setUser] = React.useState(null);
    const [request, response, promptAsync] = google.useAuthRequest({

        androidClientId: '***',
        iosClientId: '***',
        clientId: '***',

        scopes: ["profile", "email"],
        permissions: ["public_profile", "email", "gender", "location"],

    });

    React.useEffect(() => {

        if (response?.type === "success") {

            const accessToken = response.authentication.accessToken;

            const idToken = response.authentication.accessToken;

            const credential = GoogleAuthProvider.credential(idToken, accessToken);

            console.log(accessToken);

            signInWithCredential(auth, credential);

        }
    }, [response]);
}

这是您要的翻译,仅包括代码部分,没有其他内容。

英文:

The google sign in works fine, got the accessToken, but I don't see the credentials pushed into Firebase. the following is the code.

export const AuthProvider = ({children}) => {
const [accessToken, setAccessToken] = React.useState();
const [user, setUser] = React.useState(null);
const [request, response, promptAsync] = google.useAuthRequest({
androidClientId: '***',
iosClientId: '***',
clientId : '***',
scopes: ["profile", "email"],
permissions: ["public_profile","email", "gender", "location"],
});
React.useEffect( ()=>{
if(response?.type === "success"){
const accessToken = response.authentication.accessToken;
const idToken = response.authentication.accessToken;
const credential = GoogleAuthProvider.credential(idToken,accessToken);
console.log(accessToken);
signInWithCredential(auth,credential);
}   
//return Promise.reject();
}, [response]);

here is the complete useAuth.js file


import { createContext, useContext } from "react";
import {View, Text, Image, TouchableOpacity , StyleSheet} from "react-native";
import * as Google from "expo-google-app-auth";
import * as React from 'react';
import * as google from 'expo-auth-session/providers/google';
import * as WebBrowser from 'expo-web-browser';
import {GoogleAuthProvider, onAuthStateChanged, signInWithCredential, signOut} from "@firebase/auth";
import {auth} from "../firebase";
WebBrowser.maybeCompleteAuthSession();
const AuthContext = createContext({});
export const AuthProvider = ({children}) => {
const [accessToken, setAccessToken] = React.useState();
const [user, setUser] = React.useState(null);
const [request, response, promptAsync] = google.useAuthRequest({
androidClientId: '***',
iosClientId: '***',
clientId : '***',
scopes: ["profile", "email"],
permissions: ["public_profile","email", "gender", "location"],
});
React.useEffect( ()=>{
if(response?.type === "success"){
const accessToken = response.authentication.accessToken;
const idToken = response.authentication.accessToken;
const credential = GoogleAuthProvider.credential(idToken,accessToken);
console.log(accessToken);
signInWithCredential(auth,credential);
}   
//return Promise.reject();
}, [response]);
return (
<AuthContext.Provider value={{
user : null,
// signInWithGoogle,
promptAsync,
}}> 
{children}</AuthContext.Provider>
); 
};
const styles = StyleSheet.create({
container : {
flex : 1, 
backgroundColor : '#fff',
alignItems : 'center',
justifyContent : 'center',
},
});
export default function useAuth() {
return useContext(AuthContext);
}

here is the loginScreen.js file,


import React from "react";
import {View, Text, Button } from "react-native";
import useAuth from "../hooks/useAuth";
const LoginScreen = () => {
const {signInWithGoogle} = useAuth();
const {showUserInfo} = useAuth();
const {promptAsync} = useAuth();
const {user} = useAuth();
console.log(user);
return(
<View>
<Text>
Login to the app
</Text> 
{/*  <Button title = "Login" onPress={signInWithGoogle}/>   
*/}  
<Button title = "Login" onPress={()=>{
promptAsync();
}}/>    
</View>
)
}
export default LoginScreen

答案1

得分: 2

我最终通过将凭据行中的"idToken"更改为"null"来使其工作,如下所示;

const credential = GoogleAuthProvider.credential(null, accessToken);

希望能帮助到有可能遇到相同问题的人。

英文:

I finally get it working by changing "idToken" to "null" of the the credential line, as the following;

const credential = GoogleAuthProvider.credential(null,accessToken);

hope it helps, if someone maybe facing the same problem.

huangapple
  • 本文由 发表于 2023年2月24日 01:52:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定