Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

feat: Social Logins Implementation #150

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/remote/repositories/auth_repository.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -27,6 +28,11 @@ class AuthRepository {
await ApiManager.callSafely(() => ApiManager.instance.authService.register(register));
}

Future<AuthToken> googleSignIn(GoogleSignInModel googleSignIn) async {
final body = await ApiManager.callSafely(() => ApiManager.instance.authService.googleSignIn(googleSignIn));
return AuthToken.fromJson(body);
}

Future<void> deleteToken() async {
await _storage.delete(key: AUTH_TOKEN);
Logger.root.info("Deleted token.");
Expand Down
15 changes: 15 additions & 0 deletions lib/remote/requests/google_signin.dart
Original file line number Diff line number Diff line change
@@ -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<String, String> toJson() => {'name': name, "email": email, "id_token": idToken};
}
9 changes: 9 additions & 0 deletions lib/remote/services/auth_service.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/remote/services/auth_service.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -13,6 +14,9 @@ abstract class AuthService extends ChopperService {
@Post(path: "register")
Future<Response<Object>> register(@Body() Register register);

@Post(path: "google/auth/callback")
Future<Response<Map<String, dynamic>>> googleSignIn(@Body() GoogleSignInModel googleSignIn);

static AuthService create() {
final client = ChopperClient(
baseUrl: API_URL,
Expand Down
23 changes: 23 additions & 0 deletions lib/screens/login/bloc/login_bloc.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -24,5 +26,26 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
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);
}
}
}
18 changes: 18 additions & 0 deletions lib/screens/login/bloc/login_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@ class LoginButtonPressed extends LoginEvent {
@override
List<Object> get props => [login];
}

class GoogleSignInButtonPressed extends LoginEvent{


const GoogleSignInButtonPressed();

@override
List<Object> get props => [];
}

class GoogleSignInFailed extends LoginEvent{
final String message;

const GoogleSignInFailed(this.message);

@override
List<Object> get props => [message];
}
15 changes: 14 additions & 1 deletion lib/screens/login/login_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_signin_button/flutter_signin_button.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';
Expand Down Expand Up @@ -43,6 +44,13 @@ class _LoginFormState extends State<LoginForm> {
final _passwordController = TextEditingController();
final _formKey = GlobalKey<FormState>();


Future<void> _onGoogleSignInButtonPressed() async {
BlocProvider.of<LoginBloc>(context).add(
GoogleSignInButtonPressed()
);
}

void _onLoginButtonPressed() {
if (!_formKey.currentState.validate()) return;

Expand Down Expand Up @@ -132,7 +140,12 @@ class _LoginFormState extends State<LoginForm> {
onPressed: () => Navigator.push(
context, MaterialPageRoute(builder: (context) => RegisterScreen())),
),
)
),
SignInButton(
Buttons.Google,
onPressed: _onGoogleSignInButtonPressed,
elevation: 5,
),
],
),
);
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down