Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lulin/task/apple signin #55

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Changes from 1 commit
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
Next Next commit
apple signin initial set
  • Loading branch information
Ida631 committed Nov 20, 2024
commit baa4387da280089afd5d62420df50d8e3869fa2e
12 changes: 9 additions & 3 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -47,7 +47,9 @@ PODS:
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS
- Toast (4.1.0)
- sign_in_with_apple (0.0.1):
- Flutter
- Toast (4.1.1)
- url_launcher_ios (0.0.1):
- Flutter

@@ -64,6 +66,7 @@ DEPENDENCIES:
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- share (from `.symlinks/plugins/share/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- sign_in_with_apple (from `.symlinks/plugins/sign_in_with_apple/ios`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)

SPEC REPOS:
@@ -99,6 +102,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/share/ios"
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"
sign_in_with_apple:
:path: ".symlinks/plugins/sign_in_with_apple/ios"
url_launcher_ios:
:path: ".symlinks/plugins/url_launcher_ios/ios"

@@ -119,9 +124,10 @@ SPEC CHECKSUMS:
path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46
share: 0b2c3e82132f5888bccca3351c504d0003b3b410
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
Toast: ec33c32b8688982cecc6348adeae667c1b9938da
sign_in_with_apple: f3bf75217ea4c2c8b91823f225d70230119b8440
Toast: 1f5ea13423a1e6674c4abdac5be53587ae481c4e
url_launcher_ios: 5334b05cef931de560670eeae103fd3e431ac3fe

PODFILE CHECKSUM: 4e8f8b2be68aeea4c0d5beb6ff1e79fface1d048

COCOAPODS: 1.15.2
COCOAPODS: 1.16.2
5 changes: 2 additions & 3 deletions ios/Runner/Runner.entitlements
Original file line number Diff line number Diff line change
@@ -2,10 +2,9 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.associated-domains</key>
<key>com.apple.developer.applesignin</key>
<array>
<string>applinks:compositesai.com</string>
<string>webcredentials:compositesai.com</string>
<string>Default</string>
</array>
</dict>
</plist>
66 changes: 66 additions & 0 deletions lib/presentation/settings/viewModels/login_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// lib/presentation/viewmodels/login_view_model.dart

import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:domain/usecases/auth_usecase.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import 'package:http/http.dart' as http;

class LoginViewModel extends ChangeNotifier {
final AuthUseCase authUseCase;
@@ -109,4 +113,66 @@ class LoginViewModel extends ChangeNotifier {
final accessToken = await authUseCase.syncUser(displayName, email, photoUrl);
return accessToken;
}

Future<void> signInWithApple() async {
try {
// Request credentials from Apple
final credential = await SignInWithApple.getAppleIDCredential(
scopes: [
AppleIDAuthorizationScopes.email,
AppleIDAuthorizationScopes.fullName,
],
webAuthenticationOptions: WebAuthenticationOptions(
clientId: 'com.example.swiftcompsignin',
redirectUri: kIsWeb //This is where Apple sends the user back after they sign in.
? Uri.parse('https://compositesai.com/')
: Uri.parse(
'https://flutter-sign-in-with-apple-example.glitch.me/callbacks/sign_in_with_apple',
),
),
);

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 validationResponse = await http.Client().post(
validateTokenEndpoint,
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
'identityToken': identityToken,
}),
);

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

// Extract user information
final String? email = credential.email;
final String? givenName = credential.givenName;

if (email == null) {
throw Exception('Email not available in Apple credentials');
}

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

// Notify listeners for UI update
notifyListeners();
} catch (e) {
print('Sign in with Apple failed: $e');
rethrow; // Optionally rethrow for higher-level error handling
}
}

}
3 changes: 3 additions & 0 deletions lib/presentation/settings/views/login_page.dart
Original file line number Diff line number Diff line change
@@ -120,6 +120,9 @@ class _LoginPageState extends State<LoginPage> {
}
}




@override
void dispose() {
_emailController.dispose();
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -779,6 +779,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.4.1"
sign_in_with_apple:
dependency: "direct main"
description:
name: sign_in_with_apple
sha256: "602f1374c9c4c33889c969b53ebf7cc8417c22cc7e25ea771581330173bc6603"
url: "https://pub.dev"
source: hosted
version: "6.1.3"
sign_in_with_apple_platform_interface:
dependency: transitive
description:
name: sign_in_with_apple_platform_interface
sha256: c2ef2ce6273fce0c61acd7e9ff5be7181e33d7aa2b66508b39418b786cca2119
url: "https://pub.dev"
source: hosted
version: "1.1.0"
sign_in_with_apple_web:
dependency: transitive
description:
name: sign_in_with_apple_web
sha256: c009e9beeb6c376e86aaa154fcc8b4e075d4bad90c56286b9668a51cdb6129ea
url: "https://pub.dev"
source: hosted
version: "2.1.0"
sky_engine:
dependency: transitive
description: flutter
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -49,6 +49,7 @@ dependencies:
flutter_dotenv: ^5.0.2
flutter_secure_storage: ^9.2.2
google_sign_in: ^6.2.2
sign_in_with_apple: ^6.1.3


dev_dependencies: