AzureAD / azure-activedirectory-library-for-js

The code for ADAL.js and ADAL Angular has been moved to the MSAL.js repo. Please open any issues or PRs at the link below.
https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/maintenance/adal-angular
Apache License 2.0
627 stars 374 forks source link

Wrong token (user logged in -> User login is required) #758

Closed Taerarenai closed 6 years ago

Taerarenai commented 6 years ago

The title pretty much sums it up. Im using adal token to access a specific api. It all works as it should, unless i edit the JS (i added Loading Overlay so it shows a loading icon until the api call is Done). After that, i just get ->

user already logged in
ADAL error occurred: User login is required
Calling API User failed.Unauthorized

Im guessing its unauthorized because the token is wrong (cached token is exactly the same, but im still new to this so thats how it should be perhaps?).

My only fix so far is to purposely break the script (token) by calling the wrong appid here ->

authContext.acquireToken(authContext.config.clientId, function (error, token) { By calling clientId (instead of the id of the app that we need to auth against) it gets into a refresh loop (trying to login i suppose) and thus removes the token. After that i can just repair the script with the correct id and it works.

Isnt there another way to remove the unwanted token and refresh? (afaik adal should refresh, but does that work if the token is not the one the app is after or just if theres no token/expired?)

I'm asking this because it would be rather impossible to fix all users problems by doing what im doing locally, therefore i need a way to remove the token if its not working and get a new one.

Adal token is set with cacheLocation: 'localStorage' (from what ive read it is needed for IE and we still have people using IE).

I'm using adaljs 1.0.17 I hope i didnt confuse you too much. Thanks in advance.

Taerarenai commented 6 years ago

Checking the user solved it.

    // Check For & Handle Redirect From AAD After Login
    var isCallback = authContext.isCallback(window.location.hash);
    authContext.handleWindowCallback();
    console.log(authContext.getLoginError());

    if (isCallback && !authContext.getLoginError()) {
        window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
    }

    // Check Login Status, Update UI
    var user = authContext.getCachedUser();
    if (user) {
            console.log("User found");

    } else {
        console.log("error");
       authContext.config.redirectUri = window.location.href;
       authContext.login();
    }