From 75ca7ba086ce8675de04b211b2db3d25c4ed1b94 Mon Sep 17 00:00:00 2001 From: Emre Date: Mon, 14 Aug 2023 19:04:24 +0200 Subject: [PATCH] Starting SignInWithGoogle :hammer: --- .../lib/screens/registration_screen.dart | 199 ++++++++++++------ .../justMUSIC/lib/services/AuthService.dart | 15 +- .../lib/view_model/UserViewModel.dart | 18 +- Sources/justMUSIC/pubspec.lock | 56 +++++ Sources/justMUSIC/pubspec.yaml | 1 + 5 files changed, 221 insertions(+), 68 deletions(-) diff --git a/Sources/justMUSIC/lib/screens/registration_screen.dart b/Sources/justMUSIC/lib/screens/registration_screen.dart index f0ada00..afb2319 100644 --- a/Sources/justMUSIC/lib/screens/registration_screen.dart +++ b/Sources/justMUSIC/lib/screens/registration_screen.dart @@ -30,14 +30,18 @@ class _RegistrationScreenState extends State { handleRegister() async { if (_formKey.currentState!.validate()) { try { - await MyApp.userViewModel.register(_userPseudoTextField.text, _passwordTextField.text, _userMailTextField.text); + await MyApp.userViewModel.register(_userPseudoTextField.text, + _passwordTextField.text, _userMailTextField.text); Navigator.pushNamed(context, '/explanation'); } catch (e) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( e.toString() ?? "", - style: GoogleFonts.plusJakartaSans(color: Colors.white, fontWeight: FontWeight.w400, fontSize: 20.h), + style: GoogleFonts.plusJakartaSans( + color: Colors.white, + fontWeight: FontWeight.w400, + fontSize: 20.h), ), backgroundColor: Colors.red, ), @@ -46,6 +50,11 @@ class _RegistrationScreenState extends State { } } + signInWithGoogle() async { + await MyApp.userViewModel.signInWithGoogle(); + //Navigator.pushNamed(context, '/explanation'); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -63,8 +72,10 @@ class _RegistrationScreenState extends State { padding: EdgeInsets.only(top: 100.h), child: AutoSizeText( "On a besoin de ça!", - style: - GoogleFonts.plusJakartaSans(color: Colors.white, fontWeight: FontWeight.w600, fontSize: 30.w), + style: GoogleFonts.plusJakartaSans( + color: Colors.white, + fontWeight: FontWeight.w600, + fontSize: 30.w), maxLines: 1, maxFontSize: 50, overflow: TextOverflow.fade, @@ -76,7 +87,8 @@ class _RegistrationScreenState extends State { mainAxisAlignment: MainAxisAlignment.start, children: [ Padding( - padding: EdgeInsets.symmetric(horizontal: defaultPadding), + padding: EdgeInsets.symmetric( + horizontal: defaultPadding), child: Padding( padding: EdgeInsets.only(bottom: 50.h), child: Column( @@ -90,7 +102,9 @@ class _RegistrationScreenState extends State { child: AutoSizeText( "Promis c’est rapide.", style: GoogleFonts.plusJakartaSans( - color: Colors.white, fontWeight: FontWeight.w400, fontSize: 17.w), + color: Colors.white, + fontWeight: FontWeight.w400, + fontSize: 17.w), maxFontSize: 20, textAlign: TextAlign.center, ), @@ -100,7 +114,10 @@ class _RegistrationScreenState extends State { ), ), Padding( - padding: EdgeInsets.only(bottom: 16.h, left: defaultPadding, right: defaultPadding), + padding: EdgeInsets.only( + bottom: 16.h, + left: defaultPadding, + right: defaultPadding), child: TextFormField( controller: _userPseudoTextField, keyboardAppearance: Brightness.dark, @@ -112,24 +129,37 @@ class _RegistrationScreenState extends State { }, cursorColor: primaryColor, keyboardType: TextInputType.emailAddress, - style: GoogleFonts.plusJakartaSans(color: primaryColor, fontSize: 15), + style: GoogleFonts.plusJakartaSans( + color: primaryColor, fontSize: 15), decoration: InputDecoration( focusedBorder: OutlineInputBorder( - borderSide: BorderSide(width: 1.sp, color: strokeTextField), - borderRadius: const BorderRadius.all(Radius.circular(10))), - prefix: const Padding(padding: EdgeInsets.only(left: 20.0)), - suffix: const Padding(padding: EdgeInsets.only(left: 20.0)), + borderSide: BorderSide( + width: 1.sp, + color: strokeTextField), + borderRadius: const BorderRadius.all( + Radius.circular(10))), + prefix: const Padding( + padding: EdgeInsets.only(left: 20.0)), + suffix: const Padding( + padding: EdgeInsets.only(left: 20.0)), fillColor: bgTextField, filled: true, - focusColor: const Color.fromRGBO(255, 255, 255, 0.30), + focusColor: const Color.fromRGBO( + 255, 255, 255, 0.30), enabledBorder: const OutlineInputBorder( - borderSide: BorderSide(width: 1, color: strokeTextField), - borderRadius: BorderRadius.all(Radius.circular(10))), + borderSide: BorderSide( + width: 1, color: strokeTextField), + borderRadius: BorderRadius.all( + Radius.circular(10))), hintText: 'Pseudo', - hintStyle: GoogleFonts.plusJakartaSans(color: strokeTextField)), + hintStyle: GoogleFonts.plusJakartaSans( + color: strokeTextField)), )), Padding( - padding: EdgeInsets.only(bottom: 16.h, left: defaultPadding, right: defaultPadding), + padding: EdgeInsets.only( + bottom: 16.h, + left: defaultPadding, + right: defaultPadding), child: TextFormField( controller: _userMailTextField, keyboardAppearance: Brightness.dark, @@ -141,24 +171,36 @@ class _RegistrationScreenState extends State { }, cursorColor: primaryColor, keyboardType: TextInputType.emailAddress, - style: GoogleFonts.plusJakartaSans(color: primaryColor), + style: GoogleFonts.plusJakartaSans( + color: primaryColor), decoration: InputDecoration( focusedBorder: OutlineInputBorder( - borderSide: BorderSide(width: 1, color: strokeTextField), - borderRadius: BorderRadius.all(Radius.circular(10))), - prefix: const Padding(padding: EdgeInsets.only(left: 20.0)), - suffix: const Padding(padding: EdgeInsets.only(left: 20.0)), + borderSide: BorderSide( + width: 1, color: strokeTextField), + borderRadius: BorderRadius.all( + Radius.circular(10))), + prefix: const Padding( + padding: EdgeInsets.only(left: 20.0)), + suffix: const Padding( + padding: EdgeInsets.only(left: 20.0)), fillColor: bgTextField, filled: true, - focusColor: Color.fromRGBO(255, 255, 255, 0.30), + focusColor: + Color.fromRGBO(255, 255, 255, 0.30), enabledBorder: OutlineInputBorder( - borderSide: BorderSide(width: 1, color: strokeTextField), - borderRadius: BorderRadius.all(Radius.circular(10))), + borderSide: BorderSide( + width: 1, color: strokeTextField), + borderRadius: BorderRadius.all( + Radius.circular(10))), hintText: 'Email', - hintStyle: GoogleFonts.plusJakartaSans(color: strokeTextField)), + hintStyle: GoogleFonts.plusJakartaSans( + color: strokeTextField)), )), Padding( - padding: EdgeInsets.only(bottom: 16.h, left: defaultPadding, right: defaultPadding), + padding: EdgeInsets.only( + bottom: 16.h, + left: defaultPadding, + right: defaultPadding), child: TextFormField( controller: _passwordTextField, keyboardAppearance: Brightness.dark, @@ -170,21 +212,29 @@ class _RegistrationScreenState extends State { return null; }, cursorColor: primaryColor, - style: GoogleFonts.plusJakartaSans(color: primaryColor), + style: GoogleFonts.plusJakartaSans( + color: primaryColor), decoration: InputDecoration( focusedBorder: OutlineInputBorder( - borderSide: BorderSide(width: 1, color: strokeTextField), - borderRadius: BorderRadius.all(Radius.circular(10))), - prefix: const Padding(padding: EdgeInsets.only(left: 20.0)), - suffix: const Padding(padding: EdgeInsets.only(left: 20.0)), + borderSide: BorderSide( + width: 1, color: strokeTextField), + borderRadius: + BorderRadius.all(Radius.circular(10))), + prefix: const Padding( + padding: EdgeInsets.only(left: 20.0)), + suffix: const Padding( + padding: EdgeInsets.only(left: 20.0)), fillColor: bgTextField, filled: true, focusColor: Color.fromRGBO(255, 255, 255, 0.30), enabledBorder: OutlineInputBorder( - borderSide: BorderSide(width: 1, color: strokeTextField), - borderRadius: BorderRadius.all(Radius.circular(10))), + borderSide: BorderSide( + width: 1, color: strokeTextField), + borderRadius: + BorderRadius.all(Radius.circular(10))), hintText: 'Mot de passe', - hintStyle: GoogleFonts.plusJakartaSans(color: strokeTextField), + hintStyle: GoogleFonts.plusJakartaSans( + color: strokeTextField), suffixIcon: Container( padding: EdgeInsets.only(right: 10), margin: EdgeInsets.all(5), @@ -204,8 +254,10 @@ class _RegistrationScreenState extends State { // Splash color over image child: Image( image: passenable - ? AssetImage("assets/images/show_icon.png") - : AssetImage("assets/images/hide_icon.png"), + ? AssetImage( + "assets/images/show_icon.png") + : AssetImage( + "assets/images/hide_icon.png"), height: 2, ), )), @@ -213,7 +265,10 @@ class _RegistrationScreenState extends State { ), ), Padding( - padding: EdgeInsets.only(bottom: 16.h, left: defaultPadding, right: defaultPadding), + padding: EdgeInsets.only( + bottom: 16.h, + left: defaultPadding, + right: defaultPadding), child: TextFormField( controller: _passwordConfirmTextField, keyboardAppearance: Brightness.dark, @@ -228,21 +283,29 @@ class _RegistrationScreenState extends State { }, cursorColor: primaryColor, keyboardType: TextInputType.emailAddress, - style: GoogleFonts.plusJakartaSans(color: primaryColor), + style: GoogleFonts.plusJakartaSans( + color: primaryColor), decoration: InputDecoration( focusedBorder: OutlineInputBorder( - borderSide: BorderSide(width: 1, color: strokeTextField), - borderRadius: BorderRadius.all(Radius.circular(10))), - prefix: const Padding(padding: EdgeInsets.only(left: 20.0)), - suffix: const Padding(padding: EdgeInsets.only(left: 20.0)), + borderSide: BorderSide( + width: 1, color: strokeTextField), + borderRadius: + BorderRadius.all(Radius.circular(10))), + prefix: const Padding( + padding: EdgeInsets.only(left: 20.0)), + suffix: const Padding( + padding: EdgeInsets.only(left: 20.0)), fillColor: bgTextField, filled: true, focusColor: Color.fromRGBO(255, 255, 255, 0.30), enabledBorder: OutlineInputBorder( - borderSide: BorderSide(width: 1, color: strokeTextField), - borderRadius: BorderRadius.all(Radius.circular(10))), + borderSide: BorderSide( + width: 1, color: strokeTextField), + borderRadius: + BorderRadius.all(Radius.circular(10))), hintText: 'Confirmation du Mot de passe', - hintStyle: GoogleFonts.plusJakartaSans(color: strokeTextField), + hintStyle: GoogleFonts.plusJakartaSans( + color: strokeTextField), suffixIcon: Container( padding: EdgeInsets.only(right: 10), margin: EdgeInsets.all(5), @@ -262,8 +325,10 @@ class _RegistrationScreenState extends State { // Splash color over image child: Image( image: passenable - ? AssetImage("assets/images/show_icon.png") - : AssetImage("assets/images/hide_icon.png"), + ? AssetImage( + "assets/images/show_icon.png") + : AssetImage( + "assets/images/hide_icon.png"), height: 2, ), )), @@ -271,7 +336,8 @@ class _RegistrationScreenState extends State { ), ), Padding( - padding: EdgeInsets.symmetric(horizontal: defaultPadding), + padding: EdgeInsets.symmetric( + horizontal: defaultPadding), child: SizedBox( width: 600, child: LoginButton( @@ -291,12 +357,16 @@ class _RegistrationScreenState extends State { text: TextSpan( text: 'Tu as déjà un compte?', style: GoogleFonts.plusJakartaSans( - color: Colors.white, fontWeight: FontWeight.w400, fontSize: 15), + color: Colors.white, + fontWeight: FontWeight.w400, + fontSize: 15), children: [ TextSpan( text: " Connexion", style: GoogleFonts.plusJakartaSans( - fontSize: 15, fontWeight: FontWeight.w400, color: primaryColor)), + fontSize: 15, + fontWeight: FontWeight.w400, + color: primaryColor)), ], ), ), @@ -318,10 +388,13 @@ class _RegistrationScreenState extends State { ), ), Padding( - padding: const EdgeInsets.only(left: defaultPadding, right: defaultPadding), + padding: const EdgeInsets.only( + left: defaultPadding, right: defaultPadding), child: Text( 'Ou', - style: GoogleFonts.plusJakartaSans(color: Colors.white, fontWeight: FontWeight.bold), + style: GoogleFonts.plusJakartaSans( + color: Colors.white, + fontWeight: FontWeight.bold), ), ), Expanded( @@ -341,8 +414,10 @@ class _RegistrationScreenState extends State { child: SignInButton( Buttons.Google, text: "Login with Google", - onPressed: () {}, - shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))), + onPressed: signInWithGoogle, + shape: RoundedRectangleBorder( + borderRadius: + BorderRadius.all(Radius.circular(20))), ), ), ), @@ -353,10 +428,13 @@ class _RegistrationScreenState extends State { child: Container( height: 240.h, decoration: BoxDecoration( - gradient: LinearGradient( - begin: Alignment.topRight, - stops: [0, 1], - colors: [bgColor.withOpacity(1), bgColor.withOpacity(0)])), + gradient: LinearGradient(begin: Alignment.topRight, stops: [ + 0, + 1 + ], colors: [ + bgColor.withOpacity(1), + bgColor.withOpacity(0) + ])), ), ), Align( @@ -364,7 +442,8 @@ class _RegistrationScreenState extends State { child: ConstrainedBox( constraints: BoxConstraints(maxWidth: 800), child: Padding( - padding: EdgeInsets.only(top: 45.h, left: defaultPadding, right: defaultPadding), + padding: EdgeInsets.only( + top: 45.h, left: defaultPadding, right: defaultPadding), child: ClipRRect( borderRadius: BorderRadius.circular(10.0), child: LinearProgressIndicator( diff --git a/Sources/justMUSIC/lib/services/AuthService.dart b/Sources/justMUSIC/lib/services/AuthService.dart index 79bb717..642c682 100644 --- a/Sources/justMUSIC/lib/services/AuthService.dart +++ b/Sources/justMUSIC/lib/services/AuthService.dart @@ -2,6 +2,7 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/foundation.dart'; +import 'package:google_sign_in/google_sign_in.dart'; import '../main.dart'; class AuthService { @@ -52,6 +53,19 @@ class AuthService { } } + signInWithGoogle() async { + final GoogleSignInAccount? gUser = await GoogleSignIn().signIn(); + + final GoogleSignInAuthentication gAuth = await gUser!.authentication; + + final credential = GoogleAuthProvider.credential( + accessToken: gAuth.accessToken, + idToken: gAuth.idToken, + ); + + return await FirebaseAuth.instance.signInWithCredential(credential); + } + Future generateUniqueId(String pseudo) async { String uniqueId = '$pseudo#0001'; int suffix = 1; @@ -103,7 +117,6 @@ class AuthService { await currentUser?.delete(); await FirebaseAuth.instance.signOut(); - } on FirebaseAuthException catch (e) { if (e.code == 'requires-recent-login') { throw ('Please log in again to delete your account'); diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart index 34278f2..adac733 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -12,7 +12,7 @@ import '../main.dart'; class UserViewModel { late User _userCurrent; - final AuthService authService = AuthService(); + final AuthService _authService = AuthService(); final UserService _userService = UserService(); User get userCurrent => _userCurrent; @@ -34,7 +34,7 @@ class UserViewModel { login(String pseudo, String password) async { try { var token; - await authService.login(pseudo, password); + await _authService.login(pseudo, password); await updateUserCurrent(); if (!kIsWeb) { token = await FirebaseMessaging.instance.getToken(); @@ -72,13 +72,17 @@ class UserViewModel { } try { - await authService.register(pseudo.toLowerCase(), email, password); + await _authService.register(pseudo.toLowerCase(), email, password); await updateUserCurrent(); } catch (e) { rethrow; } } + signInWithGoogle() async { + await _authService.signInWithGoogle(); + } + Future> getUsersByUniqueId(String uniqueId) async { try { var response = @@ -102,11 +106,11 @@ class UserViewModel { } logout() { - authService.signOut(); + _authService.signOut(); } delete() { - authService.delete(); + _authService.delete(); } bool isFriend(String id) { @@ -116,7 +120,7 @@ class UserViewModel { updateImage(File pp) async { try { await _userService.updateImage(pp); - } catch(e) { + } catch (e) { print(e.toString()); rethrow; } @@ -125,7 +129,7 @@ class UserViewModel { updatePseudo(String pseudo) async { try { await _userService.updatePseudo(pseudo); - } catch(e) { + } catch (e) { print(e.toString()); rethrow; } diff --git a/Sources/justMUSIC/pubspec.lock b/Sources/justMUSIC/pubspec.lock index ef6e363..89568be 100644 --- a/Sources/justMUSIC/pubspec.lock +++ b/Sources/justMUSIC/pubspec.lock @@ -608,6 +608,54 @@ packages: url: "https://pub.dev" source: hosted version: "4.0.4" + google_identity_services_web: + dependency: transitive + description: + name: google_identity_services_web + sha256: "7940fdc3b1035db4d65d387c1bdd6f9574deaa6777411569c05ecc25672efacd" + url: "https://pub.dev" + source: hosted + version: "0.2.1" + google_sign_in: + dependency: "direct main" + description: + name: google_sign_in + sha256: aab6fdc41374014494f9e9026b9859e7309639d50a0bf4a2a412467a5ae4abc6 + url: "https://pub.dev" + source: hosted + version: "6.1.4" + google_sign_in_android: + dependency: transitive + description: + name: google_sign_in_android + sha256: "8d60a787b29cb7d2bcf29230865f4a91f17323c6ac5b6b9027a6418e48d9ffc3" + url: "https://pub.dev" + source: hosted + version: "6.1.18" + google_sign_in_ios: + dependency: transitive + description: + name: google_sign_in_ios + sha256: "6ec0e13a4c5c646471b9f6a25ceb3ae76d339889d4c0f79b729bf0714215a63e" + url: "https://pub.dev" + source: hosted + version: "5.6.2" + google_sign_in_platform_interface: + dependency: transitive + description: + name: google_sign_in_platform_interface + sha256: e69553c0fc6a76216e9d06a8c3767e291ad9be42171f879aab7ab708569d4393 + url: "https://pub.dev" + source: hosted + version: "2.4.1" + google_sign_in_web: + dependency: transitive + description: + name: google_sign_in_web + sha256: "69b9ce0e760945ff52337921a8b5871592b74c92f85e7632293310701eea68cc" + url: "https://pub.dev" + source: hosted + version: "0.12.0+2" gradiantbutton: dependency: "direct main" description: @@ -904,6 +952,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.7.3" + quiver: + dependency: transitive + description: + name: quiver + sha256: b1c1ac5ce6688d77f65f3375a9abb9319b3cb32486bdc7a1e0fdf004d7ba4e47 + url: "https://pub.dev" + source: hosted + version: "3.2.1" rxdart: dependency: transitive description: diff --git a/Sources/justMUSIC/pubspec.yaml b/Sources/justMUSIC/pubspec.yaml index 872339f..494ee35 100644 --- a/Sources/justMUSIC/pubspec.yaml +++ b/Sources/justMUSIC/pubspec.yaml @@ -75,6 +75,7 @@ dependencies: timezone: ^0.9.2 firebase_messaging: ^14.6.5 cached_network_image: ^3.2.3 + google_sign_in: ^6.1.4 dev_dependencies: flutter_test: