Comments (5)
Sorry, I have been busy lately.
I got the .data;
issue but the problem is that the function refreshToken
is still not being called.
After the latest update, I tested it again, it works fine.
Thank you @agordn52 for your support.
from nylo.
Hi @KhaledAlMana,
Hope you are well.
Thanks for your comments about the framework.
Re. the refreshToken method
Have you implemented the shouldRefreshToken
?
This will tell Nylo when when to refresh the token.
E.g.
@override
Future<bool> shouldRefreshToken() async {
User? user = Auth.user();
if (user.token.expiryDate.isPast()) {
// Check if the token is expired
// This will trigger the [refreshToken] method
return true;
}
return false;
}
The full docs are here, hope that helps :)
from nylo.
Hello @agordn52,
Yes, and during the debug, it is being invoked. However, refreshToken
isn't invoked and all I have no clue why.
@override
Future<RequestHeaders> setAuthHeaders(RequestHeaders headers) async {
AuthTokens? tokens = await StorageKey.userToken.read<AuthTokens>();
if (tokens?.accessToken != null) {
headers.addBearerToken(tokens?.accessToken ?? "");
}
return headers;
}
@override
Future<bool> shouldRefreshTokens() async {
try {
AuthTokens? tokens = await StorageKey.userToken.read<AuthTokens>();
if (tokens?.accessToken == null) {
return false;
}
// If the token is expired, where expiredAt is epoch time
// add 15 seconds to the expiry time to account for latency
if (tokens?.expiresAt != null &&
DateTime.now().isAfter(
tokens?.expiresAt!.subtract(Duration(seconds: 15)) ??
DateTime.now())) {
return true;
} else {
return false;
}
} catch (e) {
print(e);
return false;
}
}
@override
refreshToken(Dio dio) async {
// debug is not hitting this function, nor printing the below.
print("refreshing token");
AuthTokens? tokens = await StorageKey.userToken.read<AuthTokens>();
print(tokens?.refreshToken);
if (tokens?.refreshToken == null) {
await Auth.logout();
routeToInitial(navigationType: NavigationType.pushAndForgetAll);
return;
}
dynamic response = (await dio.post(baseUrl + "/auth/refresh",
options: Options(
headers: {"Authorization": "Bearer ${tokens?.refreshToken}"})))
.data();
// Save the new token
await StorageKey.userToken.store(response);
}
from nylo.
Hi @KhaledAlMana,
Thanks for sharing your code.
I see a few things wrong that should be changed, please try the below 👍
@override
Future<RequestHeaders> setAuthHeaders(RequestHeaders headers) async {
AuthTokens? tokens = await StorageKey.userToken.read<AuthTokens>();
if (tokens?.accessToken != null) {
headers.addBearerToken(tokens?.accessToken ?? "");
}
return headers;
}
@override
Future<bool> shouldRefreshTokens() async {
AuthTokens? tokens = await StorageKey.userToken.read<AuthTokens>();
if (tokens?.accessToken == null) {
return true; // if accessToken is null then you should want to get a new 'AuthTokens'
}
// If the token is expired, where expiredAt is epoch time
// add 15 seconds to the expiry time to account for latency
if (DateTime.now().isAfter(tokens?.expiresAt?.subtract(Duration(seconds: 15)) ?? DateTime.now())) {
return true;
}
return false;
}
@override
refreshToken(Dio dio) async {
// debug is not hitting this function, nor printing the below.
print("refreshing token");
AuthTokens? tokens = await StorageKey.userToken.read<AuthTokens>();
print(tokens?.refreshToken);
if (tokens?.refreshToken == null) {
await Auth.logout();
routeToInitial(navigationType: NavigationType.pushAndForgetAll);
return;
}
dynamic response = (await dio.post(baseUrl + "/auth/refresh",
options: Options(
headers: {"Authorization": "Bearer ${tokens?.refreshToken}"})))
.data; // data() not a function, use data instead
// Save the new token
await StorageKey.userToken.store(response);
}
from nylo.
I'm going to close this as resolved.
If you need help understanding networking, the docs on nylo.dev are up-to-date so you'll be able to learn more 👍
from nylo.
Related Issues (20)
- widget.data() still have data HOT 8
- Checking auth token externally before visiting initial route HOT 1
- BaseOptions override does not work for request timeout HOT 2
- Edit item in collection ? HOT 3
- Back gesture not working in nylo HOT 2
- onGenerateRoute + ResponsiveFramework HOT 4
- How to override Dio baseoptions? HOT 3
- NyTextField HOT 1
- network localhost refused HOT 2
- Router extend HOT 2
- localization is broken HOT 1
- validation not working when extends NyPage HOT 1
- build fails in nylo 5.11.0 HOT 2
- Not able to use nylo or not able to flutter pub get
- Build failed TextScaler not found error HOT 1
- LocalDataException when building new Nylo project HOT 15
- A guide for i implementing websocket on the framework HOT 1
- Auth Guard not working HOT 3
- how to enable other devices in the project
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nylo.