From 75ca7ba086ce8675de04b211b2db3d25c4ed1b94 Mon Sep 17 00:00:00 2001 From: Emre Date: Mon, 14 Aug 2023 19:04:24 +0200 Subject: [PATCH 1/5] 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: From cf0accfc47ec23c90d636861bad747badb612f0b Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Wed, 16 Aug 2023 13:08:27 +0200 Subject: [PATCH 2/5] Connection with google done :white_check_mark: --- .../lib/exceptions/user_exception.dart | 6 ++ .../justMUSIC/lib/screens/login_screen.dart | 18 +++- .../justMUSIC/lib/screens/profile_screen.dart | 4 +- .../lib/screens/registration_screen.dart | 14 ++- .../justMUSIC/lib/services/AuthService.dart | 91 ++++++++++++------- .../lib/view_model/UserViewModel.dart | 16 +++- 6 files changed, 101 insertions(+), 48 deletions(-) create mode 100644 Sources/justMUSIC/lib/exceptions/user_exception.dart diff --git a/Sources/justMUSIC/lib/exceptions/user_exception.dart b/Sources/justMUSIC/lib/exceptions/user_exception.dart new file mode 100644 index 0000000..22a137a --- /dev/null +++ b/Sources/justMUSIC/lib/exceptions/user_exception.dart @@ -0,0 +1,6 @@ +class UserException implements Exception { + String code; + String description; + + UserException(this.code,this.description); +} \ No newline at end of file diff --git a/Sources/justMUSIC/lib/screens/login_screen.dart b/Sources/justMUSIC/lib/screens/login_screen.dart index 0cbddf1..a1afcf3 100644 --- a/Sources/justMUSIC/lib/screens/login_screen.dart +++ b/Sources/justMUSIC/lib/screens/login_screen.dart @@ -1,4 +1,3 @@ -import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -10,6 +9,7 @@ import 'package:justmusic/main.dart'; import 'package:justmusic/values/constants.dart'; import '../components/login_button.dart'; +import '../exceptions/user_exception.dart'; class LoginScreen extends StatefulWidget { const LoginScreen({Key? key}) : super(key: key); @@ -33,7 +33,7 @@ class _LoginScreenState extends State { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( - e.toString() ?? "", + e.toString(), style: GoogleFonts.plusJakartaSans(color: Colors.white, fontWeight: FontWeight.w400, fontSize: 20.h), ), backgroundColor: Colors.red, @@ -43,6 +43,18 @@ class _LoginScreenState extends State { } } + signInWithGoogle() async { + try { + await MyApp.userViewModel.signInWithGoogle(); + } on UserException catch (e) { + if (e.code == 'user-created') { + Navigator.pushNamed(context, '/explanation'); + } else if (e.code == 'user-already-exist') { + Navigator.pushNamed(context, '/feed'); + } + } + } + @override Widget build(BuildContext context) { return GestureDetector( @@ -269,7 +281,7 @@ class _LoginScreenState extends State { SignInButton( Buttons.Google, text: "Login with Google", - onPressed: () {}, + onPressed: signInWithGoogle, ), ], ), diff --git a/Sources/justMUSIC/lib/screens/profile_screen.dart b/Sources/justMUSIC/lib/screens/profile_screen.dart index a40fd0a..f87ff7a 100644 --- a/Sources/justMUSIC/lib/screens/profile_screen.dart +++ b/Sources/justMUSIC/lib/screens/profile_screen.dart @@ -1,4 +1,3 @@ -import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; @@ -21,8 +20,7 @@ class _ProfileScreenState extends State { @override Widget build(BuildContext context) { Future logout() async { - print("cc"); - await FirebaseAuth.instance.signOut(); + await MyApp.userViewModel.logout(); Navigator.pushNamed(context, '/welcome'); } diff --git a/Sources/justMUSIC/lib/screens/registration_screen.dart b/Sources/justMUSIC/lib/screens/registration_screen.dart index afb2319..60be518 100644 --- a/Sources/justMUSIC/lib/screens/registration_screen.dart +++ b/Sources/justMUSIC/lib/screens/registration_screen.dart @@ -7,6 +7,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_signin_button/button_list.dart'; import 'package:flutter_signin_button/button_view.dart'; import 'package:google_fonts/google_fonts.dart'; +import 'package:justmusic/exceptions/user_exception.dart'; import 'package:justmusic/values/constants.dart'; import '../components/login_button.dart'; @@ -37,7 +38,7 @@ class _RegistrationScreenState extends State { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( - e.toString() ?? "", + e.toString(), style: GoogleFonts.plusJakartaSans( color: Colors.white, fontWeight: FontWeight.w400, @@ -51,8 +52,15 @@ class _RegistrationScreenState extends State { } signInWithGoogle() async { - await MyApp.userViewModel.signInWithGoogle(); - //Navigator.pushNamed(context, '/explanation'); + try { + await MyApp.userViewModel.signInWithGoogle(); + } on UserException catch (e) { + if (e.code == 'user-created') { + Navigator.pushNamed(context, '/explanation'); + } else if (e.code == 'user-already-exist') { + Navigator.pushNamed(context, '/feed'); + } + } } @override diff --git a/Sources/justMUSIC/lib/services/AuthService.dart b/Sources/justMUSIC/lib/services/AuthService.dart index 642c682..59411f0 100644 --- a/Sources/justMUSIC/lib/services/AuthService.dart +++ b/Sources/justMUSIC/lib/services/AuthService.dart @@ -3,44 +3,52 @@ 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 'package:justmusic/exceptions/user_exception.dart'; import '../main.dart'; class AuthService { - register(String pseudo, String email, String password) async { + Future addUserToFirestore( + UserCredential userCredential, String pseudo, String email) async { + var token; + if (!kIsWeb) { + token = await FirebaseMessaging.instance.getToken(); + } else { + token = "empty"; + } + + String uniqueId = await generateUniqueId(pseudo); + + final user = { + "mail": email, + "pseudo": pseudo, + "unique_id": uniqueId, + "followed": [], + "nbCapsules": 0, + "followers": [], + "token_notify": token, + "musics_likes": [], + "picture": + "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/justMusicDefaultImage.png?alt=media&token=020d0fcb-b7df-4d4d-b380-e99597293fcc" + }; + + try { + await MyApp.db + .collection("users") + .doc(userCredential.user?.uid) + .set(user); + print("User Added"); + } catch (error) { + print("Failed to add user: $error"); + } + } + + Future register(String pseudo, String email, String password) async { try { - var token; final data = await FirebaseAuth.instance.createUserWithEmailAndPassword( email: email, password: password, ); - - if (kIsWeb) { - token = "empty"; - } else { - token = await FirebaseMessaging.instance.getToken(); - } - - String uniqueId = await generateUniqueId(pseudo); - - final user = { - "mail": email, - "pseudo": pseudo, - "unique_id": uniqueId, - "followed": [], - "nbCapsules": 0, - "followers": [], - "token_notify": token, - "musics_likes": [], - "picture": - "https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/justMusicDefaultImage.png?alt=media&token=020d0fcb-b7df-4d4d-b380-e99597293fcc" - }; - - MyApp.db - .collection("users") - .doc(data.user?.uid) - .set(user) - .then((value) => print("User Added")) - .catchError((error) => print("Failed to add user: $error")); + await addUserToFirestore(data, pseudo, email); } on FirebaseAuthException catch (e) { if (e.code == 'weak-password') { throw ('Mot de passe trop court'); @@ -55,15 +63,26 @@ 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); + final userCredential = + await FirebaseAuth.instance.signInWithCredential(credential); + final user = + await MyApp.db.collection("users").doc(userCredential.user?.uid).get(); + + if (!user.exists) { + await addUserToFirestore( + userCredential, + userCredential.user?.displayName ?? "user", + userCredential.user?.email ?? ""); + throw UserException("user-created", "L'utilisateur vien d'être créé"); + } + + throw UserException("user-already-exist", "L'utilisateur existe déjà"); } Future generateUniqueId(String pseudo) async { @@ -100,8 +119,12 @@ class AuthService { } } - void signOut() async { + signOut() async { + final GoogleSignIn googleSignIn = GoogleSignIn(); await FirebaseAuth.instance.signOut(); + if (await googleSignIn.isSignedIn()) { + await googleSignIn.signOut(); + } } Future delete() async { diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart index adac733..61173f0 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -80,7 +80,13 @@ class UserViewModel { } signInWithGoogle() async { - await _authService.signInWithGoogle(); + try { + await _authService.signInWithGoogle(); + await updateUserCurrent(); + } catch (e) { + print(e); + rethrow; + } } Future> getUsersByUniqueId(String uniqueId) async { @@ -105,12 +111,12 @@ class UserViewModel { } } - logout() { - _authService.signOut(); + logout() async { + await _authService.signOut(); } - delete() { - _authService.delete(); + delete() async { + await _authService.delete(); } bool isFriend(String id) { From a21f3f42da2e284103a5c069a47451c9927b1a71 Mon Sep 17 00:00:00 2001 From: Emre Date: Wed, 16 Aug 2023 17:57:51 +0200 Subject: [PATCH 3/5] For web --- Sources/justMUSIC/lib/services/AuthService.dart | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/justMUSIC/lib/services/AuthService.dart b/Sources/justMUSIC/lib/services/AuthService.dart index 59411f0..24e8f6d 100644 --- a/Sources/justMUSIC/lib/services/AuthService.dart +++ b/Sources/justMUSIC/lib/services/AuthService.dart @@ -120,10 +120,12 @@ class AuthService { } signOut() async { - final GoogleSignIn googleSignIn = GoogleSignIn(); await FirebaseAuth.instance.signOut(); - if (await googleSignIn.isSignedIn()) { - await googleSignIn.signOut(); + if (!kIsWeb) { + final GoogleSignIn googleSignIn = GoogleSignIn(); + if (await googleSignIn.isSignedIn()) { + await googleSignIn.signOut(); + } } } From 31ee70e83f8dbbd5bda66fc0cbc8a9515871e1fc Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Wed, 16 Aug 2023 21:30:11 +0200 Subject: [PATCH 4/5] filtered pseudo --- Sources/justMUSIC/lib/services/AuthService.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/justMUSIC/lib/services/AuthService.dart b/Sources/justMUSIC/lib/services/AuthService.dart index 59411f0..96218c8 100644 --- a/Sources/justMUSIC/lib/services/AuthService.dart +++ b/Sources/justMUSIC/lib/services/AuthService.dart @@ -77,7 +77,7 @@ class AuthService { if (!user.exists) { await addUserToFirestore( userCredential, - userCredential.user?.displayName ?? "user", + userCredential.user?.displayName?.toLowerCase().replaceAll(' ', '') ?? "user", userCredential.user?.email ?? ""); throw UserException("user-created", "L'utilisateur vien d'être créé"); } From 927b6719556b74ba485b752777fb37ac98f0b673 Mon Sep 17 00:00:00 2001 From: emkartal1 Date: Wed, 16 Aug 2023 22:34:45 +0200 Subject: [PATCH 5/5] Change likes --- Sources/justMUSIC/lib/model/Post.dart | 10 +++++----- Sources/justMUSIC/lib/model/mapper/PostMapper.dart | 2 +- Sources/justMUSIC/lib/services/PostService.dart | 2 +- Sources/justMUSIC/lib/view_model/UserViewModel.dart | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Sources/justMUSIC/lib/model/Post.dart b/Sources/justMUSIC/lib/model/Post.dart index 224d956..66c3b53 100644 --- a/Sources/justMUSIC/lib/model/Post.dart +++ b/Sources/justMUSIC/lib/model/Post.dart @@ -9,13 +9,13 @@ class Post { String? _description; late Music _music; Tuple2 _location; - int _nblikes; + List _likes; String? _selfie; DateTime _date; // Constructor Post(this._id, this._user, this._description, this._location, - this._nblikes, this._selfie, this._date); + this._likes, this._selfie, this._date); //Getters and setters String get id => _id; @@ -40,10 +40,10 @@ class Post { _location = value; } - int get nblikes => _nblikes; + List get likes => _likes; - set nblikes(int value) { - _nblikes = value; + set likes(List value) { + _likes = value; } String? get selfie => _selfie; diff --git a/Sources/justMUSIC/lib/model/mapper/PostMapper.dart b/Sources/justMUSIC/lib/model/mapper/PostMapper.dart index 61ed7c4..f846568 100644 --- a/Sources/justMUSIC/lib/model/mapper/PostMapper.dart +++ b/Sources/justMUSIC/lib/model/mapper/PostMapper.dart @@ -15,7 +15,7 @@ class PostMapper { user!, data?["description"], Tuple2(data?["place"][0], data?["place"][1]), - data?["likes"], + List.from(data?["likes"] as List), data?["selfie"], data?["date"].toDate()); } diff --git a/Sources/justMUSIC/lib/services/PostService.dart b/Sources/justMUSIC/lib/services/PostService.dart index 3eb1819..a7d9742 100644 --- a/Sources/justMUSIC/lib/services/PostService.dart +++ b/Sources/justMUSIC/lib/services/PostService.dart @@ -16,7 +16,7 @@ class PostService { "date": DateTime.now(), "place": [location?.item1, location?.item2], "song_id": idMusic, - "likes": 0 + "likes": [] }; var postAdd = await MyApp.db.collection("posts").add(post); diff --git a/Sources/justMUSIC/lib/view_model/UserViewModel.dart b/Sources/justMUSIC/lib/view_model/UserViewModel.dart index a8450af..8bff593 100644 --- a/Sources/justMUSIC/lib/view_model/UserViewModel.dart +++ b/Sources/justMUSIC/lib/view_model/UserViewModel.dart @@ -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); if (firebase_auth.FirebaseAuth.instance.currentUser!.emailVerified) { await updateUserCurrent(); if (!kIsWeb) {