Open Closed

Angular can't read AccessToken when encryption enabled #9138


User avatar
0
adhamawadhi created

Angular app first time login is work fine, but when refresh the page there is an error occured due to reading AccessToken which is encrypted in line remember-me.service.ts

ERROR SyntaxError: Unexpected token 'õ', "õ¿‹Ø]&¶`"... is not valid JSON
    at JSON.parse (<anonymous>)

Steps to reproduce

  • Enable OpenIddict Token Encryption
  • Login in angular for first time (it wokrs fine)
  • Open explorer Console devleoper
  • Refresh the page ( press F5)
  • you can see Exception in Consol tab

Suggestion to Solve I can see the id_token is available to read same as access_token when encryption enabled. Try read access_token when error occured read id_token if exists.


6 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    Have you set the DisableAccessTokenEncryption = false in the authsever project?

    Thanks.

    https://abp.io/support/questions/6562/How-to-implement-the-token-encryption-in-OpenIdDict#answer-3a105090-f2eb-80e3-6fae-d3b99bdca2ce

  • User Avatar
    0
    adhamawadhi created

    Yes, and it works fine. But I need it encrypted

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    ok, I will ask our angular Team.

    Thanks.

  • User Avatar
    0
    sumeyye.kurtulus created
    Support Team Angular Expert

    Hello, it currently works as expected as you have mentioned. However, if encryption is required, you’ll need to apply a workaround until we release an official fix for that.

    In the meantime, you can override the related service just to skip the error. Here’s a suggested approach:

    // new-remember-me.service.ts
    export class NewRememberMeService extends RememberMeService {
      readonly #rememberMe = 'remember_me';
    
      override getFromToken(accessToken: string): boolean {
        const tokenBody = accessToken.split('.')[1].replace(/-/g, '+').replace(/_/g, '/');
        try {
          const decoded = JSON.parse(atob(tokenBody));
          return Boolean(decoded[this.#rememberMe]);
        } catch {
          return false;
        }
      }
    }
    

    You need to add this provider to the app.module.ts :

    { provide: RememberMeService, useClass: NewRememberMeService },
    

    We appreciate your patience and cooperation while we work on a permanent solution.

  • User Avatar
    0
    adhamawadhi created

    OK, thanks for your help.

  • User Avatar
    0
    sumeyye.kurtulus created
    Support Team Angular Expert

    I'm glad to hear the workaround resolved the issue.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
Do you need assistance from an ABP expert?
Schedule a Meeting
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.3.0-preview. Updated on April 16, 2025, 12:13