Fix auth redirect after login
This commit is contained in:
+16
-1
@@ -113,10 +113,12 @@ class AppRouter {
|
|||||||
final isLoggedIn = GoRouterRefreshNotifier.isAuthenticated;
|
final isLoggedIn = GoRouterRefreshNotifier.isAuthenticated;
|
||||||
final isLoggingIn = state.matchedLocation == '/auth';
|
final isLoggingIn = state.matchedLocation == '/auth';
|
||||||
|
|
||||||
|
// Если не авторизован и не на экране входа - redirect на auth
|
||||||
if (!isLoggedIn && !isLoggingIn) {
|
if (!isLoggedIn && !isLoggingIn) {
|
||||||
return '/auth';
|
return '/auth';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Если авторизован и на экране входа - redirect на home
|
||||||
if (isLoggedIn && isLoggingIn) {
|
if (isLoggedIn && isLoggingIn) {
|
||||||
return '/home';
|
return '/home';
|
||||||
}
|
}
|
||||||
@@ -133,15 +135,28 @@ class GoRouterRefreshNotifier extends ChangeNotifier {
|
|||||||
GoRouterRefreshNotifier() {
|
GoRouterRefreshNotifier() {
|
||||||
// Подписка на изменения аутентификации будет добавлена через provider
|
// Подписка на изменения аутентификации будет добавлена через provider
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void setAuthenticated(bool value) {
|
||||||
|
isAuthenticated = value;
|
||||||
|
// Роутер обновится автоматически через refreshListenable
|
||||||
|
// notifyListeners вызывается через provider
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void notifyListeners() {
|
||||||
|
super.notifyListeners();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provider для обновления роутера при изменении аутентификации
|
// Provider для обновления роутера при изменении аутентификации
|
||||||
final routerRefreshProvider = ChangeNotifierProvider((ref) {
|
final routerRefreshProvider = ChangeNotifierProvider((ref) {
|
||||||
final auth = ref.watch(authProvider);
|
final auth = ref.watch(authProvider);
|
||||||
|
final notifier = GoRouterRefreshNotifier();
|
||||||
|
|
||||||
auth.whenData((user) {
|
auth.whenData((user) {
|
||||||
GoRouterRefreshNotifier.isAuthenticated = user != null;
|
GoRouterRefreshNotifier.isAuthenticated = user != null;
|
||||||
|
notifier.notifyListeners();
|
||||||
});
|
});
|
||||||
|
|
||||||
return GoRouterRefreshNotifier();
|
return notifier;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import 'package:firebase_auth/firebase_auth.dart';
|
|||||||
import 'package:google_sign_in/google_sign_in.dart';
|
import 'package:google_sign_in/google_sign_in.dart';
|
||||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||||
import '../models/user_model.dart';
|
import '../models/user_model.dart';
|
||||||
|
import '../core/router.dart';
|
||||||
|
|
||||||
class AuthNotifier extends StateNotifier<AsyncValue<UserModel?>> {
|
class AuthNotifier extends StateNotifier<AsyncValue<UserModel?>> {
|
||||||
final FirebaseAuth _auth;
|
final FirebaseAuth _auth;
|
||||||
@@ -13,6 +14,7 @@ class AuthNotifier extends StateNotifier<AsyncValue<UserModel?>> {
|
|||||||
: super(const AsyncValue.data(null)) {
|
: super(const AsyncValue.data(null)) {
|
||||||
// Слушаем изменения состояния аутентификации
|
// Слушаем изменения состояния аутентификации
|
||||||
_auth.authStateChanges().listen((user) async {
|
_auth.authStateChanges().listen((user) async {
|
||||||
|
print('Auth state changed: ${user?.email}');
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
// Загружаем или создаём пользователя в Firestore
|
// Загружаем или создаём пользователя в Firestore
|
||||||
try {
|
try {
|
||||||
@@ -82,8 +84,20 @@ class AuthNotifier extends StateNotifier<AsyncValue<UserModel?>> {
|
|||||||
|
|
||||||
final userCredential = await _auth.signInWithCredential(credential);
|
final userCredential = await _auth.signInWithCredential(credential);
|
||||||
|
|
||||||
// Пользователь будет загружен через authStateChanges listener
|
// Обновляем состояние роутера
|
||||||
|
GoRouterRefreshNotifier.setAuthenticated(true);
|
||||||
print('Signed in: ${userCredential.user?.email}');
|
print('Signed in: ${userCredential.user?.email}');
|
||||||
|
// Пользователь будет загружен через authStateChanges listener
|
||||||
|
|
||||||
|
// Обновляем provider для notifyListeners
|
||||||
|
state = AsyncValue.data(UserModel(
|
||||||
|
id: userCredential.user!.uid,
|
||||||
|
email: userCredential.user!.email ?? '',
|
||||||
|
displayName: userCredential.user!.displayName,
|
||||||
|
photoUrl: userCredential.user!.photoURL,
|
||||||
|
createdAt: DateTime.now(),
|
||||||
|
updatedAt: DateTime.now(),
|
||||||
|
));
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
print('Google Sign-In Error: $e');
|
print('Google Sign-In Error: $e');
|
||||||
state = AsyncValue.error(e, st);
|
state = AsyncValue.error(e, st);
|
||||||
@@ -94,6 +108,10 @@ class AuthNotifier extends StateNotifier<AsyncValue<UserModel?>> {
|
|||||||
try {
|
try {
|
||||||
await _googleSignIn.signOut();
|
await _googleSignIn.signOut();
|
||||||
await _auth.signOut();
|
await _auth.signOut();
|
||||||
|
|
||||||
|
// Обновляем состояние роутера
|
||||||
|
GoRouterRefreshNotifier.setAuthenticated(false);
|
||||||
|
|
||||||
state = const AsyncValue.data(null);
|
state = const AsyncValue.data(null);
|
||||||
} catch (e, st) {
|
} catch (e, st) {
|
||||||
state = AsyncValue.error(e, st);
|
state = AsyncValue.error(e, st);
|
||||||
|
|||||||
Reference in New Issue
Block a user