mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-17 22:14:28 +01:00
Integrate Firebase Authentication and Streamline Auth Flow in main.dart
This commit integrates Firebase Authentication into the application and refactors the `main.dart` file to streamline the user authentication flow. The application will now automatically switch between the main layout and the authentication UI based on the user's sign-in status. - Added Firebase initialization to the `main()` function for Firebase Authentication. - Replaced the home page in `MyApp` with a `StreamBuilder` that listens for Firebase auth state changes. - Based on the auth state, the home page will either show `MainLayout` (if the user is signed in) or `FirebaseAuthView` (if the user is not signed in). - Added necessary imports for the Firebase and authentication services.
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import 'package:auto_gpt_flutter_client/services/auth_service.dart';
|
||||
import 'package:auto_gpt_flutter_client/views/auth/firebase_auth_view.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'views/main_layout.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
|
||||
import 'package:auto_gpt_flutter_client/viewmodels/task_viewmodel.dart';
|
||||
import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart';
|
||||
@@ -14,7 +18,20 @@ import 'package:auto_gpt_flutter_client/services/benchmark_service.dart';
|
||||
import 'package:auto_gpt_flutter_client/utils/rest_api_utility.dart';
|
||||
|
||||
// TODO: Update documentation throughout project for consistency
|
||||
void main() {
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp(
|
||||
options: const FirebaseOptions(
|
||||
apiKey: 'AIzaSyBvYLAK_A0uhFuVPQbTxUdVWbb_Lsur9cg',
|
||||
authDomain: 'prod-auto-gpt.firebaseapp.com',
|
||||
projectId: 'prod-auto-gpt',
|
||||
storageBucket: 'prod-auto-gpt.appspot.com',
|
||||
messagingSenderId: '387936576242',
|
||||
appId: '1:387936576242:web:7536e0c50dd81b4dd7a66b',
|
||||
measurementId: 'G-8PRS69JJRL',
|
||||
),
|
||||
);
|
||||
|
||||
runApp(
|
||||
MultiProvider(
|
||||
providers: [
|
||||
@@ -48,29 +65,36 @@ void main() {
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Fetch services from providers
|
||||
final chatService = Provider.of<ChatService>(context, listen: false);
|
||||
final taskService = Provider.of<TaskService>(context, listen: false);
|
||||
final benchmarkService =
|
||||
Provider.of<BenchmarkService>(context, listen: false);
|
||||
taskService.loadDeletedTasks();
|
||||
|
||||
return MaterialApp(
|
||||
title: 'AutoGPT Flutter Client',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => ChatViewModel(chatService)),
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => TaskViewModel(taskService)),
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => SkillTreeViewModel(benchmarkService),
|
||||
),
|
||||
],
|
||||
child: MainLayout(),
|
||||
home: StreamBuilder<User?>(
|
||||
stream: FirebaseAuth.instance.authStateChanges(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return CircularProgressIndicator();
|
||||
}
|
||||
if (snapshot.hasData && snapshot.data != null) {
|
||||
return MultiProvider(
|
||||
providers: [
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => ChatViewModel(
|
||||
Provider.of<ChatService>(context, listen: false))),
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => TaskViewModel(
|
||||
Provider.of<TaskService>(context, listen: false))),
|
||||
ChangeNotifierProvider(
|
||||
create: (context) => SkillTreeViewModel(
|
||||
Provider.of<BenchmarkService>(context, listen: false)),
|
||||
),
|
||||
],
|
||||
child: MainLayout(),
|
||||
); // User is signed in
|
||||
}
|
||||
return FirebaseAuthView(); // User is not signed in, show auth UI
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user