sday
sday

Reputation: 1051

feathers authenticate() How do I use it without the api call failing with no parameters?

I put together a simple Aurelia app that attempts to authenticate, which works correctly, except when you refresh the browser and the constructor for the app checks to see if we are already authenticated, the API call fails. I would expect the catch in the API call to return notification that the user is either not logged in already, or that the user is in fact already logged in based on a locally stored token.

From the docs: https://docs.feathersjs.com/authentication/client.html

app.authenticate() attempts to authenticate with the server using the data you passed it. If you don't provide any options it will attempt to authenticate using a token stored in memory or in your storage engine. It returns a promise.

Here is the complete test app I'm using to test this. https://gitlab.com/sday/feathers-test

Here is the constructor code that appears to fail from: https://gitlab.com/sday/feathers-test/blob/master/src/app.js

constructor() {
    var parent=this;
    const socket = io('http://localhost:3030');
    this.f = feathers()
      .configure(feathers.socketio(socket))
      .configure(feathers.hooks())
      .configure(feathers.authentication({ storage: window.localStorage }));

    // This appears to be a problem.  If I am in fact already logged in, the callback is made, the token and user object is returned.
    // If the user is not logged in the catch isn't called, but an error is generated in the browser console.
    // Error: Could not find stored JWT and no authentication type was given
    // The docs indicate one should be able to call authenticate() without parameters to validate a current session.
    this.f.authenticate().then(function(result){
      console.log('Already Authenticated!', parent.f.get('token'));
      parent.email=parent.f.get('user').email;
      console.log("User:",parent.f.get('user'));
      parent.authenticated=true;
    }).catch(function(error){
      console.error('Not authenticated!', error);
      parent.authenticated=false;
    });
  }

enter image description here

UPDATE

Well, a case of staring at the screen too long. It is working as expected. It is in the catch when the user is not logged in. I was starring at the red thinking this was an exception rather than a console.error call inside the catch. ugh

Upvotes: 0

Views: 663

Answers (1)

Callum Linington
Callum Linington

Reputation: 14417

Your exception is actually being printed by your catch logic:

}).catch(function(error){
  console.error('Not authenticated!', error);
  parent.authenticated=false;
});

Your exception in the chrome debugger starts with Not Authenticated!.

Upvotes: 1

Related Questions