mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-24 22:27:55 +00:00
Refactor code for better readability and maintainability
- Add method to get token and validate in AuthCubit - Update AuthState with loading, success, and error states for token - Use BlocBuilder in SplashView for token validation and navigation This commit refactors the code in AuthCubit to include a method to get the token and validate it. It also updates the AuthState with loading, success, and error states for token handling. In SplashView, BlocBuilder is now used to handle token validation and navigation based on the token status.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:syncrow_app/features/auth/model/token.dart';
|
||||
import 'package:syncrow_app/features/auth/bloc/auth_cubit.dart';
|
||||
import 'package:syncrow_app/generated/assets.dart';
|
||||
import 'package:syncrow_app/navigation/routing_constants.dart';
|
||||
|
||||
@@ -10,20 +10,18 @@ class SplashView extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder(
|
||||
future: getTokenAndValidate(context),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
if (snapshot.data ?? false) {
|
||||
Future.delayed(const Duration(seconds: 1), () {
|
||||
Navigator.pushReplacementNamed(context, Routes.homeRoute);
|
||||
});
|
||||
} else {
|
||||
Future.delayed(const Duration(seconds: 1), () {
|
||||
Navigator.pushReplacementNamed(context, Routes.authLogin);
|
||||
});
|
||||
}
|
||||
return BlocBuilder<AuthCubit, AuthState>(
|
||||
builder: (context, state) {
|
||||
if (state is AuthTokenSuccess) {
|
||||
Future.delayed(const Duration(seconds: 1), () {
|
||||
Navigator.pushReplacementNamed(context, Routes.homeRoute);
|
||||
});
|
||||
} else {
|
||||
Future.delayed(const Duration(seconds: 1), () {
|
||||
Navigator.pushReplacementNamed(context, Routes.authLogin);
|
||||
});
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
body: Stack(
|
||||
alignment: Alignment.center,
|
||||
@@ -61,26 +59,4 @@ class SplashView extends StatelessWidget {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> getTokenAndValidate(BuildContext context) async {
|
||||
return await const FlutterSecureStorage()
|
||||
.read(key: Token.loginAccessTokenKey)
|
||||
.then((value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
print("Decoding token Started");
|
||||
var tokenData = Token.decodeToken(value ?? "");
|
||||
print("checking token data");
|
||||
|
||||
if (tokenData.containsKey('exp')) {
|
||||
var exp = tokenData['exp'] ?? 0;
|
||||
var currentTime = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
print('time: $currentTime exp: $exp');
|
||||
return currentTime < exp;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user