Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

How to display failure #76

Closed
JSLNO opened this issue Dec 8, 2021 · 5 comments
Closed

How to display failure #76

JSLNO opened this issue Dec 8, 2021 · 5 comments

Comments

@JSLNO
Copy link

JSLNO commented Dec 8, 2021

Hey,
I want to display the AuthenticationError that is thrown in the login function on the sign in page. Any suggestions on how I could do that or is this even possible?

Thanks

@JSLNO JSLNO changed the title How to display a error How to display failure Dec 8, 2021
@sergiodxa
Copy link
Owner

All the strategies catch the errors your throw and wrap it in a Response.

If you defined a failureRedirect, the response would be a redirect and the error will be in the session.

If you didn't defined a failureRedirect, the response will be a 401 with the error as part of the body. This one could be accessed in a CatchBoundary component.

And you can wrap the authenticator.authenticate call in a try/catch and manually check the response and handle the error.

@JSLNO
Copy link
Author

JSLNO commented Dec 20, 2021

All the strategies catch the errors your throw and wrap it in a Response.

If you defined a failureRedirect, the response would be a redirect and the error will be in the session.

If you didn't defined a failureRedirect, the response will be a 401 with the error as part of the body. This one could be accessed in a CatchBoundary component.

And you can wrap the authenticator.authenticate call in a try/catch and manually check the response and handle the error.

I don't get why but when I try using a try/catch block theres always an error thrown even when the credentials are correct. But when I remove the block it works how it is supposed to. Here's the action function I currently have:

export const action: ActionFunction = async ({ request }) => {
    try {
        await authenticator.authenticate('local', request, {
            successRedirect: '/'
        })
    } catch (e) {
        if (e instanceof Response) {
            const data = await e.json()
            return { error: data.message }
        }
        throw e
    }

    return {}
}

This works when the credentials are incorrect, but when they are correct, it throws:
FetchError: invalid json response body at reason: Unexpected end of JSON input

@sergiodxa
Copy link
Owner

That’s because the library made use of Remix ability to throw responses including redirects

@JSLNO
Copy link
Author

JSLNO commented Dec 20, 2021

Sound's interesting, do you have any suggestion on how I could fix it?
Thanks

@JSLNO
Copy link
Author

JSLNO commented Dec 20, 2021

Sound's interesting, do you have any suggestion on how I could fix it? Thanks

Okay, I got it working like this:

export const action: ActionFunction = async ({ request }) => {
	try {
		await authenticator.authenticate('local', request, {
			successRedirect: '/'
		})
	} catch (e) {
		console.log(e)
		if (e instanceof Response && e.status === 401) {
			const data = await e.json()

			return {
				error: data.message || 'Unauthorized'
			}
		}
		throw e
	}

	return {}
}

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

No branches or pull requests

2 participants