🤖
GoogleSignInを実装する
公式ドキュメント通りにやれば進めれると思ったが?
GoogleSignInをドキュメントを見ながら、iOS、Androidの両方で実装したのですが、なんだか設定の仕方が変わっている気がする?
Firebaseのコンソールで、いつもAndroidに追加するコードが書かれていなかった?
今回GoogleSignInに使用したDartのパッケージ
デザインが用意されているログインのボタンのライブラリ
GoogleSignInに必要なライブラリ
完成品のソースコード
設定の前にフィンガープリントを生成する
パスワードの入力が求めれるので、ネットでデバッグ用はandroidと書かれていたので、androidにした。
keytool -list -v \
-alias androiddebugkey -keystore ~/.android/debug.keystore
いつもの!
apply plugin: 'com.google.gms.google-services'
これが、新しいもの?、使えない様ですが...
plugins {
id 'com.android.application'
// Add the Google services Gradle plugin
id 'com.google.gms.google-services'
...
}
昔から、ある同じ方法でbuild.gradleを設定する
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 31 // 31にする
ndkVersion flutter.ndkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.goole_sign_app"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
minSdkVersion 21 // 21にする
targetSdkVersion 31 //31にする
multiDexEnabled true //追加
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation platform('com.google.firebase:firebase-bom:30.3.2')
implementation 'com.android.support:multidex:1.0.3' // 追加
}
FlutterFireの公式ドキュメントに、マニュアルという昔からある設定方法が残っていました!
今回はこちらを参考にAndroidの設定を行いました。
iOS側の設定
pub.devの通りにやればできました。
サンプルコード
pubspec.yaml
name: goole_sign_app
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.17.3 <3.0.0"
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
firebase_core: ^1.21.1
firebase_auth: ^3.7.0
google_sign_in: ^5.4.1
sign_in_button: ^3.0.0
dev_dependencies:
flutter_test:
sdk: flutter
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^2.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter packages.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
main.dart
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:goole_sign_app/next_page.dart';
import 'package:sign_in_button/sign_in_button.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
// FlutterFireの公式ドキュメントのコードをそのまま使う!
Future<UserCredential> signInWithGoogle() async {
// 認証フローを起動する
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth =
await googleUser?.authentication;
// 新しいクレデンシャルを作成する
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
print(credential);
// NextPageへ画面遷移するコード
Navigator.push(
context, MaterialPageRoute(builder: (context) => NextPage()));
// サインインしたら、UserCredential を返す。
return await FirebaseAuth.instance.signInWithCredential(credential);
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
// ここでGoogleSignInの関数を実行する
Container(
width: 200,
height: 30,
child: SignInButton(Buttons.google, onPressed: () {
signInWithGoogle();
}),
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
next_page.dart
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class NextPage extends StatelessWidget {
const NextPage({Key? key}) : super(key: key);
Widget build(BuildContext context) {
// FibaseAutnを使えるようにする変数
final _auth = FirebaseAuth.instance;
return Scaffold(
appBar: AppBar(
// AppBarに表示されるデフォルトのボタンを非表示にする!
automaticallyImplyLeading: false,
actions: [
// ここで、ログアウトするメソッド実行する!
IconButton(
onPressed: () async {
await _auth.signOut();
// 前のページへ戻るコード
Navigator.pop(context);
},
icon: Icon(Icons.arrow_back))
],
title: Text('GoogleSignInしました!'),
),
body: Center(
child: Container(
child: Text('ようこそ!'),
),
),
);
}
}
スクリーンショット
最後に
以前は、UdemyやYouTubeを見ながら、作っていたのですが、今回はそんなことしなくても機能を実装することができました!
何度もやっていたので、設定の仕方がわかったのでしょうね。
1年前の自分にはできなかったですね。なんどもUdemyやってました😅
Discussion