How-to Guides
Sign In with Google
Authenticate users with Google and receive an authorization code for token exchange
Sign In with Google
Use the SDK's Auth module to sign in users with their Google account and receive an authorization code for your backend.
Prerequisites
- The starti.app SDK is installed and initialized
- Google sign-in is configured for your app in the starti.app manager
Steps
Call signIn with the "google" provider
const result = await startiapp.Auth.signIn("google");Check the result and handle success or failure
if (result.isSuccess) {
// Send the authorization code to your backend to exchange for tokens
console.log("Authorization code:", result.authorizationCode);
console.log("Code verifier:", result.codeVerifier);
console.log("Redirect URI:", result.redirectUri);
} else {
console.error("Sign in failed:", result.errorMessage);
}Exchange the code on your backend
Send authorizationCode, codeVerifier, and redirectUri to your server, which exchanges them with Google for access and refresh tokens.
Complete example
await startiapp.initialize();
async function loginWithGoogle() {
const result = await startiapp.Auth.signIn("google");
if (!result.isSuccess) {
alert("Login failed: " + result.errorMessage);
return;
}
// Exchange with your backend
const response = await fetch("/api/auth/google", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
authorizationCode: result.authorizationCode,
codeVerifier: result.codeVerifier,
redirectUri: result.redirectUri,
}),
});
const session = await response.json();
console.log("Logged in as:", session.email);
}You can check if the user is already authenticated before showing a sign-in button:
const loggedIn = await startiapp.Auth.isAuthenticated();