1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Can't configure google_sign_in without firebase

Discussão em 'Mobile' iniciado por Stack, Outubro 4, 2024 às 01:32.

  1. Stack

    Stack Membro Participativo

    I install google_sign_in and create successfully my keytool sha key , i configure my google console acccount and create credentials (i enable also People API).

    I pass my generate keytool file into android/app folder and put that code in build.grandle :

    signingConfigs{
    debug{
    keyAlias 'xxxxx'
    storePassword 'xxxxx'
    storeFile file('xxxxx.jks')
    keyPassword 'xxxxx'
    }
    }

    buildTypes {
    release {
    signingConfig signingConfigs.debug
    }
    debug{
    signingConfig signingConfigs.debug
    }
    }


    Also i put that on manifest :

    <activity android:name="com.google.android.gms.auth.api.signin.internal.SignInHubActivity" />


    When i run it it still gives me errors like this :

    I/flutter (32747): PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null, null)


    What else i forgot ?

    Here is my fluter code :

    final GoogleSignIn _googleSignIn = GoogleSignIn(
    scopes: ['email'],
    clientId:"XXXXXXXXXXXX"
    );

    Future<void> google_LogIn(BuildContext context) async {
    try {
    print(1);
    final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
    print(2);
    if (googleUser != null) {
    final GoogleSignInAuthentication googleAuth =
    await googleUser.authentication;
    print(3);
    print('Token : ${googleAuth.idToken!}');
    final response = await http.post(
    Uri.parse('${dotenv.env['baseUrl']}/auth_google'),
    headers: <String, String>{
    'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(<String, String>{
    'idToken': googleAuth.idToken!,
    }),
    );

    if (response.statusCode == 200) {
    final Map<String, dynamic> user = jsonDecode(response.body);
    ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
    content: Text('Sign in successful! Welcome ${user['name']}')),
    );
    } else {
    ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(content: Text('Failed to sign in with Google')),
    );
    }
    }
    } catch (error) {
    ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(content: Text('Error occurred during Google sign in: $error')),
    );
    print(error);
    }
    }


    And my node server :


    const { OAuth2Client } = require('google-auth-library');

    //Google Auth
    async auth_google(req, res) {
    try {
    const token = req.body.idToken;
    const payload = await logIn_Controller.verify(token);
    res.status(200).json(payload);
    } catch (error) {
    res.status(401).json({ error: 'Invalid token' });
    }
    },


    async verify(token) {
    const ticket = await client.verifyIdToken({
    idToken: token,
    audience: CLIENT_ID,
    });
    const payload = ticket.getPayload();
    return payload;
    },


    When i try to print the token is null also .

    Some times if i comment the scopes and clientId it says this :

    I/flutter (18971): Null check operator used on a null value


    So what is my problem ?

    The node server is not even triggered so something wrong about auth ,specific in this command :

    final GoogleSignInAuthentication googleAuth =
    await googleUser.authentication;


    Because the first one is returning some info data about the users .So im guessing that is something wrong in that specific command.

    Continue reading...

Compartilhe esta Página