Skip to content

Commit

Permalink
google oauth added
Browse files Browse the repository at this point in the history
  • Loading branch information
Shock@5678 committed Aug 16, 2024
1 parent ae720d5 commit afae38b
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 68 deletions.
8 changes: 8 additions & 0 deletions lib/core/queries/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class AuthQueries {
''';
}

String gAuth(String? name, String email) {
return '''
mutation{
oAuth(userInput: {email: "$email", name: "$name"})
}
''';
}

String loginAsGuest(String? name) {
return '''
mutation{
Expand Down
55 changes: 55 additions & 0 deletions lib/data/datasource/remote/remote_auth_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,61 @@ class RemoteAuthApi {
}
}

Future<DataState<UserEntity>> gAuth(String name, String email) async {
log('name: $name');
log('email: $email');

final isConnected = await utils.checkInternetConnectivity();

if (!isConnected) {
return DataFailed('Beacon is trying to connect with internet...');
}

final QueryResult result = await clientNonAuth.mutate(
MutationOptions(document: gql(_authQueries.gAuth(name, email))));

log(result.toString());

if (result.data != null && result.isConcrete) {
final token = "Bearer ${result.data!['oAuth']}";

UserModel? user;

user = UserModel(authToken: token, isGuest: false);

// storing auth token in hive
await localApi.saveUser(user);

// loading clients
final authClient = await graphqlConfig.authClient();
final subscriptionClient = await graphqlConfig.graphQlClient();
locator<RemoteAuthApi>().loadClient(authClient);
locator<RemoteHomeApi>().loadClient(authClient, subscriptionClient);
locator<RemoteGroupApi>().loadClient(authClient, subscriptionClient);
locator<RemoteHikeApi>().loadClient(authClient, subscriptionClient);

// fetching User Info

final dataState = await fetchUserInfo();

if (dataState is DataSuccess) {
final updatedUser = dataState.data!
.copyWithModel(authToken: user.authToken, isGuest: user.isGuest);

// saving locally
await localApi.saveUser(updatedUser);

return DataSuccess(updatedUser);
}
} else if (result.hasException) {
final message = encounteredExceptionOrError(result.exception!);

return DataFailed(message);
}

return DataFailed('An unexpected error occured.');
}

Future<DataState<UserEntity>> login(String email, String password) async {
log('calling login function $email');
final isConnected = await utils.checkInternetConnectivity();
Expand Down
5 changes: 5 additions & 0 deletions lib/data/repositories/auth_repository_implementation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class AuthRepositoryImplementation implements AuthRepository {
return remoteAuthApi.login(email, password);
}

@override
Future<DataState<UserEntity>> oAuth(String name, String email) {
return remoteAuthApi.gAuth(name, email);
}

@override
Future<DataState<UserEntity>> register(
String name, String email, String password) {
Expand Down
2 changes: 2 additions & 0 deletions lib/domain/repositories/auth_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ abstract class AuthRepository {
// Login function
Future<DataState<UserEntity>> login(String email, String password);

Future<DataState<UserEntity>> oAuth(String name, String email);

Future<DataState<String>> sendVerificationCode();

Future<DataState<UserEntity>> completeVerification();
Expand Down
4 changes: 4 additions & 0 deletions lib/domain/usecase/auth_usecase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class AuthUseCase {
return authRepository.login(email, password);
}

Future<DataState<UserEntity>> oAuthUseCase(String name, String email) async {
return authRepository.oAuth(name, email);
}

Future<DataState<UserEntity>> getUserInfoUseCase() async {
return authRepository.getUser();
}
Expand Down
51 changes: 0 additions & 51 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:beacon/config/enviornment_config.dart';
import 'package:beacon/core/utils/constants.dart';
import 'package:beacon/presentation/auth/auth_cubit/auth_cubit.dart';
import 'package:beacon/presentation/auth/verification_cubit/verification_cubit.dart';
import 'package:beacon/presentation/group/cubit/group_cubit/group_cubit.dart';
Expand Down Expand Up @@ -79,53 +78,3 @@ class MyApp extends StatelessWidget {
)
];
}

class DrawCircle extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = kBlue
..style = PaintingStyle.fill;

canvas.drawCircle(Offset(size.width / 2, 0), size.width / 2, paint);
}

@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false;
}
}

// class DrawCircle extends CustomPainter {
// @override
// void paint(Canvas canvas, Size size) {
// final paint = Paint()
// ..color = kBlue
// ..style = PaintingStyle.fill;

// final path = Path();

// // Convert angles to radians
// final angle1Rad = 60 * (math.pi / 180);
// final angle2Rad = 30 * (math.pi / 180);

// // Calculate the height of the cut based on the rectangle width and angle
// final cutHeight1 = size.width * math.tan(angle1Rad);
// final cutHeight2 = size.width * math.tan(angle2Rad);

// // Define the path
// path.moveTo(0, cutHeight1); // Start at the top-left corner with a cut
// path.lineTo(size.width, 0); // Top-right corner
// path.lineTo(
// size.width, size.height - cutHeight2); // Bottom-right corner with a cut
// path.lineTo(0, size.height); // Bottom-left corner
// path.close();

// canvas.drawPath(path, paint);
// }

// @override
// bool shouldRepaint(covariant CustomPainter oldDelegate) {
// return false;
// }
// }
36 changes: 19 additions & 17 deletions lib/presentation/auth/auth_cubit/auth_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:developer';

import 'package:beacon/config/router/router.dart';
import 'package:beacon/core/resources/data_state.dart';
import 'package:beacon/domain/usecase/auth_usecase.dart';
Expand Down Expand Up @@ -71,25 +69,29 @@ class AuthCubit extends Cubit<AuthState> {
}

void googleSignIn() async {
try {
const List<String> scopes = <String>[
'email',
'https://www.googleapis.com/auth/contacts.readonly',
];
const List<String> scopes = <String>[
'email',
'https://www.googleapis.com/auth/contacts.readonly',
];

GoogleSignIn _googleSignIn = GoogleSignIn(
// Optional clientId
// clientId: 'your-client_id.apps.googleusercontent.com',
scopes: scopes,
);
GoogleSignIn _googleSignIn = GoogleSignIn(
scopes: scopes,
);

final gAuth=await _googleSignIn.signIn();
final gAuth = await _googleSignIn.signIn();

if (gAuth != null && gAuth.displayName != null) {
var dataState =
await authUseCase.oAuthUseCase(gAuth.displayName!, gAuth.email);

// log(_googleSignIn.currentUser!.email);
// log(_googleSignIn.currentUser!.displayName!);
} catch (e) {
log(e.toString());
if (dataState is DataSuccess && dataState.data != null) {
emit(SuccessState());
} else {
emit(AuthErrorState(error: dataState.error!));
}
} else {
emit(AuthErrorState(
error: 'Something went wrong please try again later!'));
}
}
}

0 comments on commit afae38b

Please sign in to comment.