refreshToken static method

Future<bool> refreshToken()

Refresh the auth token

Implementation

static Future<bool> refreshToken() async {
  try {
    bool tokenExpired = DashCliEnv.instance.env.isAuthTokenExpired;
    if (!tokenExpired) return true;
    wtLog.info('Refreshing token...');
    if (DashCliEnv.instance.env.isRefreshTokenExpired) {
      wtLog.error('Session expired. Please login again');
      logout();
      return false;
    }
    Response res = await BaseApi.getInstance.post(
      Uri.https(CliConstants.baseUrl, CliConstants.refreshTokenPath),
      headers: <String, String>{
        HttpHeaders.authorizationHeader:
            'Bearer ${DashCliEnv.instance.env.refreshToken}'
      },
    );
    if (res.statusCode >= 200 && res.statusCode < 300) {
      Map<String, dynamic> data =
          json.decode(res.body) as Map<String, dynamic>;
      DashCliEnv.addNew('access_token', data['access_token']);
      DashCliEnv.instance.env.authToken = data['access_token'];
      wtLog.log('Token refreshed successfully');
      return true;
    }
    return false;
  } catch (e) {
    wtLog.error(e.toString());
    return false;
  }
}