react-native-google-signin / google-signin

Google Sign-in for your React Native applications
https://react-native-google-signin.github.io/
MIT License
3.12k stars 876 forks source link

Can't verify google firebase token on laravel backend with google client #1204

Closed Gabotron-ES closed 7 months ago

Gabotron-ES commented 9 months ago

in my react native app I want to let users sign in with google, for this I'm using firebase. In react native side I get user token id and then send it to the backend where i use google client api to verify token id but I never get to validate it as it returns false.

On react native side:

import auth from '@react-native-firebase/auth';
import { GoogleSignin } from '@react-native-google-signin/google-signin';

useEffect(() => {

        GoogleSignin.configure({
            webClientId: 'XXX---.apps.googleusercontent.com',
        });

       }, []);

const onGoogleButtonPress = async () =>
    {

    try
    {

        //Get the users ID token
        const { idToken } = await GoogleSignin.signIn();
        console.log('idToken', idToken);
        // Create a Google credential with the token
        const googleCredential = auth.GoogleAuthProvider.credential(idToken);
        console.log('googleCredential', googleCredential);
        // Sign-in the user with the credential
        let signWithCredential = await auth().signInWithCredential(googleCredential);

        let iddd = await auth().currentUser.getIdToken();
        console.log('iddd',iddd);

        let url = env.BASE_URL+'/api/auth/sign-in-with-google';
        let payload = { token_id : iddd };
        let response = await http.post(url, payload);

    }
    catch (error)
    {

        console.log(error);
        throw error;
    }

    };

On my PHP backend side (laravel).

public function signInWithGoogle(Request $request)
    {

        $token_id = $request->input('token_id');

        $client =  new Google_Client(['client_id' => 'XXX---.apps.googleusercontent.com']);

        try {
            $payload = $client->verifyIdToken($token_id);
            //$payload returns false and I get Invalid token message, no error message or catched error...
            if (!$payload) {
                return response()->json(['error' => 'Invalid token', 'payload' => $payload], 401);
            }

            $userid = $payload['sub'];
            $email = $payload['email'];
            $name = $payload['name'];

            // Your logic here...

            return response()->json(['message' => 'Success']); // or return your desired data
        } catch (Exception $e) {
            return response()->json(['error' => 'Invalid token: ' . $e->getMessage()], 401);
        }

        return response()->json([
            'results' => $results,
        ]);
    }

PROBLEM: payload always returns false for some reason

vonovak commented 7 months ago

Hello and thanks for asking, this bug tracker is reserved for bugs, while this seems like a usage question. With that, I'm going to close this. Thank you 🙂