LizG
LizG

Reputation: 31

expo-auth-google to expo-auth-session

I'm trying to follow an old tutorial that uses the deprecated expo-auth-google sign in method. I know expo-auth-session is the newer version but I'm getting this error in my application when I try to use it:

ValidationError: "redirect_uri" is not allowed. "client_id" is not allowed. "response_type" is not allowed. "state" is not allowed. "scope" is not allowed

I have everything set up on GCP and Firebase. Here's my code, and the error I'm getting. Could you please help me?

PS: I have the keys in the code, I just didn't want to post them here.

The tutorial I followed: https://www.youtube.com/watch?v=qJaFIGjyRms[enter image description here](https://i.sstatic.net/4jCDb.png)


import React, {
    createContext,
    useContext,
    useMemo,
    useEffect,
    useState,
} from "react";
import * as Google from "expo-auth-session/providers/google";
import Constants from "expo-constants";
import {
    GoogleAuthProvider,
    onAuthStateChanged,
    signInWithCredential,
    signOut,
} from "firebase/auth";
import { auth } from "../firebase";

const AuthContext = createContext({});

export const AuthProvider = ({ children }) => {
    const [error, setError] = useState(null);
    const [user, setUser] = useState(null);
    const [loadingInitial, setLoadingInitial] = useState(true);
    const [loading, setLoading] = useState(false);

    const [_request, _loginResult, promptGoogle] = Google.useAuthRequest({
        androidClientId: '**',
        iosClientId: '**',
        expoClientId: '**',
        scopes: ["profile", "email", "openid"],
        responseType: "id_token",
    });

    const logout = () => {
        setLoading(true);
        signOut(auth)
            .catch((error) => setError(error))
            .finally(() => {
                setLoading(false);
            });
    };

    const signInWithGoogle = () => {
        setLoading(true);
        promptGoogle()
            .then((loginResult) => {
                if (loginResult?.type === "success") {
                    const idToken = loginResult.params.id_token;
                    const credential = GoogleAuthProvider.credential(idToken);
                    signInWithCredential(auth, credential).then(() => {
                        console.log("Logged in");
                    });
                } else {
                    throw new Error("Google login failed");
                }
            })
            .catch((error) => setError(error))
            .finally(() => setLoading(false));
    };

    useEffect(() => {
        const unsub = onAuthStateChanged(auth, (firebaseUser) => {
            if (firebaseUser) {
                setUser(firebaseUser);
            } else {
                setUser(null);
            }
            setLoadingInitial(false);
        });

        return unsub;
    }, []);

    const memoedValue = useMemo(
        () => ({
            user,
            loading,
            error,
            signInWithGoogle,
            logout,
        }),
        [user, loading, error]
    );

    return (
        <AuthContext.Provider value={memoedValue}>
            {!loadingInitial && children}
        </AuthContext.Provider>
    );
};

export default function useAuth() {
    return useContext(AuthContext);
}

I'm just trying to get Google sign-in working. I've tried following along https://www.npmjs.com/package/expo-auth-session and the turorials on Expo's documentation but I'm not seeming to get anywhere

Upvotes: 3

Views: 2841

Answers (1)

bonbonvoyage
bonbonvoyage

Reputation: 31

I have had the same issue, have managed to get it working using expo-dev-client.

I didn't find a way to fix this for Expo Go - not sure if there is one currently. It seems like using Oauth google login on IOS, currently requires building the app.

Once you configure the build, you can use

import { makeRedirectUri } from 'expo-auth-session';

to get the correct redirect URL.

const [request, response, promptAsync] = Google.useAuthRequest({
 clientId: 'xxxx',
 iosClientId:
        'xxxx',
 redirectUri: makeRedirectUri()});

Also remember to generate IOS credentials in the google console.

Upvotes: 1

Related Questions