add users to loops post google sign in (#194)

This commit is contained in:
Ben Senescu 2026-05-13 21:44:34 -04:00 committed by GitHub
parent 701ace3dff
commit f926a0f78c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -56,22 +56,7 @@ function createAuth() {
}); });
}, },
afterEmailVerification: async (user) => { afterEmailVerification: async (user) => {
try { await syncHostedVerifiedContact(user, "email verification");
await upsertHostedVerifiedContact({
userId: user.id,
email: user.email,
name: user.name,
});
} catch (error) {
console.error(
"Failed to sync Loops profile after verification:",
{
userId: user.id,
email: user.email,
error,
},
);
}
}, },
}, },
socialProviders: getSocialProviders(), socialProviders: getSocialProviders(),
@ -81,6 +66,17 @@ function createAuth() {
}), }),
plugins: [...baseAuthConfig.plugins, tanstackStartCookies()], plugins: [...baseAuthConfig.plugins, tanstackStartCookies()],
databaseHooks: { databaseHooks: {
user: {
create: {
after: async (user, context) => {
if (context?.path !== "/callback/google") {
return;
}
await syncHostedVerifiedContact(user, "Google sign up");
},
},
},
session: { session: {
create: { create: {
before: async (session) => { before: async (session) => {
@ -108,6 +104,25 @@ function createAuth() {
let authInstance: ReturnType<typeof createAuth> | null = null; let authInstance: ReturnType<typeof createAuth> | null = null;
async function syncHostedVerifiedContact(
user: { id: string; email: string; name?: string | null },
context: string,
) {
try {
await upsertHostedVerifiedContact({
userId: user.id,
email: user.email,
name: user.name,
});
} catch (error) {
console.error(`Failed to sync Loops profile after ${context}:`, {
userId: user.id,
email: user.email,
error,
});
}
}
function getTrustedOrigins(baseUrl: string) { function getTrustedOrigins(baseUrl: string) {
const trustedOrigins = [baseUrl]; const trustedOrigins = [baseUrl];
@ -148,21 +163,29 @@ function getHostedSecret() {
} }
function getSocialProviders() { function getSocialProviders() {
return {
google: getGoogleSocialProviderConfig(),
};
}
function getGoogleSocialProviderConfig() {
const googleClientId = env.GOOGLE_CLIENT_ID?.trim(); const googleClientId = env.GOOGLE_CLIENT_ID?.trim();
const googleClientSecret = env.GOOGLE_CLIENT_SECRET?.trim(); const googleClientSecret = env.GOOGLE_CLIENT_SECRET?.trim();
if (!googleClientId || !googleClientSecret) { if (!googleClientId) {
return undefined; throw new Error("GOOGLE_CLIENT_ID is required in hosted mode");
}
if (!googleClientSecret) {
throw new Error("GOOGLE_CLIENT_SECRET is required in hosted mode");
} }
return { return {
google: {
clientId: googleClientId, clientId: googleClientId,
clientSecret: googleClientSecret, clientSecret: googleClientSecret,
mapProfileToUser: (profile: { name?: string }) => ({ mapProfileToUser: (profile: { name?: string }) => ({
name: profile.name, name: profile.name,
}), }),
},
}; };
} }
@ -183,6 +206,7 @@ export function hasHostedAuthConfig() {
try { try {
getHostedBaseUrl(); getHostedBaseUrl();
getHostedSecret(); getHostedSecret();
getGoogleSocialProviderConfig();
return ( return (
Reflect.get(env, "BYPASS_EMAIL_VERIFICATION") === "true" || Reflect.get(env, "BYPASS_EMAIL_VERIFICATION") === "true" ||
hasHostedAuthEmailConfig() hasHostedAuthEmailConfig()