Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AcquireTokenSilent not working #45

Closed
skyshine999 opened this issue Oct 23, 2019 · 12 comments
Closed

AcquireTokenSilent not working #45

skyshine999 opened this issue Oct 23, 2019 · 12 comments

Comments

@skyshine999
Copy link

skyshine999 commented Oct 23, 2019

I am getting response after successful login after that i am killing my app and try to do silent login it is navigating to user interaction login. Am i doing anything wrong
code:

import AzureAuth from 'react-native-azure-auth';

const options = {
    clientId: ‘XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’,
    tenant: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    redirectUri: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’://auth',
    persistentCache:true,
    authorityUrl:'https://login.microsoftonline.com/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’/oauth2/v2.0/'
}
const azureAuth = new AzureAuth(options);
const custom_scope =["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX’"];


class AzureAuthLogin {

    static getToken = async() => {
        try {
            let token = await azureAuth.webAuth.authorize({scope: custom_scope[0]})
            return token
        } catch (error) {
            console.log(error)
            return error
        }
    }
    static getSilentToken = async (tokenUserId) => {
        try {
            // Try to get cached token or refresh an expired ones
            let token = await azureAuth.auth.acquireTokenSilent({ userId: tokenUserId,scope: custom_scope[0]})
            if (!token) {
                // No cached tokens or the requested scope defines new not yet consented permissions
                // Open a window for user interaction
                token = await azureAuth.webAuth.authorize({scope: custom_scope[0]})
                return token
            }
            return token
        } catch (error) {
            console.log(error)
            return error
        }
    }

    
}

export default AzureAuthLogin;
@vmurin
Copy link
Owner

vmurin commented Oct 23, 2019

The code is truly OK. But I don't see the calls of this methods. Probably you always call getToken before getSilentToken
By the way - the cache is persistent by default.

Did you check the API access with obtained access token? Possibly you do not get the right token at all? Some app registration issues could be the reason.

Are you on android or iOS? I can check only android version and the persistent cache in the sample app works as intended. The AsyncStorage is actually cross platform feature of RN, but theoretically it could have some iOS issues.

Please check all the points above and come back with report :)

@skyshine999
Copy link
Author

skyshine999 commented Oct 23, 2019

I am testing in iOS, I am calling getToken then it is showing Microsoft login page and i am getting response like this

accessToken: “XXXXXXXXXXX”

clientId: “XXXXXXXXXXXX”

expireOn: XXXXXXXXXXX

idTokenExpireOn: XXXXXXXXX

rawIdToken: “XXXXXXXXX
”
scope: Scope {scopeStr: “XXXXXXXXX”}

tenantId: “XXXXXXXXXXXXXXXX”

userId: “XXXXXXXXXX”

userName: “XXXXXXXXX”


After that i am killing my app try calling silent token it is again showing microsoft login page and i am doing login then it is navigating to home page.From second time onwards silent token is working fine only first time silent token is navigating to login page.
I am doubting that in async storage token is not storing

@vmurin
Copy link
Owner

vmurin commented Oct 23, 2019

I suppose you encountered some AsyncStorage issue. Are you on the last RN version?

Please check related github issues and solutions proposed there.
For example:
react-native-async-storage/async-storage#158
react-native-async-storage/async-storage#89

Actually you can test AsyncStorage independent from AzureAuth library. Try to instantiate AS and save and retrieve some value.

@skyshine999
Copy link
Author

Yes i am using react native latest version
Ok i will check async storage

@vmurin
Copy link
Owner

vmurin commented Oct 23, 2019

You did not listed refresh token in your response. It should be there. The scope offline_access needed to get refresh token is always added automatically. If you did not get it - something goes wrong. Probably you app registration has some restrictions...

@skyshine999
Copy link
Author

I debugged Azure auth class, In my response refresh token is there will it cause any problem

@vmurin
Copy link
Owner

vmurin commented Oct 23, 2019

I debugged Azure auth class, In my response refresh token is there will it cause any problem

OK. It should be there, so here is everything right! ;)

@skyshine999
Copy link
Author

I checked again async storage is working fine.
1.First time getting response after successful login
2.Killed the app tried silent login then it is asking Yes or no for project then it is navigating to home
3.Killed the app tried silent login, It is working
Second time onwards silent login is working fine

@vmurin
Copy link
Owner

vmurin commented Oct 24, 2019

Hi,

in the issue #46 you wrote

working fine in ios

Does it mean you have now solved this cache issue?
Please update the issue tracker with your solution or found bug.

@skyshine999
Copy link
Author

Due to our company policy in azure it is asking again login

@elad101
Copy link

elad101 commented Nov 27, 2023

@vmurin when I called acquireTokenSilent, my tokenExpireDate wasn't extended.
Is it related to the App Reg configuration?
In addition, it was mentioned on other Azure auth packages forceRefresh attribute.
Why don't you support that?

@Monisankarnath
Copy link

Monisankarnath commented Jan 30, 2024

Hi @vmurin, I am facing the same issue. If I try to login using webAuth.authorize I am able to login by adding the email and password in the b2c login and get the response, after that if I try to login using auth.acquireTokenSilent I get an error - [TypeError: Cannot read property 'accessToken' of null]. Now, if I try webAuth.authorize again I dont need to add email and password and it directly allows me to login after a popup.

Both the getAccessToken and getRefreshToken are returning null.

Package versions -

  • "@react-native-async-storage/async-storage": "^1.21.0", // auto linked if I import AsyncStorage, tested separately working as expected
  • "react-native-azure-auth": "^1.8.9",
  • "react-native": "0.68.5",
    I am facing this issue in ios for now. Didn't test it in android. Please help me here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants