f62cdfe00e
<img width="568" height="513" alt="CleanShot 2025-12-05 at 14 27 16" src="https://github.com/user-attachments/assets/1f44d8b9-8791-4b44-96d5-4a0960a1ab36" /> <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds Google OAuth login and a cookie-based “last used” indicator on the login page, with supporting backend, routes, and schema updates. > > - **Auth/Backend**: > - **Google OAuth**: Integrates `remix-auth-google` via new `addGoogleStrategy` and enables when `AUTH_GOOGLE_CLIENT_ID/SECRET` are set (`services/googleAuth.server.ts`, `services/auth.server.ts`). > - **User handling**: Implements `findOrCreateGoogleUser` with linking/upsert logic and conflict logging (`models/user.server.ts`). > - **MFA + session**: Google/GitHub/Magic callbacks now set session, handle MFA, and set a "last-auth-method" cookie (`routes/auth.google*.tsx`, `routes/auth.github.callback.tsx`, `routes/magic.tsx`, `services/lastAuthMethod.server.ts`). > - **GitHub strategy**: Safer email check (`services/gitHubAuth.server.ts`). > - **Routes/UI**: > - **Login page**: Adds "Continue with Google" button and animated "Last used" badge based on cookie; keeps GitHub/Email options (`routes/login._index/route.tsx`). > - **Redirect safety**: Sanitize redirect paths and persist redirect via cookies in auth actions (`routes/auth.github.ts`, `routes/auth.google.ts`). > - **Assets**: Adds `GoogleLogo` SVG. > - **Avatar**: Set `referrerPolicy="no-referrer"` on profile image. > - **Config/Schema**: > - **Env**: Adds `AUTH_GOOGLE_CLIENT_ID`/`AUTH_GOOGLE_CLIENT_SECRET` (`env.server.ts`). > - **DB**: Extends `AuthenticationMethod` enum with `GOOGLE` (Prisma schema + migration). > - **Dependencies**: > - Adds `remix-auth-google` in `package.json`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 9f84f974bd6f21f1699c4f69a6aa91616842d1b1. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: James Ritchie <james@trigger.dev>
38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
import { type ActionFunction, type LoaderFunction, redirect, createCookie } from "@remix-run/node";
|
|
import { authenticator } from "~/services/auth.server";
|
|
import { env } from "~/env.server";
|
|
import { sanitizeRedirectPath } from "~/utils";
|
|
|
|
export let loader: LoaderFunction = () => redirect("/login");
|
|
|
|
export let action: ActionFunction = async ({ request }) => {
|
|
const url = new URL(request.url);
|
|
const redirectTo = url.searchParams.get("redirectTo");
|
|
const safeRedirect = sanitizeRedirectPath(redirectTo, "/");
|
|
|
|
try {
|
|
// call authenticate as usual, in successRedirect use returnTo or a fallback
|
|
return await authenticator.authenticate("github", request, {
|
|
successRedirect: safeRedirect,
|
|
failureRedirect: "/login",
|
|
});
|
|
} catch (error) {
|
|
// here we catch anything authenticator.authenticate throw, this will
|
|
// include redirects
|
|
// if the error is a Response and is a redirect
|
|
if (error instanceof Response) {
|
|
// we need to append a Set-Cookie header with a cookie storing the
|
|
// returnTo value (store the sanitized path)
|
|
error.headers.append("Set-Cookie", await redirectCookie.serialize(safeRedirect));
|
|
}
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const redirectCookie = createCookie("redirect-to", {
|
|
maxAge: 60 * 60, // 1 hour
|
|
httpOnly: true,
|
|
sameSite: "lax",
|
|
secure: env.NODE_ENV === "production",
|
|
});
|