简体   繁体   中英

Dart/Flutter Error: A value of type 'AuthSession' can't be assigned to a variable of type 'CognitoAuthSession'

Our application requires to get the user role from Cognito , to solve this we followed the this comment but the linter (from Android Studio ) and the running time show us the next bug:

[+10900 ms] [+10852 ms] lib/auth/infrastructure/auth.repository.dart:61:43: Error: A value of type 'AuthSession' can't be assigned to a variable of type 'CognitoAuthSession'.
[   +3 ms] [   +2 ms]  - 'AuthSession' is from 'package:amplify_auth_plugin_interface/src/Session/AuthSession.dart' ('/opt/flutter/.pub-cache/hosted/pub.dartlang.org/amplify_auth_plugin_interface-0.1.1/lib/src/Session/AuthSession.dart').
[        ] [        ]  - 'CognitoAuthSession' is from 'package:amplify_auth_cognito/src/CognitoSession/CognitoAuthSession.dart' ('/opt/flutter/.pub-cache/hosted/pub.dartlang.org/amplify_auth_cognito-0.1.1/lib/src/CognitoSession/CognitoAuthSession.dart').
[        ] [        ]       CognitoAuthSession fetchedSession = await _authCategory.fetchAuthSession(
[        ] [        ]                                           ^

The next is the repository's code where we are using Amplify Auth :

import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify.dart';
import 'package:amplify_flutter/categories/amplify_categories.dart';

class AmplifyAuthRepository implements AuthRepository {
  AmplifyAuthRepository({AuthCategory? authCategory})
      : _authCategory = authCategory ?? Amplify.Auth;

  final AuthCategory _authCategory;

@override
  Future<Map<String, dynamic>> fetchSession() async {
    try {
      CognitoAuthSession fetchedSession = await _authCategory.fetchAuthSession(
          options: CognitoSessionOptions(getAWSCredentials: true));
      String token = fetchedSession.userPoolTokens.idToken;
      Map<String, dynamic> payload = Jwt.parseJwt(token);
      // Access the groups
      List groups = payload['cognito:groups'];
      Map<String, dynamic> result = {
        'isSignedIn': fetchedSession.isSignedIn,
        'roles': groups
      };
      return result;
    } on AmplifyException catch (error) {
      // TODO: Catch error for analytics, not required for frontend.
      throw AmplifyException(error.toString());
    }
  }
}

It must be a CognitoAuthSession type variable in order to extract the session token and with it, to get the user's role(as you can see on the aforementioned comment).

For more details please visit this issue .

Flutter (Channel stable, 2.0.4, on Linux, locale en_US.UTF-8)
  • Flutter version 2.0.4 at /opt/flutter
  • Framework revision b1395592de (7 days ago), 2021-04-01 14:25:01 -0700
  • Engine revision 2dce47073a
  • Dart version 2.12.2

May i be using this wrong?

Thanks.

cast await _authCategory.fetchAuthSession response to CognitoAuthSession.

CognitoAuthSession fetchedSession =
        await _authCategory.fetchAuthSession(options: CognitoSessionOptions(getAWSCredentials: true)) as CognitoAuthSession;
    print('fetchedSession ${fetchedSession.userPoolTokens?.idToken}');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM