| 6 | export let loader: LoaderFunction = () => redirect("/login"); |
| 7 | |
| 8 | export let action: ActionFunction = async ({ request }) => { |
| 9 | const url = new URL(request.url); |
| 10 | const redirectTo = url.searchParams.get("redirectTo"); |
| 11 | |
| 12 | try { |
| 13 | // call authenticate as usual, in successRedirect use returnTo or a fallback |
| 14 | return await authenticator.authenticate("github", request, { |
| 15 | successRedirect: redirectTo ?? "/", |
| 16 | failureRedirect: "/login", |
| 17 | }); |
| 18 | } catch (error) { |
| 19 | // here we catch anything authenticator.authenticate throw, this will |
| 20 | // include redirects |
| 21 | // if the error is a Response and is a redirect |
| 22 | if (error instanceof Response) { |
| 23 | // we need to append a Set-Cookie header with a cookie storing the |
| 24 | // returnTo value |
| 25 | error.headers.append("Set-Cookie", await redirectCookie.serialize(redirectTo)); |
| 26 | } |
| 27 | throw error; |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | export const redirectCookie = createCookie("redirect-to", { |
| 32 | maxAge: 60 * 60, // 1 hour |