feat: implement social login functionality with Google, Facebook, and Twitter, including API integration and platform-specific configurations.
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:real_estate_mobile/core/network/api_client.dart';
|
||||
import 'package:real_estate_mobile/core/network/api_exceptions.dart';
|
||||
import 'package:real_estate_mobile/core/services/push_notification_service.dart';
|
||||
@@ -7,6 +10,7 @@ import 'package:real_estate_mobile/features/auth/data/auth_repository.dart';
|
||||
import 'package:real_estate_mobile/features/auth/data/models/register_request.dart';
|
||||
import 'package:real_estate_mobile/features/auth/data/models/user_model.dart';
|
||||
import 'package:real_estate_mobile/features/messaging/data/socket_service.dart';
|
||||
import 'package:twitter_login/twitter_login.dart';
|
||||
|
||||
// Auth state
|
||||
enum AuthStatus { initial, loading, authenticated, error }
|
||||
@@ -170,6 +174,182 @@ class AuthNotifier extends StateNotifier<AuthState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> signInWithGoogle() async {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.loading,
|
||||
errorMessage: null,
|
||||
fieldErrors: [],
|
||||
);
|
||||
|
||||
try {
|
||||
final googleSignIn = GoogleSignIn(
|
||||
scopes: ['email', 'profile'],
|
||||
serverClientId: '721413957511-goij5fnpal64o86vcppmkh743jhiv4nj.apps.googleusercontent.com',
|
||||
);
|
||||
final account = await googleSignIn.signIn();
|
||||
|
||||
if (account == null) {
|
||||
// User cancelled
|
||||
state = state.copyWith(status: AuthStatus.initial);
|
||||
return;
|
||||
}
|
||||
|
||||
final user = await _repository.socialAuth(
|
||||
provider: 'google',
|
||||
providerId: account.id,
|
||||
email: account.email,
|
||||
name: account.displayName,
|
||||
avatar: account.photoUrl,
|
||||
);
|
||||
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.authenticated,
|
||||
user: user,
|
||||
);
|
||||
_connectSocket();
|
||||
_initPushNotifications();
|
||||
} on ApiException catch (e) {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
errorMessage: e.message,
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('Google sign-in error: $e');
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
errorMessage: 'Google sign-in failed. Please try again.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> signInWithFacebook() async {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.loading,
|
||||
errorMessage: null,
|
||||
fieldErrors: [],
|
||||
);
|
||||
|
||||
try {
|
||||
final result = await FacebookAuth.instance.login(
|
||||
permissions: ['email', 'public_profile'],
|
||||
);
|
||||
|
||||
if (result.status == LoginStatus.cancelled) {
|
||||
state = state.copyWith(status: AuthStatus.initial);
|
||||
return;
|
||||
}
|
||||
|
||||
if (result.status != LoginStatus.success) {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
errorMessage: result.message ?? 'Facebook login failed',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final userData = await FacebookAuth.instance.getUserData(
|
||||
fields: 'id,name,email,picture.width(200)',
|
||||
);
|
||||
|
||||
final email = userData['email'] as String?;
|
||||
if (email == null || email.isEmpty) {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
errorMessage: 'Email not available from Facebook. Please use email login.',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final pictureData = userData['picture']?['data'];
|
||||
final avatarUrl = pictureData?['url'] as String?;
|
||||
|
||||
final user = await _repository.socialAuth(
|
||||
provider: 'facebook',
|
||||
providerId: userData['id'] as String,
|
||||
email: email,
|
||||
name: userData['name'] as String?,
|
||||
avatar: avatarUrl,
|
||||
);
|
||||
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.authenticated,
|
||||
user: user,
|
||||
);
|
||||
_connectSocket();
|
||||
_initPushNotifications();
|
||||
} on ApiException catch (e) {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
errorMessage: e.message,
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('Facebook sign-in error: $e');
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
errorMessage: 'Facebook sign-in failed. Please try again.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> signInWithTwitter() async {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.loading,
|
||||
errorMessage: null,
|
||||
fieldErrors: [],
|
||||
);
|
||||
|
||||
try {
|
||||
final twitterLogin = TwitterLogin(
|
||||
apiKey: 'eWEzaDJ0al9lX1diOGY0ejRYeXM6MTpjaQ',
|
||||
apiSecretKey: 'HadGMSRKKswfXOajbmggrnmLAn9cxJsl8GtFxGHqjX-CJ0A2Tu',
|
||||
redirectURI: 'request://',
|
||||
);
|
||||
|
||||
final authResult = await twitterLogin.login();
|
||||
|
||||
if (authResult.status == TwitterLoginStatus.cancelledByUser) {
|
||||
state = state.copyWith(status: AuthStatus.initial);
|
||||
return;
|
||||
}
|
||||
|
||||
if (authResult.status != TwitterLoginStatus.loggedIn) {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
errorMessage: authResult.errorMessage ?? 'Twitter login failed',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final twitterUser = authResult.user!;
|
||||
|
||||
final user = await _repository.socialAuth(
|
||||
provider: 'twitter',
|
||||
providerId: twitterUser.id.toString(),
|
||||
email: '${twitterUser.screenName}@twitter.placeholder',
|
||||
name: twitterUser.name,
|
||||
avatar: twitterUser.thumbnailImage,
|
||||
);
|
||||
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.authenticated,
|
||||
user: user,
|
||||
);
|
||||
_connectSocket();
|
||||
_initPushNotifications();
|
||||
} on ApiException catch (e) {
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
errorMessage: e.message,
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('Twitter sign-in error: $e');
|
||||
state = state.copyWith(
|
||||
status: AuthStatus.error,
|
||||
errorMessage: 'Twitter sign-in failed. Please try again.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> register({
|
||||
required String name,
|
||||
required String email,
|
||||
|
||||
Reference in New Issue
Block a user