isAuthenticated method
Checks if the user is authenticated with a valid, non-expired token.
This method performs a simple expiry check on the access token, matching the pattern used in Kinde's js-utils SDK and other Kinde SDKs.
This method does not perform any login completion or mutate state.
Returns true if:
- Auth state exists
- Access token exists and is not empty
- Token has not expired
Returns false otherwise.
⚠️ Note: If your app supports web-based login flows, make sure to call completePendingLoginIfNeeded first to finalize any in-progress authentication before calling this.
Example:
if (await sdk.isAuthenticated()) {
// User is authenticated
} else {
// Redirect to login
}
Implementation
Future<bool> isAuthenticated() async {
final state = authState;
if (state == null || state.isExpired()) {
return false;
}
final token = state.accessToken;
if (token == null || token.isEmpty) {
return false;
}
return true;
}