Skip to content
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

Request headers bug #72

Open
Abilay99 opened this issue Nov 30, 2022 · 2 comments
Open

Request headers bug #72

Abilay99 opened this issue Nov 30, 2022 · 2 comments
Labels
question Further information is requested

Comments

@Abilay99
Copy link

I can't refresh token because of i use accessToken for API refreshToken for refreshing token but _httpClient interceptors using Fresh. There are changed headers Authorization only for refreshToken. How to solve this problem?

@felangel
Copy link
Owner

felangel commented Dec 1, 2022

Can you share some sample code or a reproduction sample? I'm not sure I fully understand the problem you're facing, thanks!

@felangel felangel added the question Further information is requested label Dec 1, 2022
@Abilay99 Abilay99 closed this as completed Dec 2, 2022
@Abilay99 Abilay99 reopened this Dec 2, 2022
@Abilay99
Copy link
Author

Abilay99 commented Dec 2, 2022

class TranslateApiClient {
  TranslateApiClient({Dio? httpClient})
      : _httpClient = (httpClient ?? Dio())
    ..options.baseUrl = baseUrl
    ..interceptors.add(_fresh)
    ..interceptors.add(
      LogInterceptor(),
    )
    ..options.followRedirects=false
    ..options.validateStatus=((status) => status != null && status <= 505);

  final Dio _httpClient;

  static final _fresh = Fresh.oAuth2(
    tokenHeader: (token){
      if (token.accessToken == '') {
        return {};
      }
      return {
        'Authorization': '${token.tokenType} ${token.accessToken}',
      };
    },
    tokenStorage: CacheTokenMemory(),
    refreshToken: (token, client) async {
      client.options.headers.addAll(
        {
          'Authorization': '${token?.tokenType} ${token?.refreshToken}',
        },
      );
      try {
        final response = await client.post<dynamic>('/auth/refresh',);

        if(response.statusCode != 201 || response.statusCode != 200){
          throw RevokeTokenException();
        }

        final data = response.data as Map<String, dynamic>;
        final accessToken = data['access_token'].toString();
        final refreshToken = data['refresh_token'].toString();

        return OAuth2Token(
          accessToken: accessToken,
          refreshToken: refreshToken,
        );
      } on DioError catch(err){
        print(err.message);
      }
      return const OAuth2Token(
        accessToken: '',
      );
    },
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants