Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Add token check on app startup
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityDarkLab committed Jan 29, 2024
1 parent bff02be commit 2e6bcdb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
15 changes: 11 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import 'firebase_options.dart';
final scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();

Future<void> main() async {
Logger.level = Level.info;
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

Logger.level = Level.debug;
runApp(
const ProviderScope(
const ProviderScope(
child: App(),
),
);
Expand All @@ -29,12 +29,19 @@ Future<void> main() async {
bool isPushNotificationListenerSet = false;

class App extends ConsumerWidget {
const App({super.key});

const App({
super.key,
});


@override
Widget build(BuildContext context, WidgetRef ref) {
final userState = ref.watch(userViewModelProvider);

bool isLoggedIn = ref.watch(userViewModelProvider).user != null;


_handleErrors(ref, userState);
_setupNotifications(ref, userState);

Expand All @@ -45,7 +52,7 @@ class App extends ConsumerWidget {
ref.watch(themeModeProvider), // Use the theme mode from the provider
navigatorKey: navigatorKey,
scaffoldMessengerKey: scaffoldMessengerKey,
home: userState.user == null
home: !isLoggedIn
? const WelcomeScreen()
: const NavigationTab(),
routes: _buildRoutes(),
Expand Down
26 changes: 25 additions & 1 deletion lib/view_models/user_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ import 'package:gocast_mobile/models/error/error_model.dart';
import 'package:gocast_mobile/models/user/user_state_model.dart';
import 'package:gocast_mobile/utils/globals.dart';
import 'package:gocast_mobile/utils/sort_utils.dart';
import 'package:jwt_decode/jwt_decode.dart';
import 'package:logger/logger.dart';

class UserViewModel extends StateNotifier<UserState> {
final Logger _logger = Logger();

final GrpcHandler _grpcHandler;

UserViewModel(this._grpcHandler) : super(const UserState());
UserViewModel(this._grpcHandler) : super(const UserState()){
// Check if the user is already logged in
_checkToken();
}


/// Handles basic authentication.
/// If the authentication is successful, it navigates to the courses screen.
Expand Down Expand Up @@ -152,4 +157,23 @@ class UserViewModel extends StateNotifier<UserState> {
state = state.copyWith(selectedSemester: choice);
}

Future<void> _checkToken() async {
String token = await _getToken();
if(token.isNotEmpty && !Jwt.isExpired(token)) {
_logger.i('Token found, fetching user: $token');
fetchUser();
}else {
_logger.i('Token not found or expired');
}
}

Future<String> _getToken() async {
try {
return await TokenHandler.loadToken('jwt');
} catch(e){
Logger().w("Token not found");
return '';
}
}

}
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.6.7"
jwt_decode:
dependency: "direct main"
description:
name: jwt_decode
sha256: d2e9f68c052b2225130977429d30f187aa1981d789c76ad104a32243cfdebfbb
url: "https://pub.dev"
source: hosted
version: "0.3.1"
lints:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies:
chewie: ^1.7.4
url_launcher: ^6.2.3
path_provider: any
jwt_decode: ^0.3.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 2e6bcdb

Please sign in to comment.