Untitled

mail@pastecode.io avatar
unknown
dart
17 days ago
1.5 kB
1
Indexable
Never
Future<String> handleRefreshToken(String token) async {
    if (authBloc.userDataModel != null) {
      final DateTime expiresToken =
          DateTime.parse(authBloc.userDataModel!.expiresToken);
      final DateTime date2 = DateTime.now().toUtc();
      final bool difference = date2.difference(expiresToken).isNegative;
      if (!difference) {
        Map<String, dynamic> body = <String, dynamic>{
          'refreshToken': authBloc.userDataModel!.refreshToken
        };
        final Response response = await http.post(
          Uri.parse(Urls.refreshToken),
          body: json.encode(body),
          headers: headersRequest,
        );
        if (response.statusCode == 401 &&
            response.body ==
                '{"code":401,"message":"user is not allowed to login by the system login policy"}') {
          blockUser();
        }
        Map<String, dynamic> newToken =
            json.decode(response.body) as Map<String, dynamic>;
        debugPrint(newToken.toString());
        authBloc.userDataModel = UserData.copyWith(
          oldData: authBloc.userDataModel!,
          accessToken: newToken['access']['token'] as String,
          expiresToken: newToken['access']['expires'] as String,
          refreshToken: newToken['refresh']['token'] as String,
        );
        await authBloc.persistUser(user: authBloc.userDataModel!);
        return newToken['access']['token'] as String;
      }
      return token;
    } else {
      return token;
    }
  }
Leave a Comment