You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
No matter what I try, this never matches, as @auth0/auth0-react seems to maintain its own compiled version of @auth0/auth0-spa-js that I can’t reference.
In the code above, if I look at error.constructor, it points to node_modules/@auth0/auth0-react/node_modules/@auth0/auth0-spa-js/dist/auth0-spa-js.production.esm.js.
I’ve looked through my package-lock.json and I don’t believe I have multiple versions of auth0-spa-js installed; my hunch is the rollup build script is doing something weird here? If so, I think the only way I can reference the error classes correctly is if @auth0/auth0-react re-exports them.
Describe the ideal solution
Re-export auth0-spa-js’s errors in auth0-react/src/errors.tsx:
export*from'@auth0/auth0-spa-js/errors'
Then I can import all error types from auth0-react:
It may also be useful to refactor OAuthError to extend from GenericError—this way we can do a simple check to see if the error is coming from the Auth0 libraries:
+ import { GenericError } from '@auth0/auth0-spa-js'
/**
* An OAuth2 error will come from the authorization server and will have at least an `error` property which will
* be the error code. And possibly an `error_description` property
*
* See: https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.3.1.2.6
*/
- export class OAuthError extends Error {+ export class OAuthError extends GenericError {
constructor(public error: string, public error_description?: string) {
- super(error_description || error);+ super(error, error_description);
// https://github.com/Microsoft/TypeScript-wiki/blob/master/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, OAuthError.prototype);
}
}
import{GenericError,useAuth0}from'@auth0/auth0-react'constErrorComponent=()=>{const{ error }=useAuth0()if(errorinstanceofGenericError){console.log("It’s an Auth0 error")}
Alternatives and current workarounds
I may be able to do duck-typing, like described in auth0/auth0-spa-js#700, but it’s a brittle solution that would break if error names/messages change.
Checklist
Describe the problem you'd like to have solved
I’d like to determine what types of errors are thrown when checking the error object from
useAuth0()
:auth0-react
exportsOAuthError
, which I’m able to check, but it also forwards errors created byauth0-spa-js
.There’s a number of useful types we could check, like
AuthenticationError
,MfaRequiredError
, etc, but the following code fails:No matter what I try, this never matches, as
@auth0/auth0-react
seems to maintain its own compiled version of@auth0/auth0-spa-js
that I can’t reference.In the code above, if I look at
error.constructor
, it points tonode_modules/@auth0/auth0-react/node_modules/@auth0/auth0-spa-js/dist/auth0-spa-js.production.esm.js
.I’ve looked through my
package-lock.json
and I don’t believe I have multiple versions ofauth0-spa-js
installed; my hunch is therollup
build script is doing something weird here? If so, I think the only way I can reference the error classes correctly is if@auth0/auth0-react
re-exports them.Describe the ideal solution
Re-export
auth0-spa-js
’s errors inauth0-react/src/errors.tsx
:Then I can import all error types from
auth0-react
:It may also be useful to refactor
OAuthError
to extend fromGenericError
—this way we can do a simple check to see if the error is coming from the Auth0 libraries:Alternatives and current workarounds
I may be able to do duck-typing, like described in auth0/auth0-spa-js#700, but it’s a brittle solution that would break if error names/messages change.
Additional context
Related issue: auth0/auth0-spa-js#700
The text was updated successfully, but these errors were encountered: