diff --git a/android/app/build.gradle b/android/app/build.gradle index 743c3ad..17c0297 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -43,7 +43,7 @@ android { targetSdkVersion 28 versionCode flutterVersionCode.toInteger() versionName flutterVersionName - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { @@ -62,6 +62,9 @@ flutter { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + androidTestImplementation 'androidx.test:runner:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' } + +// Add the following line to the bottom of the file: +apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin \ No newline at end of file diff --git a/android/app/google-services.json b/android/app/google-services.json new file mode 100644 index 0000000..b3f5975 --- /dev/null +++ b/android/app/google-services.json @@ -0,0 +1,85 @@ +{ + "project_info": { + "project_number": "653980426563", + "firebase_url": "https://istart-c130a.firebaseio.com", + "project_id": "istart-c130a", + "storage_bucket": "istart-c130a.appspot.com" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:653980426563:android:84187d91af3c634e7b4ff6", + "android_client_info": { + "package_name": "nov1kov.ru.istart" + } + }, + "oauth_client": [ + { + "client_id": "653980426563-mh68vb30ovkq1lpsm5rj0pvpdqinpa8c.apps.googleusercontent.com", + "client_type": 1, + "android_info": { + "package_name": "nov1kov.ru.istart", + "certificate_hash": "65a9b1f45a975d0cd4d8da765a4cbf86085dfbf9" + } + }, + { + "client_id": "653980426563-f7j1a72db8t6gl6cm3p2f2981spgd8q3.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyCrQyWJaPb6EAeT3OnVJWJF7svRxqDUmv8" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "653980426563-f7j1a72db8t6gl6cm3p2f2981spgd8q3.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + }, + { + "client_info": { + "mobilesdk_app_id": "1:653980426563:android:e89511fadcac1d917b4ff6", + "android_client_info": { + "package_name": "ru.nov1kov.istart" + } + }, + "oauth_client": [ + { + "client_id": "653980426563-hmacfcpbs8f4m8akj7eoegqa3bn8140p.apps.googleusercontent.com", + "client_type": 1, + "android_info": { + "package_name": "ru.nov1kov.istart", + "certificate_hash": "65a9b1f45a975d0cd4d8da765a4cbf86085dfbf9" + } + }, + { + "client_id": "653980426563-f7j1a72db8t6gl6cm3p2f2981spgd8q3.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyCrQyWJaPb6EAeT3OnVJWJF7svRxqDUmv8" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "653980426563-f7j1a72db8t6gl6cm3p2f2981spgd8q3.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/android/build.gradle b/android/build.gradle index b7faad8..abe1620 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,13 +1,14 @@ buildscript { - ext.kotlin_version = '1.2.71' + ext.kotlin_version = '1.3.0' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.2.1' + classpath 'com.android.tools.build:gradle:3.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath 'com.google.gms:google-services:3.2.1' // Google Services plugin } } } diff --git a/android/gradle.properties b/android/gradle.properties index 2bd6f4f..08f2b5f 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,2 +1,3 @@ org.gradle.jvmargs=-Xmx1536M - +android.enableJetifier=true +android.useAndroidX=true diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index 2819f02..3752d54 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip \ No newline at end of file diff --git a/lib/auth.dart b/lib/auth.dart new file mode 100644 index 0000000..1553f1b --- /dev/null +++ b/lib/auth.dart @@ -0,0 +1,44 @@ +import 'dart:async'; +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:google_sign_in/google_sign_in.dart'; + + +class Auth { + final GoogleSignIn _googleSignIn = GoogleSignIn(); + final FirebaseAuth _auth = FirebaseAuth.instance; + + static final Auth _instance = Auth._internal(); + + FirebaseUser user; + + factory Auth() { + return _instance; + } + + Auth._internal(); + + Future googleSignIn() async { + final GoogleSignInAccount googleUser = await _googleSignIn.signIn(); + final GoogleSignInAuthentication googleAuth = await googleUser.authentication; + + final AuthCredential credential = GoogleAuthProvider.getCredential( + accessToken: googleAuth.accessToken, + idToken: googleAuth.idToken, + ); + + final FirebaseUser user = (await _auth.signInWithCredential(credential)).user; + print("signed in " + user.displayName); + this.user = user; + return user; + } + + Future getCurrentUser() async { + FirebaseUser user = await _auth.currentUser(); + this.user = user; + return user; + } + + Future signOut() async { + return _auth.signOut(); + } +} diff --git a/lib/authpage.dart b/lib/authpage.dart index f15f809..83b3731 100644 --- a/lib/authpage.dart +++ b/lib/authpage.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; +import 'auth.dart'; import 'hellopage.dart'; import 'util.dart'; @@ -74,10 +75,7 @@ class _AuthPageState extends State { flex: 0, child: RaisedButton( onPressed: () { - Navigator.pushReplacement( - context, - MaterialPageRoute(builder: (context) => HelloPage()), - ); + login(); }, textColor: Colors.white, padding: const EdgeInsets.all(0.0), @@ -129,4 +127,16 @@ class _AuthPageState extends State { ), ); } + + void login() { + Auth().googleSignIn().then((user) { + Navigator.pushReplacement( + context, + MaterialPageRoute(builder: (context) => HelloPage()), + ); + }).catchError((error) { + final snackBar = SnackBar(content: Text('Ощибка при авторизации ' + error.toString())); + Scaffold.of(context).showSnackBar(snackBar); + }); + } } diff --git a/lib/hellopage.dart b/lib/hellopage.dart index 833b0b2..d8cb156 100644 --- a/lib/hellopage.dart +++ b/lib/hellopage.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:istart/resultpage.dart'; import 'Repository.dart'; +import 'auth.dart'; import 'util.dart'; class HelloPage extends StatefulWidget { @@ -11,7 +12,7 @@ class HelloPage extends StatefulWidget { class _HelloPageState extends State { String getHelloText() { - return "Привет, " + "Константин"; + return "Привет, " + Auth().user.displayName; } List> _dropDownMenuItems; diff --git a/lib/splash.dart b/lib/splash.dart index 9152d1f..506836b 100644 --- a/lib/splash.dart +++ b/lib/splash.dart @@ -1,8 +1,10 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:istart/resultpage.dart'; +import 'package:istart/util.dart'; import 'Repository.dart'; +import 'auth.dart'; import 'authpage.dart'; import 'hellopage.dart'; @@ -12,30 +14,46 @@ class splashscreen extends StatefulWidget { } class _splashscreenState extends State { - @override - void initState(){ + void initState() { super.initState(); - Timer(Duration(seconds: 1), () async { - var r = Repository(); - await r.load(); - Navigator.of(context).pushReplacement(MaterialPageRoute( - builder: (context) => AuthPage(), - )); + var auth = Auth(); + auth.getCurrentUser().then((user) async { + await Repository().load(); + if (user != null) { + openHelloPage(); + }else{ + openAuthPage(); + } + }).catchError((error) async { + await Repository().load(); + openAuthPage(); }); } + void openAuthPage() { + Navigator.of(context).pushReplacement(MaterialPageRoute( + builder: (context) => AuthPage(), + )); + } + + void openHelloPage() { + Navigator.of(context).pushReplacement(MaterialPageRoute( + builder: (context) => HelloPage(), + )); + } + // added test yourself - // and made the text to align at center + // and made the text to align at center @override Widget build(BuildContext context) { return Scaffold( - backgroundColor: Colors.indigo, + backgroundColor: CustomColors.GradientBottom, body: Center( child: Text( - "Quizstar\nTest Yourself !!", + "iStart", style: TextStyle( - fontSize: 50.0, + fontSize: 16.0, color: Colors.white, fontFamily: "Satisfy", ), @@ -44,4 +62,4 @@ class _splashscreenState extends State { ), ); } -} \ No newline at end of file +} diff --git a/pubspec.lock b/pubspec.lock index 6dc7b17..b7ab223 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -36,6 +36,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.2" + firebase_auth: + dependency: "direct main" + description: + name: firebase_auth + url: "https://pub.dartlang.org" + source: hosted + version: "0.14.0+5" + firebase_core: + dependency: transitive + description: + name: firebase_core + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.0+9" flutter: dependency: "direct main" description: flutter @@ -53,6 +67,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.11.0" + google_sign_in: + dependency: "direct main" + description: + name: google_sign_in + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.7" gradient_app_bar: dependency: "direct main" description: @@ -158,4 +179,4 @@ packages: version: "2.0.8" sdks: dart: ">=2.2.2 <3.0.0" - flutter: ">=0.1.4 <2.0.0" + flutter: ">=1.5.0 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index d72d1eb..972138f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -26,6 +26,13 @@ dependencies: gradient_app_bar: ^0.1.3 flutter_web_browser: ^0.11.0 + # Add the dependency for the Firebase Core Flutter SDK + # Add the dependency for the FlutterFire plugin for Google Analytics + # Add the dependencies for any other Firebase products you want to use in your app + # For example, to use Firebase Authentication and Cloud Firestore + firebase_auth: ^0.14.0+5 + google_sign_in: + dev_dependencies: flutter_test: sdk: flutter