Skip to content

Commit 5a7e5f3

Browse files
committed
part 05 completed
1 parent 966d3a6 commit 5a7e5f3

File tree

7 files changed

+109
-44
lines changed

7 files changed

+109
-44
lines changed

android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ android {
3535
defaultConfig {
3636
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
3737
applicationId "com.example.firebase_app_web"
38-
minSdkVersion 16
38+
minSdkVersion 18
3939
targetSdkVersion 30
4040
versionCode flutterVersionCode.toInteger()
4141
versionName flutterVersionName

lib/Service/Auth_Service.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:firebase_auth/firebase_auth.dart';
33
import 'package:flutter/cupertino.dart';
44
import 'package:flutter/material.dart';
55
import 'package:google_sign_in/google_sign_in.dart';
6+
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
67

78
class AuthClass {
89
GoogleSignIn _googleSignIn = GoogleSignIn(
@@ -12,6 +13,7 @@ class AuthClass {
1213
],
1314
);
1415
FirebaseAuth auth = FirebaseAuth.instance;
16+
final storage = new FlutterSecureStorage();
1517

1618
Future<void> googleSignIn(BuildContext context) async {
1719
try {
@@ -28,6 +30,7 @@ class AuthClass {
2830
try {
2931
UserCredential userCredential =
3032
await auth.signInWithCredential(credential);
33+
storeTokenAndData(userCredential);
3134
Navigator.pushAndRemoveUntil(
3235
context,
3336
MaterialPageRoute(builder: (builder) => HomePage()),
@@ -45,4 +48,23 @@ class AuthClass {
4548
ScaffoldMessenger.of(context).showSnackBar(snackbar);
4649
}
4750
}
51+
52+
Future<void> storeTokenAndData(UserCredential userCredential) async {
53+
await storage.write(
54+
key: "token", value: userCredential.credential.token.toString());
55+
await storage.write(
56+
key: "userCredential", value: userCredential.toString());
57+
}
58+
59+
Future<String> getToken() async {
60+
return await storage.read(key: "token");
61+
}
62+
63+
Future<void> logout() async {
64+
try {
65+
await _googleSignIn.signOut();
66+
await auth.signOut();
67+
await storage.delete(key: "token");
68+
} catch (e) {}
69+
}
4870
}

lib/main.dart

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:firebase_app_web/Service/Auth_Service.dart';
2+
import 'package:firebase_app_web/pages/HomePage.dart';
13
import 'package:firebase_app_web/pages/SignInPage.dart';
24
import 'package:firebase_app_web/pages/SignUpPage.dart';
35
import 'package:firebase_core/firebase_core.dart';
@@ -18,21 +20,29 @@ class MyApp extends StatefulWidget {
1820
}
1921

2022
class _MyAppState extends State<MyApp> {
21-
firebase_auth.FirebaseAuth firebaseAuth = firebase_auth.FirebaseAuth.instance;
23+
Widget currentPage = SignUpPage();
24+
AuthClass authClass = AuthClass();
2225

23-
void signup() async {
24-
try {
25-
await firebaseAuth.createUserWithEmailAndPassword(
26-
email: "devstackin1@gmail.com", password: "123456");
27-
} catch (e) {
28-
print(e);
26+
@override
27+
void initState() {
28+
// TODO: implement initState
29+
super.initState();
30+
checkLogin();
31+
}
32+
33+
void checkLogin() async {
34+
String token = await authClass.getToken();
35+
if (token != null) {
36+
setState(() {
37+
currentPage = HomePage();
38+
});
2939
}
3040
}
3141

3242
@override
3343
Widget build(BuildContext context) {
3444
return MaterialApp(
35-
home: SignUpPage(),
45+
home: currentPage,
3646
);
3747
}
3848
}

lib/pages/HomePage.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:firebase_app_web/Service/Auth_Service.dart';
2+
import 'package:firebase_app_web/pages/SignUpPage.dart';
13
import 'package:flutter/material.dart';
24

35
class HomePage extends StatefulWidget {
@@ -8,8 +10,23 @@ class HomePage extends StatefulWidget {
810
}
911

1012
class _HomePageState extends State<HomePage> {
13+
AuthClass authClass = AuthClass();
1114
@override
1215
Widget build(BuildContext context) {
13-
return Scaffold();
16+
return Scaffold(
17+
appBar: AppBar(
18+
actions: [
19+
IconButton(
20+
icon: Icon(Icons.logout),
21+
onPressed: () async {
22+
await authClass.logout();
23+
Navigator.pushAndRemoveUntil(
24+
context,
25+
MaterialPageRoute(builder: (builder) => SignUpPage()),
26+
(route) => false);
27+
})
28+
],
29+
),
30+
);
1431
}
1532
}

lib/pages/SignInPage.dart

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:firebase_app_web/Service/Auth_Service.dart';
12
import 'package:firebase_app_web/pages/SignUpPage.dart';
23
import 'package:flutter/material.dart';
34
import 'package:flutter_svg/flutter_svg.dart';
@@ -17,6 +18,7 @@ class _SignInPageState extends State<SignInPage> {
1718
TextEditingController _emailController = TextEditingController();
1819
TextEditingController _pwdController = TextEditingController();
1920
bool circular = false;
21+
AuthClass authClass = AuthClass();
2022

2123
@override
2224
Widget build(BuildContext context) {
@@ -40,11 +42,13 @@ class _SignInPageState extends State<SignInPage> {
4042
SizedBox(
4143
height: 20,
4244
),
43-
buttonItem("assets/google.svg", "Continue with Google", 25),
45+
buttonItem("assets/google.svg", "Continue with Google", 25, () {
46+
authClass.googleSignIn(context);
47+
}),
4448
SizedBox(
4549
height: 15,
4650
),
47-
buttonItem("assets/phone.svg", "Continue with Mobile", 30),
51+
buttonItem("assets/phone.svg", "Continue with Mobile", 30, () {}),
4852
SizedBox(
4953
height: 18,
5054
),
@@ -162,39 +166,43 @@ class _SignInPageState extends State<SignInPage> {
162166
);
163167
}
164168

165-
Widget buttonItem(String imagepath, String buttonName, double size) {
166-
return Container(
167-
width: MediaQuery.of(context).size.width - 60,
168-
height: 60,
169-
child: Card(
170-
color: Colors.black,
171-
elevation: 8,
172-
shape: RoundedRectangleBorder(
173-
borderRadius: BorderRadius.circular(15),
174-
side: BorderSide(
175-
width: 1,
176-
color: Colors.grey,
177-
),
178-
),
179-
child: Row(
180-
mainAxisAlignment: MainAxisAlignment.center,
181-
children: [
182-
SvgPicture.asset(
183-
imagepath,
184-
height: size,
185-
width: size,
186-
),
187-
SizedBox(
188-
width: 15,
169+
Widget buttonItem(
170+
String imagepath, String buttonName, double size, Function onTap) {
171+
return InkWell(
172+
onTap: onTap,
173+
child: Container(
174+
width: MediaQuery.of(context).size.width - 60,
175+
height: 60,
176+
child: Card(
177+
color: Colors.black,
178+
elevation: 8,
179+
shape: RoundedRectangleBorder(
180+
borderRadius: BorderRadius.circular(15),
181+
side: BorderSide(
182+
width: 1,
183+
color: Colors.grey,
189184
),
190-
Text(
191-
buttonName,
192-
style: TextStyle(
193-
color: Colors.white,
194-
fontSize: 17,
185+
),
186+
child: Row(
187+
mainAxisAlignment: MainAxisAlignment.center,
188+
children: [
189+
SvgPicture.asset(
190+
imagepath,
191+
height: size,
192+
width: size,
195193
),
196-
),
197-
],
194+
SizedBox(
195+
width: 15,
196+
),
197+
Text(
198+
buttonName,
199+
style: TextStyle(
200+
color: Colors.white,
201+
fontSize: 17,
202+
),
203+
),
204+
],
205+
),
198206
),
199207
),
200208
);

pubspec.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ packages:
104104
description: flutter
105105
source: sdk
106106
version: "0.0.0"
107+
flutter_secure_storage:
108+
dependency: "direct main"
109+
description:
110+
name: flutter_secure_storage
111+
url: "https://pub.dartlang.org"
112+
source: hosted
113+
version: "4.2.0"
107114
flutter_svg:
108115
dependency: "direct main"
109116
description:
@@ -289,4 +296,4 @@ packages:
289296
version: "5.1.0"
290297
sdks:
291298
dart: ">=2.12.0 <3.0.0"
292-
flutter: ">=1.24.0-7.0"
299+
flutter: ">=1.26.0-17.6.pre"

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies:
3232
firebase_auth: ^1.0.2
3333
flutter_svg: ^0.22.0
3434
google_sign_in: ^5.0.2
35+
flutter_secure_storage: ^4.2.0
3536

3637
dev_dependencies:
3738
flutter_test:

0 commit comments

Comments
 (0)