UNPKG

@softr/sdk

Version:

The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.

56 lines (54 loc) • 2.5 kB
import { assert, panic } from "../../assertions.js"; import { writeCookies } from "../../cookies.js"; import { sessionConfig } from "../config.js"; import { makePlatform } from "../platform.js"; import { clearPendingAuth, makePendingAuthCookie, makeSessionDataCookie, makeSessionTokenCookie } from "../session.js"; import { authError } from "./utils.js"; //#region src/auth/methods/social.ts /** * Signs a visitor in with a Google token the browser obtained, creating their * account the first time. * * Pass the id token Google Identity Services handed the page. An access token * is rejected, and so is a token minted for another app. `fullName` names a * first-time visitor and does nothing for one who already has an account. * * A `signedIn` result means the visitor is signed in. An app that also asks for * an authenticator app answers `authenticatorRequired` instead, naming the step * to send them to next. */ async function signInWithGoogle(credentials) { const result = await makePlatform(sessionConfig().applicationId).signInWithGoogle(credentials); if (!result.ok) { if (result.status === 429) return authError("tooManyAttempts", result.message); if (result.status === 400 && result.message.startsWith("Emails with ") && result.message.endsWith(" domain is not allowed to signup.")) return authError("emailDomainNotAllowed", result.message); if (result.status === 400 || result.status === 401) return authError("invalidCredentials", result.message); if (result.status >= 500) panic(`Expected Google sign-in to succeed, but got ${result.status}: ` + result.message); throw new Error(`${result.message}. Check SOFTR_APPLICATION_ID and that the app allows Google sign-in.`); } if (result.data.status === "MFA_REQUIRED") { const { mfa } = result.data; assert(mfa, "Expected a challenge from Google sign-in, but got none"); writeCookies([makePendingAuthCookie(mfa.challengeToken)]); return { success: true, data: { status: "authenticatorRequired", step: mfa.action === "VERIFICATION_REQUIRED" ? "verify" : mfa.action === "SETUP_REQUIRED" ? "setup" : "setupOptional" } }; } const { session } = result.data; assert(session, "Expected a session from Google sign-in, but got none"); writeCookies([...makeSessionTokenCookie(session.jwt), makeSessionDataCookie({ user: session.user }, session.jwt)]); clearPendingAuth(); return { success: true, data: { status: "signedIn", user: session.user } }; } //#endregion export { signInWithGoogle };