BREAKING:

Google Login in Flutter

Step 1 – Set up in Google Cloud Console

  1. Go to Google Cloud Console.
  2. Create a project.
  3. Enable Google Sign-In API.
  4. Create OAuth 2.0 Client IDs:
    • For Android → Provide package name & SHA-1 key.
    • For iOS → Provide bundle ID.
  5. Save Client ID.

Step 2 – Add dependencies

yamlSao chépChỉnh sửadependencies:
  google_sign_in: ^6.2.1

Step 3 – Configure Android

  • Edit android/app/build.gradle:
gradleSao chépChỉnh sửadefaultConfig {
    applicationId "com.example.app"
    minSdkVersion 21
}
  • Add SHA-1 key in Google Cloud Console.

Step 4 – Configure iOS

  • In ios/Runner/Info.plist, add:
xmlSao chépChỉnh sửa<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>YOUR_REVERSED_CLIENT_ID</string>
    </array>
  </dict>
</array>

Step 5 – Implement in Flutter

dartSao chépChỉnh sửaimport 'package:google_sign_in/google_sign_in.dart';

final GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: ['email', 'profile'],
);

Future<void> signInWithGoogle() async {
  try {
    final account = await _googleSignIn.signIn();
    if (account != null) {
      print('User: ${account.displayName}, Email: ${account.email}');
    }
  } catch (error) {
    print('Sign in failed: $error');
  }
}

If you tell me whether you’re making a website or a Flutter/mobile app, I can give you a ready-to-run code example for your exact case.
Which one are you working on?

Bạn đã nói:

login by Google step by step for mobile

ChatGPT đã nói:

Alright — let’s do Google Login for mobile step-by-step.
I’ll cover Flutter, but the steps are almost identical for native Android/iOS.


1. Set up Google Cloud Console

  1. Go to Google Cloud Console.
  2. Create a new project (or select an existing one).
  3. Enable the Google Sign-In API:
    • Menu → APIs & Services → Library → search “Google Sign-In API” → Enable.
  4. Go to APIs & Services → CredentialsCreate Credentials → OAuth client ID.
  5. Choose Application type:
    • Android → Provide package name and SHA-1 fingerprint.
      • Get SHA-1: shSao chépChỉnh sửakeytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
    • iOS → Provide your iOS bundle ID.
  6. Save your OAuth Client ID (we’ll use it in code).

2. Add Flutter dependency

In your pubspec.yaml:

yamlSao chépChỉnh sửadependencies:
  google_sign_in: ^6.2.1

Run:

shSao chépChỉnh sửaflutter pub get

3. Android configuration

android/app/build.gradle

gradleSao chépChỉnh sửadefaultConfig {
    applicationId "com.example.app" // Your app package name
    minSdkVersion 21
}

Google Cloud Console:

  • Make sure you have the same package name here.
  • Add the SHA-1 fingerprint.

4. iOS configuration

Open ios/Runner/Info.plist and add:

xmlSao chépChỉnh sửa<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>YOUR_REVERSED_CLIENT_ID</string>
    </array>
  </dict>
</array>

YOUR_REVERSED_CLIENT_ID = reverse the Client ID from Google Console.
Example:
Client ID: 12345.apps.googleusercontent.com
Reversed: com.googleusercontent.apps.12345


5. Implement Sign-In

lib/main.dart

dartSao chépChỉnh sửaimport 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';

final GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: ['email', 'profile'],
);

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Google Login Example')),
        body: Center(
          child: ElevatedButton(
            onPressed: () async {
              try {
                final account = await _googleSignIn.signIn();
                if (account != null) {
                  debugPrint('Name: ${account.displayName}');
                  debugPrint('Email: ${account.email}');
                  debugPrint('Photo URL: ${account.photoUrl}');
                }
              } catch (error) {
                debugPrint('Sign in failed: $error');
              }
            },
            child: const Text('Sign in with Google'),
          ),
        ),
      ),
    );
  }
}

6. (Optional) Get ID Token for Backend

If your backend needs authentication:

dartSao chépChỉnh sửafinal auth = await _googleSignIn.currentUser?.authentication;
final idToken = auth?.idToken;

Send idToken to your server to verify with Google.


7. Test

  • Android → Run with flutter run on a real device or emulator.
  • iOS → Open in Xcode → run on simulator/real device.

Post A Comment

Your email address will not be published. Required fields are marked *

Leave a Reply