Skip to content

Commit

Permalink
Check if user is already logged in while loading the app
Browse files Browse the repository at this point in the history
  • Loading branch information
omerfaunal committed Dec 23, 2023
1 parent 15570fd commit df37858
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions project/FrontEnd/collaborative_science_platform/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import 'package:flutter_portal/flutter_portal.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:provider/provider.dart';

void main() {
void main() async {
configureApp();
runApp(const MyApp());
runApp(ChangeNotifierProvider.value(value: Auth(), child: const MyApp()));
}

void configureApp() {
Expand All @@ -27,11 +27,16 @@ void configureApp() {

class MyApp extends StatelessWidget {
const MyApp({super.key});

Future<void> checkTokenAndLogin(BuildContext context) async {
final auth = Provider.of<Auth>(context, listen: false);
await auth.checkTokenAndLogin();
}

@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<Auth>(create: (context) => Auth()),
ChangeNotifierProvider<ScreenNavigation>(create: (context) => ScreenNavigation()),
ChangeNotifierProvider<ProfileDataProvider>(create: (context) => ProfileDataProvider()),
ChangeNotifierProvider<NodeProvider>(create: (context) => NodeProvider()),
Expand All @@ -41,16 +46,28 @@ class MyApp extends StatelessWidget {
create: (context) => QuestionAnswerProvider()),
ChangeNotifierProvider<AnnotationProvider>(create: (context) => AnnotationProvider()),
],
child: Portal(
child: MaterialApp.router(
routerConfig: router,
debugShowCheckedModeBanner: false,
title: Constants.appName,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color.fromARGB(255, 85, 234, 145)),
useMaterial3: true,
),
),
child: FutureBuilder(
future: checkTokenAndLogin(context),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Portal(
child: MaterialApp.router(
routerConfig: router,
debugShowCheckedModeBanner: false,
title: Constants.appName,
theme: ThemeData(
colorScheme:
ColorScheme.fromSeed(seedColor: const Color.fromARGB(255, 85, 234, 145)),
useMaterial3: true,
),
),
);
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
);
}
Expand Down

0 comments on commit df37858

Please sign in to comment.