mirror of
https://github.com/we-promise/sure
synced 2026-04-25 17:15:07 +02:00
Expose ui_layout and ai_enabled to mobile clients and add enable_ai endpoint (#983)
* Wire ui layout and AI flags into mobile auth Include ui_layout and ai_enabled in mobile login/signup/SSO payloads, add an authenticated endpoint to enable AI from Flutter, and gate mobile navigation based on intro layout and AI consent flow. * Linter * Ensure write scope on enable_ai * Make sure AI is available before enabling it * Test improvements * PR comment * Fix review issues: test assertion bug, missing coverage, and Dart defaults (#985) - Fix login test to use ai_enabled? (method) instead of ai_enabled (column) to match what mobile_user_payload actually serializes - Add test for enable_ai when ai_available? returns false (403 path) - Default aiEnabled to false when user is null in AuthProvider to avoid showing AI as available before authentication completes - Remove extra blank lines in auth_provider.dart and auth_service.dart https://claude.ai/code/session_01LEYYmtsDBoqizyihFtkye4 Co-authored-by: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -3,23 +3,64 @@ class User {
|
||||
final String email;
|
||||
final String? firstName;
|
||||
final String? lastName;
|
||||
final String uiLayout;
|
||||
final bool aiEnabled;
|
||||
|
||||
User({
|
||||
required this.id,
|
||||
required this.email,
|
||||
this.firstName,
|
||||
this.lastName,
|
||||
required this.uiLayout,
|
||||
required this.aiEnabled,
|
||||
});
|
||||
|
||||
bool get isIntroLayout => uiLayout == 'intro';
|
||||
|
||||
factory User.fromJson(Map<String, dynamic> json) {
|
||||
return User(
|
||||
id: json['id'].toString(),
|
||||
email: json['email'] as String,
|
||||
firstName: json['first_name'] as String?,
|
||||
lastName: json['last_name'] as String?,
|
||||
uiLayout: (json['ui_layout'] as String?) ?? 'dashboard',
|
||||
// Default to true when key is absent (legacy payloads from older app versions).
|
||||
// Avoids regressing existing users who would otherwise be incorrectly gated.
|
||||
aiEnabled: json.containsKey('ai_enabled')
|
||||
? (json['ai_enabled'] == true)
|
||||
: true,
|
||||
);
|
||||
}
|
||||
|
||||
User copyWith({
|
||||
String? id,
|
||||
String? email,
|
||||
String? firstName,
|
||||
String? lastName,
|
||||
String? uiLayout,
|
||||
bool? aiEnabled,
|
||||
}) {
|
||||
return User(
|
||||
id: id ?? this.id,
|
||||
email: email ?? this.email,
|
||||
firstName: firstName ?? this.firstName,
|
||||
lastName: lastName ?? this.lastName,
|
||||
uiLayout: uiLayout ?? this.uiLayout,
|
||||
aiEnabled: aiEnabled ?? this.aiEnabled,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'email': email,
|
||||
'first_name': firstName,
|
||||
'last_name': lastName,
|
||||
'ui_layout': uiLayout,
|
||||
'ai_enabled': aiEnabled,
|
||||
};
|
||||
}
|
||||
|
||||
String get displayName {
|
||||
if (firstName != null && lastName != null) {
|
||||
return '$firstName $lastName';
|
||||
|
||||
@@ -22,6 +22,8 @@ class AuthProvider with ChangeNotifier {
|
||||
bool _showMfaInput = false; // Track if we should show MFA input field
|
||||
|
||||
User? get user => _user;
|
||||
bool get isIntroLayout => _user?.isIntroLayout ?? false;
|
||||
bool get aiEnabled => _user?.aiEnabled ?? false;
|
||||
AuthTokens? get tokens => _tokens;
|
||||
bool get isLoading => _isLoading;
|
||||
bool get isInitializing => _isInitializing; // Expose initialization state
|
||||
@@ -321,6 +323,27 @@ class AuthProvider with ChangeNotifier {
|
||||
return _tokens?.accessToken;
|
||||
}
|
||||
|
||||
Future<bool> enableAi() async {
|
||||
final accessToken = await getValidAccessToken();
|
||||
if (accessToken == null) {
|
||||
_errorMessage = 'Session expired. Please login again.';
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
|
||||
final result = await _authService.enableAi(accessToken: accessToken);
|
||||
if (result['success'] == true) {
|
||||
_user = result['user'] as User?;
|
||||
_errorMessage = null;
|
||||
notifyListeners();
|
||||
return true;
|
||||
}
|
||||
|
||||
_errorMessage = result['error'] as String?;
|
||||
notifyListeners();
|
||||
return false;
|
||||
}
|
||||
|
||||
void clearError() {
|
||||
_errorMessage = null;
|
||||
notifyListeners();
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../providers/auth_provider.dart';
|
||||
import 'dashboard_screen.dart';
|
||||
import 'chat_list_screen.dart';
|
||||
import 'more_screen.dart';
|
||||
@@ -15,60 +17,147 @@ class _MainNavigationScreenState extends State<MainNavigationScreen> {
|
||||
int _currentIndex = 0;
|
||||
final _dashboardKey = GlobalKey<DashboardScreenState>();
|
||||
|
||||
late final List<Widget> _screens;
|
||||
List<Widget> _buildScreens(bool introLayout) {
|
||||
final screens = <Widget>[];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_screens = [
|
||||
DashboardScreen(key: _dashboardKey),
|
||||
const ChatListScreen(),
|
||||
const MoreScreen(),
|
||||
const SettingsScreen(),
|
||||
];
|
||||
if (!introLayout) {
|
||||
screens.add(DashboardScreen(key: _dashboardKey));
|
||||
}
|
||||
|
||||
screens.add(const ChatListScreen());
|
||||
|
||||
if (!introLayout) {
|
||||
screens.add(const MoreScreen());
|
||||
}
|
||||
|
||||
screens.add(const SettingsScreen());
|
||||
|
||||
return screens;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: IndexedStack(
|
||||
index: _currentIndex,
|
||||
children: _screens,
|
||||
List<NavigationDestination> _buildDestinations(bool introLayout) {
|
||||
final destinations = <NavigationDestination>[];
|
||||
|
||||
if (!introLayout) {
|
||||
destinations.add(
|
||||
const NavigationDestination(
|
||||
icon: Icon(Icons.home_outlined),
|
||||
selectedIcon: Icon(Icons.home),
|
||||
label: 'Home',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
destinations.add(
|
||||
const NavigationDestination(
|
||||
icon: Icon(Icons.chat_bubble_outline),
|
||||
selectedIcon: Icon(Icons.chat_bubble),
|
||||
label: 'AI Chat',
|
||||
),
|
||||
bottomNavigationBar: NavigationBar(
|
||||
selectedIndex: _currentIndex,
|
||||
onDestinationSelected: (index) {
|
||||
setState(() {
|
||||
_currentIndex = index;
|
||||
});
|
||||
// Reload preferences whenever switching back to dashboard
|
||||
if (index == 0) {
|
||||
_dashboardKey.currentState?.reloadPreferences();
|
||||
}
|
||||
},
|
||||
destinations: const [
|
||||
NavigationDestination(
|
||||
icon: Icon(Icons.home_outlined),
|
||||
selectedIcon: Icon(Icons.home),
|
||||
label: 'Home',
|
||||
);
|
||||
|
||||
if (!introLayout) {
|
||||
destinations.add(
|
||||
const NavigationDestination(
|
||||
icon: Icon(Icons.more_horiz),
|
||||
selectedIcon: Icon(Icons.more_horiz),
|
||||
label: 'More',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
destinations.add(
|
||||
const NavigationDestination(
|
||||
icon: Icon(Icons.settings_outlined),
|
||||
selectedIcon: Icon(Icons.settings),
|
||||
label: 'Settings',
|
||||
),
|
||||
);
|
||||
|
||||
return destinations;
|
||||
}
|
||||
|
||||
Future<bool> _showEnableAiPrompt() async {
|
||||
final authProvider = Provider.of<AuthProvider>(context, listen: false);
|
||||
|
||||
final shouldEnable = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Turn on AI Chat?'),
|
||||
content: const Text('AI Chat is currently disabled in your account settings. Would you like to turn it on now?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('Not now'),
|
||||
),
|
||||
NavigationDestination(
|
||||
icon: Icon(Icons.chat_bubble_outline),
|
||||
selectedIcon: Icon(Icons.chat_bubble),
|
||||
label: 'AI Chat',
|
||||
),
|
||||
NavigationDestination(
|
||||
icon: Icon(Icons.more_horiz),
|
||||
selectedIcon: Icon(Icons.more_horiz),
|
||||
label: 'More',
|
||||
),
|
||||
NavigationDestination(
|
||||
icon: Icon(Icons.settings_outlined),
|
||||
selectedIcon: Icon(Icons.settings),
|
||||
label: 'Settings',
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
child: const Text('Turn on AI'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (shouldEnable != true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final enabled = await authProvider.enableAi();
|
||||
|
||||
if (!enabled && mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(authProvider.errorMessage ?? 'Unable to enable AI right now.'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return enabled;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Consumer<AuthProvider>(
|
||||
builder: (context, authProvider, _) {
|
||||
final introLayout = authProvider.isIntroLayout;
|
||||
final screens = _buildScreens(introLayout);
|
||||
final destinations = _buildDestinations(introLayout);
|
||||
|
||||
if (_currentIndex >= screens.length) {
|
||||
_currentIndex = 0;
|
||||
}
|
||||
|
||||
final chatIndex = introLayout ? 0 : 1;
|
||||
final homeIndex = 0;
|
||||
|
||||
return Scaffold(
|
||||
body: IndexedStack(
|
||||
index: _currentIndex,
|
||||
children: screens,
|
||||
),
|
||||
bottomNavigationBar: NavigationBar(
|
||||
selectedIndex: _currentIndex,
|
||||
onDestinationSelected: (index) async {
|
||||
if (index == chatIndex && !authProvider.aiEnabled) {
|
||||
final enabled = await _showEnableAiPrompt();
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_currentIndex = index;
|
||||
});
|
||||
|
||||
if (!introLayout && index == homeIndex) {
|
||||
_dashboardKey.currentState?.reloadPreferences();
|
||||
}
|
||||
},
|
||||
destinations: destinations,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -435,6 +435,42 @@ class AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> enableAi({
|
||||
required String accessToken,
|
||||
}) async {
|
||||
try {
|
||||
final url = Uri.parse('${ApiConfig.baseUrl}/api/v1/auth/enable_ai');
|
||||
final response = await http.patch(
|
||||
url,
|
||||
headers: {
|
||||
...ApiConfig.getAuthHeaders(accessToken),
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
).timeout(const Duration(seconds: 30));
|
||||
|
||||
final responseData = jsonDecode(response.body);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final user = User.fromJson(responseData['user']);
|
||||
await _saveUser(user);
|
||||
return {
|
||||
'success': true,
|
||||
'user': user,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
'success': false,
|
||||
'error': responseData['error'] ?? responseData['errors']?.join(', ') ?? 'Failed to enable AI',
|
||||
};
|
||||
} catch (e) {
|
||||
return {
|
||||
'success': false,
|
||||
'error': 'Network error: ${e.toString()}',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await _storage.delete(key: _tokenKey);
|
||||
await _storage.delete(key: _userKey);
|
||||
@@ -474,12 +510,7 @@ class AuthService {
|
||||
Future<void> _saveUser(User user) async {
|
||||
await _storage.write(
|
||||
key: _userKey,
|
||||
value: jsonEncode({
|
||||
'id': user.id,
|
||||
'email': user.email,
|
||||
'first_name': user.firstName,
|
||||
'last_name': user.lastName,
|
||||
}),
|
||||
value: jsonEncode(user.toJson()),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user