Festify / cordova-spotify-oauth

🔐 Easy Spotify authentication for Cordova / PhoneGap apps
MIT License
16 stars 18 forks source link

'auth_failed'. Potential local storage issue? #7

Closed ghost closed 6 years ago

ghost commented 6 years ago

Environment Details: Hardware: MacBok Air (2017) Node: v6.11.4 Ionic: 3.18.0 Cordova: 7.1.0 Cordova-spotify-oauth: 0.1.10 Side menu template, scaffolded app.

Hi Guys, My AWS lambda functions are working fine and I can see they are returning the access token, however, I was receiving 'undefined' when trying to log it or bind it to the display for testing.

I put a catch on the cordova.plugins.spotifyAuth.authorize(config) call and was able to log the following error:

console.log:
            {"line":2,"column":5224,"sourceURL":"http://192.168.0.11:8101/plugins/cordova-spotify-oauth/www/build/spotify-oauth.min.js","name":"auth_failed","__zone_symbol__currentTask":{"type":"microTask","state":"notScheduled","source":"Promise.then","zone":"angular","cancelFn":null,"runCount":0}}

Having a look in the spotify-oauth.ts file, I can see that this error can be caused if the scopes arrays don't match (I believe I ruled this out quite quickly), or if there is no SpotifyOAuthData in localStorage (the line / column numbers seemed to match) ...

In the catch, I tried to carry out a window.localStorage.getItem('SpotifyOAuthData') and It doesn't look like any data has been persisted to the localStorage. I presumed then that this could be environmental and tested the same application under Windows using the Android emulator. The token was displayed on screen as expected...

I tested localStorage myself, in a similar way. I was able to set an Item and get the same item, displaying it on screen successfully using window.localStorage. I'm starting to believe it is an issue on the iOS plugin but I can't quiet pinpoint why the data is not being set.

I'm going to try a few more things tomorrow. But thought I'd post this to see if you guys had encountered any similar issues or had any ideas for resolution.

Any help appreciated.

App.ts

initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.statusBar.styleDefault();
      this.splashScreen.hide();

      const config = {
        clientId: "xxxxxxxx",
        redirectUrl: "vimana://callback",
        scopes: [], // have tried with 'streaming' and null.
        tokenExchangeUrl: "https://xxxxxxx-api.eu-central-1.amazonaws.com/dev/exchange",
        tokenRefreshUrl: "https://xxxxxxxxx-api.eu-central-1.amazonaws.com/dev/refresh",
      }

      cordova.plugins.spotifyAuth.authorize(config)
      .then(({ accessToken, expiresAt }) => {
        this.events.publish('spotify:tokensaved', accessToken, expiresAt);
      })
      .catch(error => {
        console.log(JSON.stringify(error));
      });

    });
  }`

Home.ts

export class HomePage {

  token: any;

  constructor(public events: Events, public navCtrl: NavController) {
    events.subscribe('spotify:tokensaved', (accessToken, expiresAt) => {
      // user and time are the same arguments passed in `events.publish(user, time)`
      console.log(accessToken);
      console.log(expiresAt);

      this.token = accessToken;

    });
  }

}

Home.html

<ion-content padding>
  Your token is: {{ token }}
</ion-content>
NeoLegends commented 6 years ago

I think its unlikely that localStorage might be the issue here. Could you check the native iOS code instead? At least with raw cordova, you can start your app from within XCode and place breakpoints inside the plugin code. This way, you can find out the errors and we're going to get them fixed.

ghost commented 6 years ago

Sure can @NeoLegends! It might be tomorrow before I get the chance but I'll post back any findings.

Thanks for your quick reply.

NeoLegends commented 6 years ago

Any details why you closed this?

ghost commented 6 years ago

Hi @NeoLegends, I've been having a few issues getting the app to build in XCode, to get to the point of being able to debug the plugin as stated above (issues that are unrelated).

I have tested the plugin on my Mac on Android and didn't work either (it's working fine in Android on my windows machine). The common denominator is my macbook air. I believe the issues are related to my development environment. Not the plugin.

I'm changing direction, scaffolding a new app and potentially changing the auth mechanism. If I do re-add the plugin and get the app up, I'll comment on this post.

brianeroth commented 6 years ago

@NeoLegends @chrissysemens Hey guys, I'm seeing this issue on my end too (same error from the authorize function). I checked localStorage and nothing is being set and the promise is rejected in saveAndHandleErrors. Using the iOS emulator through Xcode. Any ideas?

NeoLegends commented 6 years ago

Same idea: could you run your app with the XCode debugger attached and step though the plugin code and check for anything weird in the console output?

mahnuh commented 6 years ago

I have been struggling with this issue and fortunately was able to figure it out.

My best guess is that you are using WKWebView and therefor are subjected to CORS. Setting Access-Control-Allow-Origin: '*' for the responses (I am running my own token exchange service) solved it for me, everything is working as expected now.

NeoLegends commented 6 years ago

We're not using WKWebView specifically, but since we're firing HTTP requests from within the Cordova Context (WKWebView or UIWebView, not sure), we're subject to CORS. I'll add a note to the README, that your server must support CORS for this library to work. :)