Spaces:
Runtime error
Runtime error
File size: 1,473 Bytes
5b12974 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import type { Authenticator } from "remix-auth";
import { GoogleStrategy } from "@coji/remix-auth-google";
import { env } from "~/env.server";
import { findOrCreateUser } from "~/models/user.server";
import type { AuthUser } from "./authUser";
import { postAuthentication } from "./postAuth.server";
import { logger } from "./logger.service";
export function addGoogleStrategy(
authenticator: Authenticator<AuthUser>,
clientId: string,
clientSecret: string,
) {
const googleStrategy = new GoogleStrategy(
{
clientId,
clientSecret,
redirectURI: `${env.LOGIN_ORIGIN}/auth/google/callback`,
},
async ({ tokens }) => {
const profile = await GoogleStrategy.userProfile(tokens);
const emails = profile.emails;
if (!emails) {
throw new Error("Google login requires an email address");
}
try {
logger.debug("Google login", {
emails,
profile,
});
const { user, isNewUser } = await findOrCreateUser({
email: emails[0].value,
authenticationMethod: "GOOGLE",
authenticationProfile: profile,
authenticationExtraParams: {},
});
await postAuthentication({ user, isNewUser, loginMethod: "GOOGLE" });
return {
userId: user.id,
};
} catch (error) {
console.error(error);
throw error;
}
},
);
authenticator.use(googleStrategy as any, "google");
}
|