How to Add Social Login to an AI-Built App
A practical walkthrough of adding Google, GitHub, or Apple sign-in to an app built with an AI coding agent, including which providers to pick and what breaks if you add it after users already exist.
If you built your app with an AI coding agent, there is a decent chance it shipped with email and password and nothing else, because that is the fastest form to scaffold. Adding social login to an AI-built app is not conceptually hard. It is the same OAuth flow every app on the internet uses: register with a provider, redirect the user there, get a token back, create or match a local account. What trips people up is scope. A vague instruction to an agent produces a Google button that works fine in a demo and breaks the moment a real user's email does not match what is already sitting in your database.
This is the actual decision tree: which providers are worth a button, when social login beats plain email and password, what breaks if you bolt it onto an app that already has users, and a code example close enough to what your agent will produce that you can check it line by line, rather than another list of reasons social login is good.
Why the button matters more than it looks
Every extra field on a signup form costs you some fraction of the people who almost joined. The password field is usually the worst offender, because it asks a new visitor to invent and remember a credential before they have any real reason to trust you with one. Social login skips that step. The user clicks a button they already recognize, approves a permission screen on the provider's own page, and lands back in your app signed in. No form to fill in, no password to forget, no reset-email flow you have to build and maintain.
It also shifts a chunk of security work onto the provider. Google, GitHub, and Apple already run account recovery, breach monitoring, and two-factor prompts at a scale you will never replicate for a new app. Every user who signs in through one of them inherits that infrastructure for free.
The OAuth flow, in plain terms
Strip away the vendor-specific SDK calls and every social login button runs the same five steps.
Your app redirects the user to the provider's authorization URL, along with your app's client ID and the permissions, or scopes, you are asking for.
The user approves the request on the provider's own page, never yours. They type their password into Google or GitHub, never into your app.
The provider redirects back to a callback URL you registered in advance, carrying an authorization code.
Your backend, not the browser, exchanges that code for an access token and usually an ID token containing the user's verified email and name.
Your app looks up a local user record matching that provider account. If none exists, it creates one.
The redirect URL is the detail that breaks most agent-built implementations, because local development uses one URL and production uses another, and the provider needs both registered ahead of time. If you want the full spec on scopes and redirect requirements, Google's OAuth 2.0 documentation covers it directly.
Which providers to offer, and when
A Google sign-in button is close to the default choice for an AI-built app aimed at a general audience, since it covers both personal Gmail users and Workspace accounts and needs the least explaining. Beyond that, the right providers depend on who is actually using the app:
Google. Broadest overlap with any general consumer or B2B product. Offer it first if you offer only one.
GitHub. Strong fit for developer tools and anything coding-adjacent, weak fit for a consumer app.
Apple. Mostly matters if you ship on iOS, since Apple requires an Apple sign-in option if you offer any other third-party login in an App Store app. Skip it for a web-only product.
Facebook, X, or LinkedIn. Only worth the maintenance if your audience is specifically already active there. For most AI-built app ideas, they add a button nobody clicks.
Email and password. Keep it. Some users do not want another account tied to a social identity, and anyone who signed up before you added social login is already using it.
Social login vs email and password: pick a default, not a religion
The real decision is not either-or, it is which one gets the bigger button and which one is the fallback. Offering both and defaulting to social login gives most users the fast path while covering the people who cannot or will not use it. Weighed head to head, social login vs email password comes down to a short list of tradeoffs: social login wins on conversion, on removing a password from your own database, and on inheriting provider-side account recovery, while email and password wins for users without a supported social account, for corporate tools where IT manages identity separately, and for keeping your app working even if a provider has an outage.
How to prompt an AI coding agent to add it correctly
If you are past the point of building an app with AI from scratch and just need to bolt on a provider, the prompt matters more than the code. A vague request like "add Google login" leaves the agent guessing at scopes, redirect URLs, and how to treat a returning user. Spell those out and the result is almost always correct on the first pass.
A prompt worth giving an agent looks closer to this:
Add Google OAuth sign-in alongside the existing email and password login.
- Use [library or service] for the OAuth flow.
- Redirect URIs: http://localhost:3000/auth/callback for local dev,
https://[production-domain]/auth/callback for production.
- On callback, look up the user by the provider's unique account ID,
not by the email address on the token.
- If no matching account exists but the verified email matches an
existing email and password account, do not auto-merge. Show a
message asking the user to sign in with their password first, then
link Google from account settings.
- Store only the fields the app needs: name, email, avatar URL. Do
not persist the provider's access or refresh token unless a
specific feature needs to call that provider's API later.Whatever library the agent reaches for, the client secret it generates for you does not belong in the codebase. The same rules that apply to any other credential in environment variables and secrets in an AI-built app apply here too.
A worked example
The exact method names differ by library and version, so treat the shape of this code as what a correct implementation looks like, not a snippet to paste directly. This example follows the pattern Supabase Auth uses, since it is a common backend for AI-built apps and its OAuth handling is representative of how most providers structure the same flow.
// Client: start the OAuth flow
await supabase.auth.signInWithOAuth({
provider: 'google',
options: {
redirectTo: `${window.location.origin}/auth/callback`
}
});
// Server: after the provider redirects back, the session is already
// set. Read the verified identity off the session. Never trust
// anything the client itself claims about who the user is.
const { data: { user } } = await supabase.auth.getUser();
const verifiedEmail = user?.email;
const providerId = user?.identities?.[0]?.id;What breaks if you add social login to an app that already has users
This is the part most AI-built apps get wrong, because it only shows up once real users exist and it is invisible in a demo. If the callback logic matches purely on provider ID, a returning user who signed up months ago with email and password gets a brand new account the first time they click "Sign in with Google", because no record with that provider ID exists yet. Now the same person has two accounts, their old data sits under one and their new activity sits under the other, and support tickets start arriving with some version of "I lost my account."
The fix has to happen at the schema level, not just in the callback handler. Rather than storing a provider ID as a column on the users table, most apps that get this right keep a separate table of linked identities, one row per provider per user, so a single account can have an email and password identity and a Google identity at the same time. That decision connects directly to choosing a database for an AI-built app, and it is much cheaper to get right before launch than to migrate afterward.
On the callback itself: check for an existing account with the same verified email before creating a new one, and if you find one, prompt the user to link accounts rather than merging silently. Silent merging on email alone is its own security hole, since not every provider verifies the email it hands back. The account model this depends on is the same one covered in adding user accounts to an AI-built app, and it is worth reading before you touch the login flow if the agent designed that model without much guidance.
A short security note
Never store a provider's access or refresh token anywhere a browser script can read it, such as localStorage. Keep it server-side, and only keep it at all if a feature genuinely needs to call that provider's API again later.
Check the email_verified claim before treating an OAuth email as trustworthy. Not every provider guarantees the email it returns has actually been confirmed.
Match returning users by the provider's account ID, not by email, once an account is linked. Email addresses can change or get reassigned upstream in ways your app never sees.
Treat the OAuth client secret like any other credential in the project. An agent that pastes it into a committed file is the same failure mode covered in can an AI coding agent leak your API keys, just with a different key.
Frequently asked questions
Is social login safer than email and password?
For most users, yes, mostly because it removes a password your app would otherwise have to store and defend on its own. It also means you are trusting the provider's account security instead of your own, which is a reasonable trade for a small team but still worth weighing consciously rather than assuming it is automatically safer.
Should a new AI-built app skip email and password entirely?
Only if you are confident every realistic user already has one of the accounts you support. Most products keep both and simply default to social login for the faster path, with email and password as the fallback.
What happens if a user loses access to the Google account they signed up with?
This is the strongest argument for capturing a verified email during onboarding even when the primary path is social login, so support has some way to confirm identity and handle recovery manually outside the OAuth flow itself.
Do I need to support every major provider?
No. Two providers, usually Google plus one audience-specific option such as GitHub for a developer tool or Apple for an iOS app, cover most products. Extra buttons mostly add maintenance without adding many new signups.
Can I add a second provider later without breaking existing accounts?
Yes, as long as new sign-ins are matched against existing accounts by verified email before a new record gets created, which is the same logic that prevents duplicate accounts when you add social login to an app for the first time.
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


