Skip to content

Commit

Permalink
[#31] fix can not get token
Browse files Browse the repository at this point in the history
  • Loading branch information
nkhanh44 committed Aug 23, 2023
1 parent 9ad57d3 commit bda39bb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
5 changes: 2 additions & 3 deletions lib/api/data_sources/token_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ class TokenDataSourceImpl extends TokenDataSource {
);
return apiToken;
}

return await _secureStorage.getValue(key: SecureStorageKey.apiToken)
as ApiToken;
ApiToken apiToken = await _secureStorage.getValue<ApiToken>(key: SecureStorageKey.apiToken);
return apiToken;
}

@override
Expand Down
2 changes: 0 additions & 2 deletions lib/api/interceptor/auth_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ class AuthInterceptor extends Interceptor {
RequestOptions options,
RequestInterceptorHandler handler,
) async {
print("@@@ get 1");
final token = await _tokenDataSource.getToken();
print("@@@ get here $token");
options.headers.putIfAbsent(
_headerAuthorization, () => "${token.tokenType} ${token.accessToken}");
super.onRequest(options, handler);
Expand Down
1 change: 0 additions & 1 deletion lib/repositories/authentication_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class AuthenticationRepositoryImpl extends AuthenticationRepository {
grantType: _grantType,
));
await _tokenDataSource.setToken(response.toApiToken());
print("@@@ settoken ${response.toApiToken()}");
return response.toLoginModel();
} catch (exception) {
throw NetworkExceptions.fromDioException(exception);
Expand Down
15 changes: 9 additions & 6 deletions lib/storage/secure_storage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@ import 'dart:convert';

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:survey_flutter/model/api_token.dart';
import 'package:survey_flutter/storage/secure_storage.dart';

import '../di/provider/flutter_secure_storage.dart';

final secureStorageProvider = Provider<SecureStorage>((_) {
return SecureStorageImpl(FlutterSecureStorageProvider().getStorage());
});

class SecureStorageImpl extends SecureStorage {
final FlutterSecureStorage _storage;
SecureStorageImpl(this._storage);

@override
Future<M> getValue<M extends SecureStorageModel>(
{required SecureStorageKey key}) async {
Future<M> getValue<M extends SecureStorageModel>({required SecureStorageKey key}) async {
final rawValue = await _storage.read(key: key.string);
if (rawValue == null) {
throw SecureStorageError.failToGetValue;
}
final jsonValue = jsonDecode(rawValue);

return await jsonDecode(rawValue);
if (M == ApiToken) {
return ApiToken.fromJson(jsonValue) as M;
} else {
throw ArgumentError('Invalid SecureStorageModel type');
}
}

@override
Future<void> save<M extends SecureStorageModel>(
{required M value, required SecureStorageKey key}) async {
Future<void> save<M extends SecureStorageModel>({required M value, required SecureStorageKey key}) async {
final encodedValue = jsonEncode(value);
await _storage.write(key: key.string, value: encodedValue);
}
Expand Down

0 comments on commit bda39bb

Please sign in to comment.