Skip to content

Commit

Permalink
add firebase auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Nov1kov committed Sep 29, 2019
1 parent ff2b03b commit 19a0362
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 26 deletions.
9 changes: 6 additions & 3 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ android {
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
Expand All @@ -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
85 changes: 85 additions & 0 deletions android/app/google-services.json
Original file line number Diff line number Diff line change
@@ -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"
}
5 changes: 3 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -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 }
}
}

Expand Down
3 changes: 2 additions & 1 deletion android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.gradle.jvmargs=-Xmx1536M

android.enableJetifier=true
android.useAndroidX=true
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 44 additions & 0 deletions lib/auth.dart
Original file line number Diff line number Diff line change
@@ -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<FirebaseUser> 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<FirebaseUser> getCurrentUser() async {
FirebaseUser user = await _auth.currentUser();
this.user = user;
return user;
}

Future<void> signOut() async {
return _auth.signOut();
}
}
18 changes: 14 additions & 4 deletions lib/authpage.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';

import 'auth.dart';
import 'hellopage.dart';
import 'util.dart';

Expand Down Expand Up @@ -74,10 +75,7 @@ class _AuthPageState extends State<AuthPage> {
flex: 0,
child: RaisedButton(
onPressed: () {
Navigator.pushReplacement(
context,
MaterialPageRoute(builder: (context) => HelloPage()),
);
login();
},
textColor: Colors.white,
padding: const EdgeInsets.all(0.0),
Expand Down Expand Up @@ -129,4 +127,16 @@ class _AuthPageState extends State<AuthPage> {
),
);
}

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);
});
}
}
3 changes: 2 additions & 1 deletion lib/hellopage.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -11,7 +12,7 @@ class HelloPage extends StatefulWidget {

class _HelloPageState extends State<HelloPage> {
String getHelloText() {
return "Привет, " + "Константин";
return "Привет, " + Auth().user.displayName;
}

List<DropdownMenuItem<String>> _dropDownMenuItems;
Expand Down
44 changes: 31 additions & 13 deletions lib/splash.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -12,30 +14,46 @@ class splashscreen extends StatefulWidget {
}

class _splashscreenState extends State<splashscreen> {

@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",
),
Expand All @@ -44,4 +62,4 @@ class _splashscreenState extends State<splashscreen> {
),
);
}
}
}
23 changes: 22 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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"
7 changes: 7 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 19a0362

Please sign in to comment.