Open Closed

Angular UI - atob error while decoding access token (The string to be decoded is not correctly encoded) #9936


User avatar
0
ademaygun created
  • Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.
  • Steps to reproduce the issue:

In our ABP.io v8 Angular UI application, we are experiencing the following error when some users log in: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.

Upon investigation, we found that in the getFromToken function, the access token payload is decoded using atob. When the payload length is not a multiple of 4 (due to missing Base64 padding =), atob throws this error. This happens especially when the preferred_username claim contains an email (e.g., abc@de.com).

Current Code:(in remember-me.service.ts)

const tokenBody = accessToken.split('.')[1].replace(/-/g, '+').replace(/_/g, '/');
const parsedToken = JSON.parse(atob(tokenBody));

Suggested Fix: Add missing padding before calling atob:

while (tokenBody.length % 4 !== 0) {
  tokenBody += '=';
}

Alternatively, using the jwt-decode library in Angular would be a more reliable approach.


3 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • The error occurs because the JWT access token payload is Base64Url encoded, and when its length is not a multiple of 4, the atob function in JavaScript throws an error due to missing padding. This is a known issue when decoding JWTs manually.

    • To fix this in your ABP Angular application, you should add the necessary padding to the token payload before calling atob. Here is the recommended approach:

    const tokenBody = accessToken.split('.')[1].replace(/-/g, '+').replace(/_/g, '/');
    let paddedTokenBody = tokenBody;
    while (paddedTokenBody.length % 4 !== 0) {
      paddedTokenBody += '=';
    }
    const parsedToken = JSON.parse(atob(paddedTokenBody));
    
    • Alternatively, you can use a library like jwt-decode to handle the decoding, which automatically manages padding and decoding issues.

    • This issue and workaround have been discussed in the ABP support portal. The ABP team has acknowledged the problem and suggested similar workarounds until an official fix is released.

    Sources:


    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

  • User Avatar
    0
    ademaygun created

    Hi, I will apply the temporary workaround, and I hope you will provide a permanent solution as well.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    I will forward it to our Angular team.

    Your ticket has been refunded.

    Thanks.

    https://github.com/abpframework/abp/issues/23796

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on September 23, 2025, 10:47