question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom error status on AuthenticationStrategy

See original GitHub issue

Is your feature request related to a problem? Please describe. I’m developing a custom AuthenticationStrategy. It’s working fine on the optimistic case. But on my case I need to return custom errors message when a login fail, such as when the account is blocked.

But since the method authenticate could return only two types (User on success case, and false on fail case), I don’t have a way to send a detailed error message to frontend display.

Describe the solution you’d like We could change the authenticate to return something like that:

Promise<{
  success: true
  user: User
} |
{
  success: false
  error: string
}>

It’s pretty flexible and type safe.

Describe alternatives you’ve considered Another way, in order to not have break change, is changing the return type to Promise<User | false | string> - but it could be confused about what string means.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
michaelbromleycommented, Oct 15, 2020

Oh this is a bit of a pain, but because some types are shared by both the Admin and Shop APIs, they get generated in both the generated-types.ts file (Admin API) and generated-shop-types.ts file (Shop API). Then when you try to assign one to another TS complains. It can be quite tricky to solve (you’ll see some cases in the BaseAuthResolver where I needed to import both types and make a union of them). If you can’t figure it out, throw in an as any and add a TODO: correct typings and I’ll take a look. Don’t let it hold up the rest of the work.

1reaction
michaelbromleycommented, Oct 14, 2020

Yes, that would be helpful!

A few pointers:

I’d add a authenticationError: String! field to: https://github.com/vendure-ecommerce/vendure/blob/db1893a9bf46fc95f918e9f99db2e4472d5a6c9d/packages/core/src/api/schema/common/common-types.graphql#L201-L204

Then if you run the codegen script it will update the generated class in this file and the constructor will now expect an argument for the authenticationError value.

Then in the AuthService you’d add a typeof user === 'string' check here and pass the value to the error constructor: https://github.com/vendure-ecommerce/vendure/blob/db1893a9bf46fc95f918e9f99db2e4472d5a6c9d/packages/core/src/service/services/auth.service.ts#L57-L60

Also adding an e2e test to https://github.com/vendure-ecommerce/vendure/blob/master/packages/core/e2e/authentication-strategy.e2e-spec.ts would be great.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ruby on rails - How I can add a custom error key in Devise ...
The answer is to modify config/locales/devise.en.yml but you must add the settings, they are not there by default.
Read more >
How To Implement Custom Error Responses in Express - Auth0
Learn why you may need custom error responses on 4xx errors, ... The HTTP status code is a code number returned by the...
Read more >
hapi — How to Handle 404 Responses for Missing Routes
The code samples of this tutorial are up to date and work with hapi v17. Start the error handling journey with missing resources...
Read more >
Using strong-error-handler | LoopBack Documentation
If true , all errors are printed via console.error , including an array of fields (custom error properties) that are safe to include...
Read more >
Fastify authentication strategy - Daily.dev
The code above demonstrates the usage of the fastify-auth plugin. ... Let's start implementing our custom authentication strategy in the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found