Skip to content

Commit

Permalink
Merge pull request #15 from FEUP-ESOF-2020-21/googleAuth
Browse files Browse the repository at this point in the history
✨ Google auth done
  • Loading branch information
rodykings authored Nov 20, 2020
2 parents fb7fb9f + 9062383 commit 0954847
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 85 deletions.
81 changes: 34 additions & 47 deletions whowhat/lib/pages/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ class _MyLoginState extends State<MyLogin> {
final emailController = TextEditingController();
final passwordController = TextEditingController();

checkSignIn() async {
dynamic result = await _auth.signInWithEmail(
emailController.text, passwordController.text);
if (result == null) {
print("Error signing in");
} else {
print('signed in');
print(result);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyMenu(),
));
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -62,60 +78,31 @@ class _MyLoginState extends State<MyLogin> {
fontSize: 18),
)),
Padding(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.all(5),
child: InkWell(
child: Container(
height: 62,
width: MediaQuery.of(context).size.width * 0.25,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Color(0xFFECECEC),
),
child: Icon(AppIcons.google,
color: Color(0xFF9B9B9B)),
),
),
),
Padding(
padding: EdgeInsets.all(5),
child: InkWell(
child: Container(
height: 62,
width: MediaQuery.of(context).size.width * 0.25,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Color(0xFFECECEC),
),
child: Icon(AppIcons.facebook,
color: Color(0xFF9B9B9B)),
),
padding: EdgeInsets.all(10),
child: Padding(
padding: EdgeInsets.all(5),
child: InkWell(
child: Container(
height: 62,
width: MediaQuery.of(context).size.width * 0.25,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Color(0xFFECECEC),
),
child:
Icon(AppIcons.google, color: Color(0xFF9B9B9B)),
),
],
)),
onTap: () async {
_auth.signInWithGoogle();
}),
),
),
Padding(
padding: EdgeInsets.all(10),
child: GradientButton(
text: 'Sign In',
onPressed: () async {
dynamic result = await _auth.signInWithEmail(
emailController.text, passwordController.text);
if (result == null) {
print("Error signing in");
} else {
print('signed in');
print(result);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyMenu(),
));
}
checkSignIn();
},
),
),
Expand Down
77 changes: 40 additions & 37 deletions whowhat/lib/pages/signup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class _MySignupState extends State<MySignup> {

final emailController = TextEditingController();
final passwordController = TextEditingController();
final nameController = TextEditingController();
final jobController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -31,7 +33,7 @@ class _MySignupState extends State<MySignup> {
widthFactor: 0.8,
child: Column(
children: <Widget>[
Image(image: AssetImage('assets/images/login.png')),
/*Image(image: AssetImage('assets/images/login.png')),*/
Padding(
padding: EdgeInsets.all(10),
child: TextBox(
Expand All @@ -51,6 +53,24 @@ class _MySignupState extends State<MySignup> {
textInputType: TextInputType.visiblePassword,
),
),
Padding(
padding: EdgeInsets.all(10),
child: TextBox(
icon: Icons.person,
placeholder: 'Name',
controller: nameController,
textInputType: TextInputType.name,
),
),
Padding(
padding: EdgeInsets.all(10),
child: TextBox(
icon: Icons.cases,
placeholder: 'Job',
controller: jobController,
textInputType: TextInputType.name,
),
),
Padding(
padding: EdgeInsets.only(top: 10),
child: Text(
Expand All @@ -62,49 +82,32 @@ class _MySignupState extends State<MySignup> {
fontSize: 18),
)),
Padding(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 20),
child: InkWell(
child: Container(
height: 62,
width: MediaQuery.of(context).size.width * 0.25,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Color(0xFFECECEC),
),
child: Icon(AppIcons.google,
color: Color(0xFF9B9B9B)),
),
),
),
Padding(
padding: EdgeInsets.all(5),
child: InkWell(
child: Container(
height: 62,
width: MediaQuery.of(context).size.width * 0.25,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Color(0xFFECECEC),
),
child: Icon(AppIcons.facebook,
color: Color(0xFF9B9B9B)),
),
),
padding: EdgeInsets.symmetric(vertical: 20),
child: InkWell(
child: Container(
height: 62,
width: MediaQuery.of(context).size.width * 0.25,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Color(0xFFECECEC),
),
],
)),
child:
Icon(AppIcons.google, color: Color(0xFF9B9B9B)),
),
onTap: () async {
_auth.signInWithGoogle();
}),
),
Padding(
padding: EdgeInsets.all(10),
child: GradientButton(
text: 'Register',
onPressed: () async {
dynamic result = await _auth.registerWithEmail(
emailController.text, passwordController.text);
emailController.text,
passwordController.text,
nameController.text,
jobController.text);
if (result == null) {
print("Error signing in");
} else {
Expand Down
40 changes: 39 additions & 1 deletion whowhat/lib/widgets/database/auth.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:google_sign_in/google_sign_in.dart';

class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
final CollectionReference databaseReference =
FirebaseFirestore.instance.collection('users');

//Sign in Anon

Expand All @@ -28,15 +32,49 @@ class AuthService {
}
}

Future registerWithEmail(String email, String password) async {
Future registerWithEmail(
String email, String password, String name, String job) async {
try {
UserCredential result = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
User user = result.user;

String uid = user.uid.toString();

await databaseReference.doc(uid).set({'name': name, 'job': job});

return (user);
} catch (e) {
print(e.toString());
return null;
}
}

Future<String> getUserName() async {
if (_auth.currentUser != null) {
DocumentSnapshot ds =
await databaseReference.doc(_auth.currentUser.uid).get();
return ds.data()['name'];
}

return "nada";
}

Future<UserCredential> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();

// Obtain the auth details from the request
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;

// Create a new credential
final GoogleAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);

// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}
}

0 comments on commit 0954847

Please sign in to comment.