feat: implement social login functionality with Google, Facebook, and Twitter, including API integration and platform-specific configurations.
This commit is contained in:
@@ -39,4 +39,7 @@ class ApiConstants {
|
||||
// 2FA
|
||||
static const String twoFactorVerify = '/auth/2fa/verify';
|
||||
static const String twoFactorVerifyBackup = '/auth/2fa/verify-backup';
|
||||
|
||||
// Social Auth
|
||||
static const String authSocial = '/auth/social';
|
||||
}
|
||||
|
||||
@@ -308,6 +308,13 @@ class _AgentDetailScreenState extends ConsumerState<AgentDetailScreen> {
|
||||
if (!mounted) return;
|
||||
if (conversationId != null) {
|
||||
context.push('/messages/chat/$conversationId');
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('You need to be connected to send messages.'),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) setState(() => _isStartingConversation = false);
|
||||
|
||||
@@ -130,6 +130,44 @@ class AuthRepository {
|
||||
}
|
||||
}
|
||||
|
||||
/// Social auth (Google, Facebook, etc.) — calls POST /auth/social
|
||||
Future<UserModel> socialAuth({
|
||||
required String provider,
|
||||
required String providerId,
|
||||
required String email,
|
||||
String? name,
|
||||
String? avatar,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _dio.post(
|
||||
ApiConstants.authSocial,
|
||||
data: {
|
||||
'provider': provider,
|
||||
'providerId': providerId,
|
||||
'email': email,
|
||||
if (name != null) 'name': name,
|
||||
if (avatar != null) 'avatar': avatar,
|
||||
},
|
||||
);
|
||||
|
||||
final data = response.data['data'] as Map<String, dynamic>;
|
||||
final authResponse = AuthResponse.fromJson(data);
|
||||
|
||||
await SecureStorage.saveTokens(
|
||||
accessToken: authResponse.accessToken,
|
||||
refreshToken: authResponse.refreshToken,
|
||||
);
|
||||
|
||||
return authResponse.user;
|
||||
} on DioException catch (e) {
|
||||
if (e.error is ApiException) throw e.error as ApiException;
|
||||
throw ApiException(
|
||||
message: e.message ?? 'Social login failed',
|
||||
statusCode: e.response?.statusCode,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Register returns { message, user } — no tokens.
|
||||
/// The user must verify their email before logging in.
|
||||
Future<Map<String, dynamic>> register(RegisterRequest request) async {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:real_estate_mobile/core/constants/app_colors.dart';
|
||||
import 'package:real_estate_mobile/features/auth/presentation/providers/auth_provider.dart';
|
||||
|
||||
class SocialLoginButtons extends StatelessWidget {
|
||||
class SocialLoginButtons extends ConsumerWidget {
|
||||
const SocialLoginButtons({super.key});
|
||||
|
||||
void _showComingSoon(BuildContext context) {
|
||||
@@ -16,13 +18,16 @@ class SocialLoginButtons extends StatelessWidget {
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final authState = ref.watch(authProvider);
|
||||
final isLoading = authState.status == AuthStatus.loading;
|
||||
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
// Apple
|
||||
_SocialButton(
|
||||
onTap: () => _showComingSoon(context),
|
||||
onTap: isLoading ? null : () => _showComingSoon(context),
|
||||
child: const Icon(
|
||||
Icons.apple,
|
||||
size: 28,
|
||||
@@ -32,7 +37,9 @@ class SocialLoginButtons extends StatelessWidget {
|
||||
const SizedBox(width: 32),
|
||||
// Google
|
||||
_SocialButton(
|
||||
onTap: () => _showComingSoon(context),
|
||||
onTap: isLoading
|
||||
? null
|
||||
: () => ref.read(authProvider.notifier).signInWithGoogle(),
|
||||
child: SvgPicture.asset(
|
||||
'assets/icons/google.svg',
|
||||
width: 24,
|
||||
@@ -42,7 +49,9 @@ class SocialLoginButtons extends StatelessWidget {
|
||||
const SizedBox(width: 32),
|
||||
// X (Twitter)
|
||||
_SocialButton(
|
||||
onTap: () => _showComingSoon(context),
|
||||
onTap: isLoading
|
||||
? null
|
||||
: () => ref.read(authProvider.notifier).signInWithTwitter(),
|
||||
child: SvgPicture.asset(
|
||||
'assets/icons/x.svg',
|
||||
width: 24,
|
||||
@@ -52,7 +61,9 @@ class SocialLoginButtons extends StatelessWidget {
|
||||
const SizedBox(width: 32),
|
||||
// Facebook
|
||||
_SocialButton(
|
||||
onTap: () => _showComingSoon(context),
|
||||
onTap: isLoading
|
||||
? null
|
||||
: () => ref.read(authProvider.notifier).signInWithFacebook(),
|
||||
child: SvgPicture.asset(
|
||||
'assets/icons/facebook.svg',
|
||||
width: 24,
|
||||
@@ -65,7 +76,7 @@ class SocialLoginButtons extends StatelessWidget {
|
||||
}
|
||||
|
||||
class _SocialButton extends StatelessWidget {
|
||||
final VoidCallback onTap;
|
||||
final VoidCallback? onTap;
|
||||
final Widget child;
|
||||
|
||||
const _SocialButton({
|
||||
@@ -77,10 +88,13 @@ class _SocialButton extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: onTap,
|
||||
child: SizedBox(
|
||||
width: 40,
|
||||
height: 40,
|
||||
child: Center(child: child),
|
||||
child: Opacity(
|
||||
opacity: onTap != null ? 1.0 : 0.5,
|
||||
child: SizedBox(
|
||||
width: 40,
|
||||
height: 40,
|
||||
child: Center(child: child),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -327,7 +327,9 @@ class _TopProfessionalsSectionState
|
||||
Expanded(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 12),
|
||||
child: Column(
|
||||
child: SingleChildScrollView(
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// Name
|
||||
@@ -515,6 +517,7 @@ class _TopProfessionalsSectionState
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -423,7 +423,7 @@ class MessagingNotifier extends StateNotifier<MessagingState> {
|
||||
conversations: updatedConversations,
|
||||
isSending: false,
|
||||
);
|
||||
} catch (_) {
|
||||
} catch (e) {
|
||||
final messagesAfterError =
|
||||
List<ChatMessage>.from(state.messages[conversationId] ?? []);
|
||||
messagesAfterError.removeWhere((m) => m.id == tempId);
|
||||
@@ -432,10 +432,15 @@ class MessagingNotifier extends StateNotifier<MessagingState> {
|
||||
Map<String, List<ChatMessage>>.from(state.messages)
|
||||
..[conversationId] = messagesAfterError;
|
||||
|
||||
// Show specific error for connection-required failures
|
||||
final errorMsg = e.toString().contains('connection')
|
||||
? 'You must be connected to send messages'
|
||||
: 'Failed to send message';
|
||||
|
||||
state = state.copyWith(
|
||||
messages: errorMessages,
|
||||
isSending: false,
|
||||
error: 'Failed to send message',
|
||||
error: errorMsg,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user