Skip to content

Commit

Permalink
check if user is already authenticated
Browse files Browse the repository at this point in the history
  • Loading branch information
huextrat committed Apr 9, 2020
1 parent 0ad078b commit 6ba5192
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:thegorgeousotp/pages/login_page.dart';
import 'package:thegorgeousotp/pages/splash_page.dart';
import 'package:thegorgeousotp/stores/login_store.dart';

void main() => runApp(App());
Expand All @@ -20,7 +21,7 @@ class _AppState extends State<App> {
)
],
child: const MaterialApp(
home: LoginPage(),
home: SplashPage(),
),
);
}
Expand Down
1 change: 1 addition & 0 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:thegorgeousotp/stores/login_store.dart';
import 'package:thegorgeousotp/theme.dart';

class HomePage extends StatefulWidget {
const HomePage({Key key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
Expand Down
34 changes: 34 additions & 0 deletions lib/pages/splash_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:thegorgeousotp/pages/home_page.dart';
import 'package:thegorgeousotp/pages/login_page.dart';
import 'package:thegorgeousotp/stores/login_store.dart';
import 'package:thegorgeousotp/theme.dart';

class SplashPage extends StatefulWidget {
const SplashPage({Key key}) : super(key: key);
@override
_SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {

@override
void initState() {
super.initState();
Provider.of<LoginStore>(context, listen: false).isAlreadyAuthenticated().then((result) {
if (result) {
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (_) => const HomePage()), (Route<dynamic> route) => false);
} else {
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (_) => const LoginPage()), (Route<dynamic> route) => false);
}
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: MyColors.primaryColor,
);
}
}
10 changes: 10 additions & 0 deletions lib/stores/login_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ abstract class LoginStoreBase with Store {
@observable
FirebaseUser firebaseUser;

@action
Future<bool> isAlreadyAuthenticated() async {
firebaseUser = await _auth.currentUser();
if (firebaseUser != null) {
return true;
} else {
return false;
}
}

@action
Future<void> getCodeWithPhoneNumber(BuildContext context, String phoneNumber) async {
isLoginLoading = true;
Expand Down
9 changes: 9 additions & 0 deletions lib/stores/login_store.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6ba5192

Please sign in to comment.