-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor authentication handling and remove unused token validation l… #2378
Conversation
📝 WalkthroughWalkthroughThe pull request introduces modifications to the authentication flow in the Changes
Suggested labels
Suggested reviewers
Possibly related PRs
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (1)
mobile-v3/lib/main.dart (1)
169-174
: Add logging for authentication flowThe removal of logging makes it harder to debug authentication issues in production.
Consider adding strategic log points:
if (snapshot.connectionState == ConnectionState.done) { if (!snapshot.hasData) { + logInfo('No authentication token found, redirecting to welcome screen'); return WelcomeScreen(); } else { + logInfo('Authentication token found, proceeding to main navigation'); return NavPage(); } }
if (snapshot.connectionState == ConnectionState.done) { | ||
if (!snapshot.hasData) { | ||
return WelcomeScreen(); | ||
} else { | ||
return NavPage(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding token validation before navigation
The simplified authentication flow might expose security vulnerabilities. Currently, any non-null token value will trigger navigation to NavPage without validation.
Consider adding basic token validation:
if (snapshot.connectionState == ConnectionState.done) {
if (!snapshot.hasData) {
return WelcomeScreen();
} else {
+ final token = snapshot.data!;
+ if (!isJwtValid(token)) {
+ HiveRepository.deleteData('token', HiveBoxNames.authBox);
+ return WelcomeScreen();
+ }
return NavPage();
}
}
Add this helper function:
bool isJwtValid(String token) {
try {
final parts = token.split('.');
if (parts.length != 3) return false;
final payload = json.decode(
utf8.decode(base64Url.decode(base64Url.normalize(parts[1])))
);
final expiry = DateTime.fromMillisecondsSinceEpoch(payload['exp'] * 1000);
return DateTime.now().isBefore(expiry);
} catch (e) {
return false;
}
}
} else { | ||
logInfo('Authentication token found. Navigating to NavPage'); | ||
return NavPage(); | ||
return Scaffold(body: Center(child: Text('An Error occured.'))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance error handling for better user experience
The generic error message provides little context to users and no recovery options.
Consider implementing a more informative error screen:
-return Scaffold(body: Center(child: Text('An Error occured.')));
+return Scaffold(
+ body: Center(
+ child: Column(
+ mainAxisAlignment: MainAxisAlignment.center,
+ children: [
+ Text(
+ 'Unable to verify your session',
+ style: Theme.of(context).textTheme.titleLarge,
+ ),
+ const SizedBox(height: 16),
+ ElevatedButton(
+ onPressed: () => setState(() {}),
+ child: Text('Retry'),
+ ),
+ TextButton(
+ onPressed: () {
+ HiveRepository.deleteData('token', HiveBoxNames.authBox);
+ Navigator.of(context).pushReplacement(
+ MaterialPageRoute(builder: (_) => WelcomeScreen()),
+ );
+ },
+ child: Text('Return to Login'),
+ ),
+ ],
+ ),
+ ),
+);
Committable suggestion skipped: line range outside the PR's diff.
…ogic
Summary of Changes (What does this PR do?)
Status of maturity (all need to be checked before merging):
How should this be manually tested?
What are the relevant tickets?
Screenshots (optional)
Summary by CodeRabbit
Authentication Flow
Deprecation