From 3d297feb248a58c0b284e427ff2881969df0dc05 Mon Sep 17 00:00:00 2001 From: PaRaDoX50 Date: Wed, 13 Jan 2021 03:05:55 +0530 Subject: [PATCH 1/3] Google Sign In added --- lib/remote/repositories/auth_repository.dart | 6 +++++ lib/remote/requests/google_signin.dart | 15 ++++++++++++ lib/remote/services/auth_service.chopper.dart | 9 ++++++++ lib/remote/services/auth_service.dart | 4 ++++ lib/screens/login/bloc/login_bloc.dart | 23 +++++++++++++++++++ lib/screens/login/bloc/login_event.dart | 20 ++++++++++++++++ lib/screens/login/login_screen.dart | 19 +++++++++++++++ pubspec.yaml | 2 ++ 8 files changed, 98 insertions(+) create mode 100644 lib/remote/requests/google_signin.dart diff --git a/lib/remote/repositories/auth_repository.dart b/lib/remote/repositories/auth_repository.dart index 195b25d..abbb5c7 100644 --- a/lib/remote/repositories/auth_repository.dart +++ b/lib/remote/repositories/auth_repository.dart @@ -1,6 +1,7 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:logging/logging.dart'; import 'package:mentorship_client/remote/api_manager.dart'; +import 'package:mentorship_client/remote/requests/google_signin.dart'; import 'package:mentorship_client/remote/requests/login.dart'; import 'package:mentorship_client/remote/requests/register.dart'; import 'package:mentorship_client/remote/responses/auth_token.dart'; @@ -27,6 +28,11 @@ class AuthRepository { await ApiManager.callSafely(() => ApiManager.instance.authService.register(register)); } + Future googleSignIn(GoogleSignInModel googleSignIn) async { + final body = await ApiManager.callSafely(() => ApiManager.instance.authService.googleSignIn(googleSignIn)); + return AuthToken.fromJson(body); + } + Future deleteToken() async { await _storage.delete(key: AUTH_TOKEN); Logger.root.info("Deleted token."); diff --git a/lib/remote/requests/google_signin.dart b/lib/remote/requests/google_signin.dart new file mode 100644 index 0000000..3143663 --- /dev/null +++ b/lib/remote/requests/google_signin.dart @@ -0,0 +1,15 @@ +import 'package:flutter/foundation.dart'; + +/// This data class represents all data necessary to create a login +class GoogleSignInModel { + final String name; + final String email; + final String idToken; + + GoogleSignInModel({@required this.name, @required this.email,@required this.idToken}) + : assert(name != null), + assert(email != null), + assert(idToken != null); + + Map toJson() => {'name': name, "email": email, "id_token": idToken}; +} diff --git a/lib/remote/services/auth_service.chopper.dart b/lib/remote/services/auth_service.chopper.dart index 25823ea..662e97c 100644 --- a/lib/remote/services/auth_service.chopper.dart +++ b/lib/remote/services/auth_service.chopper.dart @@ -31,4 +31,13 @@ class _$AuthService extends AuthService { final $request = Request('POST', $url, client.baseUrl, body: $body); return client.send($request); } + + @override + Future>> googleSignIn( + GoogleSignInModel googleSignIn) { + final $url = 'google/auth/callback'; + final $body = googleSignIn; + final $request = Request('POST', $url, client.baseUrl, body: $body); + return client.send, Map>($request); + } } diff --git a/lib/remote/services/auth_service.dart b/lib/remote/services/auth_service.dart index 7aee373..df4fd4f 100644 --- a/lib/remote/services/auth_service.dart +++ b/lib/remote/services/auth_service.dart @@ -1,5 +1,6 @@ import 'package:chopper/chopper.dart'; import 'package:mentorship_client/constants.dart'; +import 'package:mentorship_client/remote/requests/google_signin.dart'; import 'package:mentorship_client/remote/requests/login.dart'; import 'package:mentorship_client/remote/requests/register.dart'; @@ -13,6 +14,9 @@ abstract class AuthService extends ChopperService { @Post(path: "register") Future> register(@Body() Register register); + @Post(path: "google/auth/callback") + Future>> googleSignIn(@Body() GoogleSignInModel googleSignIn); + static AuthService create() { final client = ChopperClient( baseUrl: API_URL, diff --git a/lib/screens/login/bloc/login_bloc.dart b/lib/screens/login/bloc/login_bloc.dart index d154e6d..fbba5cd 100644 --- a/lib/screens/login/bloc/login_bloc.dart +++ b/lib/screens/login/bloc/login_bloc.dart @@ -1,8 +1,10 @@ import 'package:bloc/bloc.dart'; +import 'package:google_sign_in/google_sign_in.dart'; import 'package:mentorship_client/auth/auth_bloc.dart'; import 'package:mentorship_client/auth/bloc.dart'; import 'package:mentorship_client/failure.dart'; import 'package:mentorship_client/remote/repositories/auth_repository.dart'; +import 'package:mentorship_client/remote/requests/google_signin.dart'; import 'package:mentorship_client/screens/login/bloc/login_event.dart'; import 'package:mentorship_client/screens/login/bloc/login_state.dart'; @@ -24,5 +26,26 @@ class LoginBloc extends Bloc { yield LoginFailure(failure.message); } } + else if (event is GoogleSignInButtonPressed){ + yield LoginInProgress(); + try{ + GoogleSignIn googleSignIn = GoogleSignIn(scopes: [ + 'email' + ],); + GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn(); + GoogleSignInAuthentication authentication = await googleSignInAccount.authentication; + print(authentication.idToken); + final authToken = await authRepository.googleSignIn(GoogleSignInModel(name: googleSignInAccount.displayName, email: googleSignInAccount.email, idToken: authentication.idToken)); + yield LoginSuccess(); + authBloc.add(JustLoggedIn(authToken.token)); + + } on Failure catch (failure){ + yield LoginFailure(failure.message); + } + + } + else if (event is GoogleSignInFailed){ + yield LoginFailure(event.message); + } } } diff --git a/lib/screens/login/bloc/login_event.dart b/lib/screens/login/bloc/login_event.dart index f875cec..c0640e9 100644 --- a/lib/screens/login/bloc/login_event.dart +++ b/lib/screens/login/bloc/login_event.dart @@ -1,5 +1,7 @@ import 'package:equatable/equatable.dart'; +import 'package:mentorship_client/remote/requests/google_signin.dart'; import 'package:mentorship_client/remote/requests/login.dart'; +import 'package:mentorship_client/screens/login/bloc/bloc.dart'; abstract class LoginEvent extends Equatable { const LoginEvent(); @@ -13,3 +15,21 @@ class LoginButtonPressed extends LoginEvent { @override List get props => [login]; } + +class GoogleSignInButtonPressed extends LoginEvent{ + + + const GoogleSignInButtonPressed(); + + @override + List get props => []; +} + +class GoogleSignInFailed extends LoginEvent{ + final String message; + + const GoogleSignInFailed(this.message); + + @override + List get props => [message]; +} diff --git a/lib/screens/login/login_screen.dart b/lib/screens/login/login_screen.dart index 05f823f..6ed8d03 100644 --- a/lib/screens/login/login_screen.dart +++ b/lib/screens/login/login_screen.dart @@ -1,8 +1,11 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:flutter_signin_button/flutter_signin_button.dart'; +import 'package:google_sign_in/google_sign_in.dart'; import 'package:mentorship_client/auth/auth_bloc.dart'; import 'package:mentorship_client/extensions/context.dart'; import 'package:mentorship_client/remote/repositories/auth_repository.dart'; +import 'package:mentorship_client/remote/requests/google_signin.dart'; import 'package:mentorship_client/remote/requests/login.dart'; import 'package:mentorship_client/screens/login/bloc/bloc.dart'; import 'package:mentorship_client/screens/register/register_screen.dart'; @@ -43,6 +46,18 @@ class _LoginFormState extends State { final _passwordController = TextEditingController(); final _formKey = GlobalKey(); + GoogleSignIn _googleSignIn = GoogleSignIn( + scopes: [ + 'email' + ], + ); + + Future _onGoogleSignInButtonPressed() async { + BlocProvider.of(context).add( + GoogleSignInButtonPressed() + ); + } + void _onLoginButtonPressed() { if (!_formKey.currentState.validate()) return; @@ -89,6 +104,10 @@ class _LoginFormState extends State { key: _formKey, child: Column( children: [ + SignInButton( + Buttons.Google, + onPressed: _onGoogleSignInButtonPressed, + ), TextFormField( controller: _usernameController, validator: _validateUsername, diff --git a/pubspec.yaml b/pubspec.yaml index 4e2d84c..0ca1502 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,6 +37,8 @@ dependencies: bloc: ^6.0.1 flutter_bloc: ^6.0.1 chopper: ^3.0.3 + google_sign_in: ^4.5.9 + flutter_signin_button: ^1.1.0 # plugins not needed in app From f6c4531d04ae6c2c4ad0d6b3b08ce3a7fa60d698 Mon Sep 17 00:00:00 2001 From: PaRaDoX50 Date: Wed, 13 Jan 2021 03:12:26 +0530 Subject: [PATCH 2/3] adjusted position of google sign in button --- lib/screens/login/login_screen.dart | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/screens/login/login_screen.dart b/lib/screens/login/login_screen.dart index 6ed8d03..051b925 100644 --- a/lib/screens/login/login_screen.dart +++ b/lib/screens/login/login_screen.dart @@ -104,10 +104,6 @@ class _LoginFormState extends State { key: _formKey, child: Column( children: [ - SignInButton( - Buttons.Google, - onPressed: _onGoogleSignInButtonPressed, - ), TextFormField( controller: _usernameController, validator: _validateUsername, @@ -151,7 +147,12 @@ class _LoginFormState extends State { onPressed: () => Navigator.push( context, MaterialPageRoute(builder: (context) => RegisterScreen())), ), - ) + ), + SignInButton( + Buttons.Google, + onPressed: _onGoogleSignInButtonPressed, + elevation: 5, + ), ], ), ); From 15e14abfd6b7d1b61a2e6db6dac1a8346eba43f8 Mon Sep 17 00:00:00 2001 From: PaRaDoX50 Date: Wed, 13 Jan 2021 03:32:11 +0530 Subject: [PATCH 3/3] fixed warnings --- lib/screens/login/bloc/login_event.dart | 2 -- lib/screens/login/login_screen.dart | 7 ------- 2 files changed, 9 deletions(-) diff --git a/lib/screens/login/bloc/login_event.dart b/lib/screens/login/bloc/login_event.dart index c0640e9..d16bc77 100644 --- a/lib/screens/login/bloc/login_event.dart +++ b/lib/screens/login/bloc/login_event.dart @@ -1,7 +1,5 @@ import 'package:equatable/equatable.dart'; -import 'package:mentorship_client/remote/requests/google_signin.dart'; import 'package:mentorship_client/remote/requests/login.dart'; -import 'package:mentorship_client/screens/login/bloc/bloc.dart'; abstract class LoginEvent extends Equatable { const LoginEvent(); diff --git a/lib/screens/login/login_screen.dart b/lib/screens/login/login_screen.dart index 051b925..b330246 100644 --- a/lib/screens/login/login_screen.dart +++ b/lib/screens/login/login_screen.dart @@ -1,11 +1,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_signin_button/flutter_signin_button.dart'; -import 'package:google_sign_in/google_sign_in.dart'; import 'package:mentorship_client/auth/auth_bloc.dart'; import 'package:mentorship_client/extensions/context.dart'; import 'package:mentorship_client/remote/repositories/auth_repository.dart'; -import 'package:mentorship_client/remote/requests/google_signin.dart'; import 'package:mentorship_client/remote/requests/login.dart'; import 'package:mentorship_client/screens/login/bloc/bloc.dart'; import 'package:mentorship_client/screens/register/register_screen.dart'; @@ -46,11 +44,6 @@ class _LoginFormState extends State { final _passwordController = TextEditingController(); final _formKey = GlobalKey(); - GoogleSignIn _googleSignIn = GoogleSignIn( - scopes: [ - 'email' - ], - ); Future _onGoogleSignInButtonPressed() async { BlocProvider.of(context).add(