Skip to content

Commit

Permalink
third party signin optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Ida631 committed Nov 22, 2024
1 parent f6da740 commit 54d355c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
29 changes: 29 additions & 0 deletions data/lib/repositories/auth_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,33 @@ class AuthRepositoryImpl implements AuthRepository {
throw mapServerErrorToDomainException(response);
}
}

@override
Future<String> validateAppleToken(String identityToken) async {
final baseURL = await apiEnvironmentRepository.getBaseUrl();
final url =
Uri.parse('http://localhost:8080/api/auth/sign_in_with_apple');
final response = await client.post(
url,
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
'identityToken': identityToken,
}),
);
if (response.statusCode != 200) {
print('Token validation failed: ${response.body}');
throw Exception('Failed to validate token with backend');
}
final responseJson = jsonDecode(response.body);

// Extract email from the payload
final String? email = responseJson["payload"]?["email"]; // Safely access payload

if (email == null || email.isEmpty) {
throw Exception('Validation failed: email not retrieved from token');
}
return email;
}
}
1 change: 1 addition & 0 deletions domain/lib/repositories_abstract/auth_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ abstract class AuthRepository {
Future<void> sendSignupVerificationCode(String email);
Future<String> updatePassword(String newPassword);
Future<String> syncUser(String? displayName, String email, String? photoUrl);
Future<String> validateAppleToken(String identityToken);
}
11 changes: 8 additions & 3 deletions domain/lib/usecases/auth_usecase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class AuthUseCase {

AuthUseCase({required this.repository, required this.tokenProvider});

Future<User> signup(String email, String password,String verificationCode,{String? name}) async {
Future<User> signup(String email, String password, String verificationCode,
{String? name}) async {
return await repository.signup(email, password, verificationCode, name: name);
}

Expand Down Expand Up @@ -64,6 +65,10 @@ class AuthUseCase {
return accessToken;
}

}
Future<String> validateAppleToken(String identityToken) async {
String email = await repository.validateAppleToken(identityToken);
return email;
}


}
41 changes: 11 additions & 30 deletions lib/presentation/settings/viewModels/login_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class LoginViewModel extends ChangeNotifier {
bool get isButtonEnabled => _isButtonEnabled;
bool obscureText = true;

String? email;

void togglePasswordVisibility() {
obscureText = !obscureText;
notifyListeners();
Expand Down Expand Up @@ -122,6 +124,7 @@ class LoginViewModel extends ChangeNotifier {
}

Future<void> signInWithApple() async {
String? email;
try {
_isSigningIn = false;
// Request credentials from Apple
Expand All @@ -141,42 +144,16 @@ class LoginViewModel extends ChangeNotifier {
),
);

print('Apple credential: $credential');
// Get the identity token
final identityToken = credential.identityToken;

if (identityToken == null) {
throw Exception('Identity token not available in Apple credentials');
}
// Send token to backend for validation
// This is where app sends token to backend to check if the the "identityToken" is real and safe to use.
final validateTokenEndpoint =
Uri.parse('http://localhost:8080/api/auth/sign_in_with_apple');
final response = await http.Client().post(
validateTokenEndpoint,
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
'identityToken': identityToken,
}),
);

if (response.statusCode != 200) {
print('Token validation failed: ${response.body}');
throw Exception('Failed to validate token with backend');
}
print('Token validated successfully: ${response.body}');

// Extract user information
final responseJson = jsonDecode(response.body);
final String? email = responseJson["payload"]["email"];

if (email == null) {
throw Exception('Email not available in Apple credentials');
} else {
print(email);
}
// Validate the token with backend and retrieve email if valid
email = await validateAppleToken(identityToken);

// Sync user with backend (you may modify syncUser based on backend response if needed)
await syncUser(null, email, null);

_isSigningIn = true;
Expand All @@ -187,4 +164,8 @@ class LoginViewModel extends ChangeNotifier {
rethrow; // Optionally rethrow for higher-level error handling
}
}

Future<String> validateAppleToken(String identityToken) async {
return await authUseCase.validateAppleToken(identityToken);
}
}

0 comments on commit 54d355c

Please sign in to comment.