Skip to content
New issue

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

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

Already on GitHub? # to your account

Feature Request: [authentication-client] event on auth failure #1858

Closed
KrishnaPG opened this issue Mar 13, 2020 · 3 comments
Closed

Feature Request: [authentication-client] event on auth failure #1858

KrishnaPG opened this issue Mar 13, 2020 · 3 comments

Comments

@KrishnaPG
Copy link

KrishnaPG commented Mar 13, 2020

In the authentication-client, when auth attempt is made, login and authenticated events are generated for the auth success case:

this.authenticated = true;
this.app.emit('login', authResult);
this.app.emit('authenticated', authResult);

However, there is no event for auth failure case. Only the promise is rejected.

This feature-request is: please raise an event (something like authFailure event) in the failure case, in the catch handler shown below:

}).catch((error: FeathersError) =>
this.handleError(error, 'authenticate')
);

Such event makes it possible to watch for the login and authFailure events from anywhere in the app across different services and enable/disable certain functionality. Currently, we are handling success in the event, and failure in the catch handler, not so elegant.

fClient.on('login', authResult => Store.setUser(authResult));
fClient.on('logout', () => Store.clearUser());
fClient.on('authFailure', () => Store.clearUser()); //<-- this is not available currently
@daffl
Copy link
Member

daffl commented Mar 13, 2020

This is already possible with a short client side error hook:

fClient.service('authentication').hooks({
  error (context) {
    fClient.emit('authFailure', context.error);
  }
});

@KrishnaPG
Copy link
Author

Thank you @daffl

When using .authenticate() the hook seems to be getting invoked fine. But when doing .reAuthenticate(true) the hook is not getting invoked for some reason.

For example,

    fClient.reAuthenticate(true).catch(ex => {
      console.log("caught exception: ", ex);
    });

fClient.service('authentication').hooks({
	error(context) {
		console.log("auth error ", context.error);
	}
});

the catch exception handlers gets invoked, but not the hook for reAuth failure when validating an already stored JWT.

That is the main scenario I am trying to handle.

When socket reconnections happen the login event is generated for successful reAuth (see below handleSocket code). But when the stored JWT is failed to reAuth on reconnect, we would not know

// Only reconnect when `reAuthenticate()` or `authenticate()`
// has been called explicitly first
// Force reauthentication with the server
.then(() => this.authenticated ? this.reAuthenticate(true) : null);

@daffl
Copy link
Member

daffl commented Mar 28, 2020

Ah, indeed, the correct code is

fClient.hooks({
  error (context) {
    const { error } = context;

    if (error.code === 401) {
      client.emit('authFailure', error);
    }

    return context;
  }
});

This should emit the event on any authentication failure.

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

No branches or pull requests

2 participants