Skip to content

Commit

Permalink
Merge pull request #12 from FEUP-ESOF-2020-21/googleAuthentication
Browse files Browse the repository at this point in the history
Google authentication
  • Loading branch information
JViii authored Nov 20, 2020
2 parents bdc7f4f + 6777e3c commit 9932611
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 30 deletions.
27 changes: 21 additions & 6 deletions safe_meetings/lib/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import 'package:google_sign_in/google_sign_in.dart';
class Authentication {
final GoogleSignIn _googleSignIn = GoogleSignIn();
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
bool _loggedIn = false;

Future<String> googleSignIn() async {
bool get loggedIn => _loggedIn;

Future<User> googleSignIn() async {
final GoogleSignInAccount googleSignInAccount =
await _googleSignIn.signIn();
final GoogleSignInAuthentication googleSignInAuthentication =
Expand All @@ -19,18 +22,30 @@ class Authentication {

assert(user.displayName != null && user.email != null);
print(user.displayName);
print(user.email);
print(user.refreshToken);

final User currentUser = _firebaseAuth.currentUser;
assert(currentUser.uid == user.uid);

return 'googleSignIn';
print("loggedIN = true");
_loggedIn = true;

return user;
}

Future<String> googleSignOut() async {
_googleSignIn.signOut();
Future googleSignOut() async {
try {
await _firebaseAuth.signOut();
await _googleSignIn.signOut();
print("loggedIn = false");
_loggedIn = false;
} catch (e) {
print(e.toString());
return null;
}
}

return 'googleSignOut';
User getCurrentUser() {
return _firebaseAuth.currentUser;
}
}
1 change: 1 addition & 0 deletions safe_meetings/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SafeMeetings extends StatelessWidget {
'/home': (context) => Home(),
'/see_info': (context) => SeeInfo(),
'/sign_in': (context) => SignIn(),
'/sign_out': (context) => SignOut(),
'/search_menu': (context) => SearchMenu(),
// faltam os outros
},
Expand Down
68 changes: 51 additions & 17 deletions safe_meetings/lib/screens/home.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:safe_meetings/conference.dart';
import 'package:safe_meetings/auth.dart';
import 'package:firebase_auth/firebase_auth.dart';

class Home extends StatefulWidget {
@override
Expand All @@ -20,6 +22,8 @@ class _HomeState extends State<Home> {

bool filterReset = true;

bool signedIn = false;

void changeFilters(dynamic filters) {
setState(() {
this.titleFilter = filters['titleFilter'];
Expand Down Expand Up @@ -53,8 +57,18 @@ class _HomeState extends State<Home> {
List<Widget> confs = [];

for (int i = 0; i < conferences.length; i++) {
if ((conferences[i].getName() == this.titleFilter || this.titleFilter == "" || conferences[i].getName().toLowerCase().contains(this.titleFilter.toLowerCase())) &&
(conferences[i].getLocal() == this.localFilter || this.localFilter == "" || conferences[i].getLocal().toLowerCase().contains(this.localFilter.toLowerCase())) &&
if ((conferences[i].getName() == this.titleFilter ||
this.titleFilter == "" ||
conferences[i]
.getName()
.toLowerCase()
.contains(this.titleFilter.toLowerCase())) &&
(conferences[i].getLocal() == this.localFilter ||
this.localFilter == "" ||
conferences[i]
.getLocal()
.toLowerCase()
.contains(this.localFilter.toLowerCase())) &&
conferences[i].getHygien() >= this.hygienFilter &&
conferences[i].getSecurity() >= this.securityFilter &&
conferences[i].getInterest() >= this.interestFilter) {
Expand Down Expand Up @@ -103,9 +117,9 @@ class _HomeState extends State<Home> {

@override
Widget build(BuildContext context) {
if(this.start) {
if (this.start) {
this.start = false;

// gets the conferences passes by the loading screen
conferences = ModalRoute.of(context).settings.arguments;
}
Expand Down Expand Up @@ -139,7 +153,25 @@ class _HomeState extends State<Home> {
)
],
),
bottomNavigationBar: BottomAppBar(
bottomNavigationBar: loginButton(context),
backgroundColor: Colors.white,
body: GridView.count(
padding: EdgeInsets.all(20),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 2,
children: this.showConfs(),
),
floatingActionButton: this.filtersButton(),
);
}
}

Widget loginButton(BuildContext context) {
Authentication auth = Authentication();

return !auth.loggedIn //this condition should change but doesn't
? BottomAppBar(
color: Colors.green[50],
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand All @@ -154,16 +186,18 @@ class _HomeState extends State<Home> {
child: Text('Sign in to join conferences',
style: TextStyle(color: Colors.green[900], fontSize: 16)))
],
)),
backgroundColor: Colors.white,
body: GridView.count(
padding: EdgeInsets.all(20),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 2,
children: this.showConfs(),
),
floatingActionButton: this.filtersButton(),
);
}
))
: BottomAppBar(
color: Colors.green[50],
child: Row(
children: [
MaterialButton(
onPressed: () {
Navigator.pushNamed(context, '/sign_out');
},
child: Text('Account info',
style: TextStyle(color: Colors.green[900], fontSize: 16)))
],
),
);
}
66 changes: 59 additions & 7 deletions safe_meetings/lib/screens/sign_in.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,65 @@ class _SignInState extends State<SignIn> {
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
TextButton.icon(
onPressed: () async {
await auth.googleSignIn();
},
icon: Image.asset('images/google/google_logo.png'),
label: Text('Sign in with Google',style: TextStyle(color: Colors.green[900], fontSize: 20)),
)],
OutlineButton.icon(
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
onPressed: () async {
await auth.googleSignIn();
Navigator.pop(context);
},
icon: Image.asset('images/google/google_logo.png'),
label: Text('Sign in with Google',
style: TextStyle(color: Colors.green[900], fontSize: 20)),
)
],
),
),
);
}
}

class SignOut extends StatefulWidget {
@override
_SignOutState createState() => _SignOutState();
}

class _SignOutState extends State<SignOut> {
Authentication auth = Authentication();

@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
alignment: Alignment.center,
color: Colors.green[50],
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
OutlineButton.icon(
onPressed: () async {
await auth.googleSignOut();
Navigator.pop(context);
},
shape: new RoundedRectangleBorder(
side: BorderSide(width: 1, style: BorderStyle.solid),
borderRadius: new BorderRadius.circular(5.0)),
icon: ClipRRect(
borderRadius: BorderRadius.circular(8.0),
child: Image.network(
auth.getCurrentUser().photoURL,
height: 50.0,
width: 50.0,
fit: BoxFit.fill,
),
),
label: Text('Sign out',
style: TextStyle(color: Colors.green[900], fontSize: 20)),
),
//Text(auth.getCurrentUser().displayName),
//Text(auth.getFirebaseInstance().currentUser.email)
],
),
),
);
Expand Down

0 comments on commit 9932611

Please sign in to comment.