aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

V6 refreshSession is deprecated #12724

Closed pablovargasosorio closed 10 months ago

pablovargasosorio commented 10 months ago

Before opening, please confirm:

JavaScript Framework

Angular

Amplify APIs

Authentication

Amplify Categories

auth

Environment information

``` System: OS: macOS 14.2 CPU: (8) arm64 Apple M1 Pro Memory: 66.03 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 18.18.2 - ~/.nvm/versions/node/v18.18.2/bin/node npm: 9.8.1 - ~/.nvm/versions/node/v18.18.2/bin/npm Browsers: Chrome: 120.0.6099.109 Edge: 120.0.2210.77 Safari: 17.2 npmPackages: @angular-devkit/build-angular: ^16.2.8 => 16.2.10 @angular/animations: ^16.2.11 => 16.2.12 @angular/cdk: ^16.2.11 => 16.2.12 @angular/cli: ^16.2.8 => 16.2.10 @angular/common: ^16.2.11 => 16.2.12 @angular/compiler: ^16.2.11 => 16.2.12 @angular/compiler-cli: ^16.2.11 => 16.2.12 @angular/core: ^16.2.11 => 16.2.12 @angular/forms: ^16.2.11 => 16.2.12 @angular/localize: ^16.2.11 => 16.2.12 @angular/platform-browser: ^16.2.11 => 16.2.12 @angular/platform-browser-dynamic: ^16.2.11 => 16.2.12 @angular/router: ^16.2.11 => 16.2.12 @auth0/angular-jwt: ^5.2.0 => 5.2.0 @cypress/angular: 0.0.0-development @cypress/mount-utils: 0.0.0-development @cypress/react: 0.0.0-development @cypress/react18: 0.0.0-development @cypress/schematic: ^2.5.1 => 2.5.1 @cypress/svelte: 0.0.0-development @cypress/vue: 0.0.0-development @cypress/vue2: 0.0.0-development @fortawesome/angular-fontawesome: ^0.13.0 => 0.13.0 @fortawesome/fontawesome-free: ^6.4.2 => 6.5.1 @fortawesome/fontawesome-svg-core: ^6.4.2 => 6.5.1 @fortawesome/free-brands-svg-icons: ^6.4.2 => 6.5.1 @fortawesome/free-regular-svg-icons: ^6.4.2 => 6.5.1 @fortawesome/free-solid-svg-icons: ^6.4.2 => 6.5.1 @ng-bootstrap/ng-bootstrap: ^15.1.2 => 15.1.2 @ngrx/effects: ^16.3.0 => 16.3.0 @ngrx/entity: ^16.3.0 => 16.3.0 @ngrx/router-store: ^16.3.0 => 16.3.0 @ngrx/store: ^16.3.0 => 16.3.0 @ngrx/store-devtools: ^16.3.0 => 16.3.0 @types/bootstrap: ^5.2.9 => 5.2.10 @types/canvas-confetti: ^1.4.3 => 1.6.4 @types/jasmine: ~3.8.0 => 3.8.2 @types/lodash: ^4.14.182 => 4.14.202 @types/node: ^12.11.1 => 12.20.55 (20.10.5, 18.19.3) @types/segment-analytics: ^0.0.34 => 0.0.34 @types/textarea-caret: ^3.0.3 => 3.0.3 @typescript-eslint/eslint-plugin: ^5.40.1 => 5.62.0 @typescript-eslint/parser: ^5.40.1 => 5.62.0 aws-amplify: ^6.0.6 => 6.0.7 aws-amplify/adapter-core: undefined () aws-amplify/analytics: undefined () aws-amplify/analytics/kinesis: undefined () aws-amplify/analytics/kinesis-firehose: undefined () aws-amplify/analytics/personalize: undefined () aws-amplify/analytics/pinpoint: undefined () aws-amplify/api: undefined () aws-amplify/api/server: undefined () aws-amplify/auth: undefined () aws-amplify/auth/cognito: undefined () aws-amplify/auth/cognito/server: undefined () aws-amplify/auth/server: undefined () aws-amplify/datastore: undefined () aws-amplify/in-app-messaging: undefined () aws-amplify/in-app-messaging/pinpoint: undefined () aws-amplify/push-notifications: undefined () aws-amplify/push-notifications/pinpoint: undefined () aws-amplify/storage: undefined () aws-amplify/storage/s3: undefined () aws-amplify/storage/s3/server: undefined () aws-amplify/storage/server: undefined () aws-amplify/utils: undefined () bootstrap: ^5.3.2 => 5.3.2 bootstrap-icons: ^1.11.1 => 1.11.2 canvas-confetti: ^1.9.1 => 1.9.2 chart.js: ^4.4.0 => 4.4.1 chart.js-auto: undefined () chart.js-helpers: undefined () cypress: ^13.1.0 => 13.6.1 cypress-localstorage-commands: ^2.2.1 => 2.2.5 eslint: ^8.0.0 => 8.56.0 eslint-config-prettier: ^8.5.0 => 8.10.0 eslint-plugin-html: ^7.1.0 => 7.1.0 eslint-plugin-tsdoc: ^0.2.14 => 0.2.17 gmail-tester: ^1.3.8 => 1.3.8 jasmine-core: ~3.8.0 => 3.8.0 karma: ~6.3.0 => 6.3.20 karma-chrome-launcher: ~3.1.0 => 3.1.1 karma-coverage: ~2.0.3 => 2.0.3 karma-coverage-coffee-example: 1.0.0 karma-jasmine: ~4.0.0 => 4.0.2 karma-jasmine-html-reporter: ~1.7.0 => 1.7.0 karma-spec-reporter: ^0.0.36 => 0.0.36 karma-verbose-reporter: 0.0.8 => 0.0.8 lodash: ^4.17.21 => 4.17.21 ng-circle-progress: ^1.7.1 => 1.7.1 ng2-charts: ^5.0.3 => 5.0.4 ng2-charts-schematics: 0.0.1 ngx-cookie-service: ^16.1.0 => 16.1.0 ngx-markdown: ^16.0.0 => 16.0.0 ngx-webstorage-service: ^5.0.0 => 5.0.0 patternomaly: ^1.3.2 => 1.3.2 prettier: 2.8.0 => 2.8.0 replace-in-file: ^6.2.0 => 6.3.5 rxjs: ^7.5.6 => 7.8.1 (6.6.7) rxjs/ajax: undefined () rxjs/fetch: undefined () rxjs/internal-compatibility: undefined () rxjs/operators: undefined () rxjs/testing: undefined () rxjs/webSocket: undefined () textarea-caret: ^3.1.0 => 3.1.0 tslib: ^2.6.2 => 2.6.2 (2.6.1, 1.14.1) typescript: ~5.1.3 => 5.1.6 zone.js: ~0.13.3 => 0.13.3 npmGlobalPackages: @aws-amplify/cli: 12.8.2 corepack: 0.19.0 npm: 9.8.1 ```

Describe the bug

We used to manually refresh the session token when expired. Using the V5 cognitoUser.refreshSession function and cognitoUser.signInUserSession.refreshToken

It seems there's no alternative in V6, and no documentation for this breaking change.

Expected behavior

Be able to refresh the session in V6

Reproduction steps

  1. Having a Angular project, there's an interceptor to handle 401 responses which tries to refresh the session, using the current refresh token.
  2. Upgrade amplify/auth to from V5 to V6

Code Snippet

This is the V5 unauthorized 401 interceptor code snippet:

intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    return next.handle(request).pipe(
      catchError((error: HttpErrorResponse) => {
        if (error.status === 401) {
          return this.refreshToken(request).pipe(
            switchMap((newToken) => {
              if (newToken) {
                return next
                  .handle(
                    request.clone({
                      headers: request.headers.set(
                        'Authorization',
                        `Bearer ${newToken}`
                      ),
                    })
                  )

[...]

refreshToken(request: HttpRequest<any>): Subject<any> {
  const subject = new Subject();
  Auth.currentAuthenticatedUser()
    .then((cognitoUser) => {
      const currentSession = cognitoUser.signInUserSession;
      cognitoUser.refreshSession(
        currentSession.refreshToken,
        (err: any, result: any) => {
          if (result && result.accessToken && result.accessToken.jwtToken) {
            subject.next(result.accessToken.jwtToken);
          } else {
            subject.next(false);
          }
          subject.complete();
        }
      );
      return subject;
    })
    .catch(() => {
      subject.next(false);
      subject.complete();
    });
  return subject;
}

I tried with the equivalent V6 getCurrentUser, but the response is not a 'cognitoUser' object. Also, I coudn't find any equivalent API method for refreshSession.

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

israx commented 10 months ago

hello @pablovargasosorio . Sorry for the inconvenience. Can you trying calling fetchAuthSession as follows fetchAuthSession({forceRefresh:true}) ? This option will bypass cache and refresh the current session.

cwomack commented 10 months ago

@pablovargasosorio, let us know if making that change to the API you're calling resolves your issue. There's documentation around this API that can be found here, but if you feel there's improvements we can make then we'd love to hear it!

pablovargasosorio commented 10 months ago

@israx @cwomack It seems that works for me, thank you very much! The API doc is really poor, migrating from v5 has been a real pain. I had to dig into the inner library logic... Fortunately, that was my last migration blocker. Thanks! ;)

cwomack commented 10 months ago

@pablovargasosorio, glad to hear you were able to get this resolved! Also appreciate the feedback about the migration from v5 and improvements we can make on the docs. If there's something that was particularly unclear, we'd love to have you create an issue on our amplify-docs repo (here) so that we can make the upgrade process more clear and smooth!