Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/features/auth/repo/auth_repo.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:pulse/features/auth/screens/splash_screen.dart';
import 'package:pulse/main.dart';
import 'package:pulse/utils/endpoints.dart';
import 'package:pulse/utils/local_storage.dart';
import 'package:pulse/utils/navigation.dart';
Expand Down Expand Up @@ -30,4 +31,14 @@ class AuthRepo {
await prefs.clear();
Navigation.go(screen: SplashScreen(), context: context, replace: true);
}

static Future<void> tokenExpired() async {
await prefs.clear();
Toast.showToast(
message: 'Token Expired', context: navigatorKey.currentContext!);
Navigation.go(
screen: SplashScreen(),
context: navigatorKey.currentContext!,
replace: true);
}
}
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import 'utils/colors.dart';
import 'utils/utils.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await dotenv.load(fileName: ".env");
Expand All @@ -36,6 +38,7 @@ class Pulse extends StatelessWidget {
BlocProvider(create: (_) => SessionsCubit()),
],
child: MaterialApp(
navigatorKey: navigatorKey,
debugShowCheckedModeBanner: false,
theme: ThemeData(
scaffoldBackgroundColor: Color(0xffF5F5F5),
Expand Down
8 changes: 6 additions & 2 deletions lib/utils/requests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart' as http;
import 'package:pulse/features/auth/repo/auth_repo.dart';
import 'package:pulse/utils/utils.dart';

import 'local_storage.dart';
Expand Down Expand Up @@ -143,9 +144,12 @@ class Requests {
return throw Exception('Invalid host url');
}

if (res.statusCode == 401) {
await AuthRepo.tokenExpired();
return null;
}

if (res.statusCode != okStatusCode) {
logger.i(res.body);
logger.i(endpoint);
return throw Exception(
json.decode(res.body)?['error'] ?? 'Error occurred');
}
Expand Down
Loading
Loading