The login screen now redirects to the home screen if a user successfully logs in
This commit is contained in:
parent
475b1d0bc1
commit
04288e0d15
3 changed files with 30 additions and 4 deletions
|
|
@ -22,9 +22,12 @@ class AuthService {
|
|||
_googleSignInProvider.onAuthStateChange.pipe(_user);
|
||||
}
|
||||
|
||||
Future<void> googleLoginAndSignup() async {
|
||||
Future<FirebaseUser> googleLoginAndSignup() async {
|
||||
final user = await _googleSignInProvider.signIn();
|
||||
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
// Create a new user in Firestore if this is the first time signing in.
|
||||
if (!await _firestoreProvider.userExists(user.email)) {
|
||||
final newUserModel = UserModel(
|
||||
|
|
@ -37,6 +40,8 @@ class AuthService {
|
|||
);
|
||||
await _firestoreProvider.createUser(newUserModel, user.uid);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
Future<void> signOut() {
|
||||
|
|
|
|||
|
|
@ -1,12 +1,24 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../resources/authService.dart';
|
||||
|
||||
class HomeScreen extends StatelessWidget {
|
||||
final _auth = authService;
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Main Screen'),
|
||||
),
|
||||
body: Text('Tasks go here'),
|
||||
body: MaterialButton(
|
||||
onPressed: () => _auth.signOut(),
|
||||
child: Text(
|
||||
'SignOut',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
|
||||
|
|
@ -6,7 +8,7 @@ import '../widgets/logo.dart';
|
|||
import '../widgets/gradient_button.dart';
|
||||
|
||||
class LoginScreen extends StatelessWidget {
|
||||
final AuthService _authService = authService;
|
||||
final _authService = authService;
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
|
|
@ -23,7 +25,7 @@ class LoginScreen extends StatelessWidget {
|
|||
Expanded(
|
||||
child: Center(
|
||||
child: GradientButton(
|
||||
onTap: () => _authService.googleLoginAndSignup(),
|
||||
onTap: () => onLoginButtonTap(context),
|
||||
height: 50,
|
||||
width: 310,
|
||||
radius: 25,
|
||||
|
|
@ -38,6 +40,13 @@ class LoginScreen extends StatelessWidget {
|
|||
);
|
||||
}
|
||||
|
||||
Future<void> onLoginButtonTap(BuildContext context) async {
|
||||
final user = await _authService.googleLoginAndSignup();
|
||||
if (user != null) {
|
||||
Navigator.of(context).pushNamed('home/');
|
||||
}
|
||||
}
|
||||
|
||||
Widget getButtonBody() {
|
||||
return Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue