Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Fix csrf token in example #1

Merged
merged 6 commits into from
Oct 21, 2022
Merged

Fix csrf token in example #1

merged 6 commits into from
Oct 21, 2022

Conversation

AlexV525
Copy link
Member

@AlexV525 AlexV525 commented Oct 20, 2022

Picked from cfug/dio#1262

New Pull Request Checklist

  • I have read the Documentation
  • I have searched for a similar pull request in the project and found none
  • I have updated this branch with the latest master to avoid conflicts (via merge from master or rebase)
  • I have added the required tests to prove the fix/feature I am adding
  • I have updated the documentation (if necessary)
  • I have run the tests and they pass

This merge request fixes/refers to the following issues: example/lib/queued_interceptor_csrftoken.dart.

Pull Request Description

Summary

  • added getter methods in the Response class for better usage of statusCode
  • removed callbacks
  • replaced callbacks(by using .then( ) and .catchError( )s) with async/await
  • reflected the changes in README.md

Issues

  1. in the example(example/lib/queued_interceptor_csrftoken.dart) the _onResult method was not being called, since the example baseUrl ('http://www.dtworkroom.com/doris/1/2.0.0/') was not able to run
  2. at line 34 in the example, the token is already expired because the status code was 401. And also in onError closure, after the token has been updated then the dio asks for fetch and resolve it.
    So I thought the below code might be an extra task because the dio will update the token and resolve the request.
// If the token has been updated, repeat directly.
if (csrfToken != options.headers['csrfToken']) {
  options.headers['csrfToken'] = csrfToken;
  // repeat
  dio.fetch(options).then(
    (r) => handler.resolve(r),
    onError: handler.reject,
  );
  return;
}

Opinion

  • for better practice, using async/await can be more helpful than using in a synchronous way

@AlexV525 AlexV525 merged commit 2db69d5 into main Oct 21, 2022
@AlexV525 AlexV525 deleted the fix-csrf-token-in-example branch October 21, 2022 08:42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants