Custom error status on AuthenticationStrategy
See original GitHub issueIs 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:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
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) andgenerated-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 anas any
and add aTODO: correct typings
and I’ll take a look. Don’t let it hold up the rest of the work.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-L204Then if you run the
codegen
script it will update the generated class in this file and the constructor will now expect an argument for theauthenticationError
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-L60Also adding an e2e test to https://github.com/vendure-ecommerce/vendure/blob/master/packages/core/e2e/authentication-strategy.e2e-spec.ts would be great.