# @strutvane/social-login: "Sign in with Google" Without the Security Footguns

"Can we add *Sign in with Google*?"

It sounds like a one-day task. There's a button, a redirect, a callback, and you're done. Right?

Then the details arrive. What stops another site from logging your user into *its* account? What if the callback is replayed? What if the email Google returns was never verified? What if an attacker signs up at a provider with **your CEO's email address** and the app helpfully links it to the CEO's account? 😬

Social sign-in is one of those features where the happy path takes a day and the safe path takes a month. `@strutvane/social-login` is that month, done once.

---

## What it is

`@strutvane/social-login` adds **"Sign in with Google / Microsoft / GitHub / Apple"** to apps built on Strutvane, an open-source (MIT) TypeScript framework for business (B2B) apps.

Under the hood it speaks **OAuth 2.1** and **OpenID Connect** (the standard protocols behind "Sign in with…" buttons), with every protection those standards recommend switched on, not left to you.

## Why it exists

Most social-login code fails in the same few places:

- it skips the `state` check, so another site can complete a sign-in in your user's browser;
- it trusts the email address without checking it was verified;
- it **automatically links** a new provider to an existing account with the same email, which is a classic account-takeover hole;
- it lets a `return_to` parameter send users to any website after sign-in (an "open redirect").

This package was designed against that list. The rules are built in, and the defaults are the strict ones.

## What you get

**Four providers**

- **Google**, with an optional allow-list of Google Workspace domains.
- **Microsoft**, with per-tenant issuer checks and an optional allow-list of Microsoft tenants. The default, `organizations`, accepts work and school accounts only.
- **GitHub**, identified by the numeric account id (never the changeable username) and the primary **verified** email.
- **Apple**, including its unusual cross-site form post, handled with a short-lived, sealed hand-over cookie.

**Two routes per provider**

| Route | What it does |
|---|---|
| `GET /auth/{provider}/redirect?return_to=/dashboard` | Starts a sign-in and sends the browser to the provider |
| `GET /auth/{provider}/callback` | Where the provider sends the browser back |

**Safe account handling**

- **Never auto-link.** If someone signs in with Google using an email that already has an account, they're refused with `social_account_exists`. The real owner signs in normally, then connects Google from their settings page.
- **Linking needs a fresh sign-in** by the same user (within 10 minutes by default).
- **Registration is off by default.** When you turn it on, new accounts need a verified email.
- **Clear error codes** such as `social_state_invalid`, `social_email_unverified` or `social_registration_closed`, so your sign-in page can show a helpful message.

## The security model, in plain words

Here's what the README documents, with the jargon explained:

- **`state`** (a random 256-bit value) proves the callback belongs to a sign-in *this browser* started. It's kept in a sealed, `HttpOnly` cookie, must match, and works **only once**.
- **PKCE** (Proof Key for Code Exchange) means a stolen authorization code is useless without a secret only your server has. It's used on every provider that supports it.
- **`nonce`** binds the provider's ID token to this exact sign-in attempt.
- **ID tokens are verified properly**: the signature against the provider's published keys, only strong algorithms (`RS256`/`ES256`, never `none`), the exact issuer, the audience, and the timestamps.
- **Replays fail**: `state` and Apple hand-overs are single use, and every flow expires after 10 minutes.
- **Open redirects are blocked**: `return_to` must be a path on your own app; anything else goes home.
- **Nothing secret is logged.** Logs carry the provider, the outcome and the user id, never codes, tokens, `state`, nonces, cookies or emails.
- **Hostile responses are bounded**: outbound calls have a 10-second timeout, no redirects and a size cap.
- **Bad config stops the boot.** An unknown Microsoft tenant, a redirect to another site or an `http` app URL outside localhost is refused, with a message that names it.

## How it fits into Strutvane

`@strutvane/social-login` is an **add-on**. It depends only on the published `@strutvane/framework` (as a peer dependency) and never imports another add-on. It serves its routes through the framework's request hook, right after the identity hook, and uses the framework's own encryption, cache and logger.

There's one deliberate seam. Social sign-in proves *who someone is at Google*. Deciding **which user of your app** that is, and starting their session, belongs to your identity system. So the package asks a few narrow questions through one small contract, `SocialAccounts`:

- find the user linked to this provider identity;
- find the user who owns this email;
- who is signed in right now, and when did they last authenticate;
- create a passwordless user;
- link a provider identity to a user;
- sign this user in (with a **new** session id, keeping any second factor).

For now, you write this small adapter over your own user tables (a reference in-memory version ships in `@strutvane/social-login/testing`). If nothing is bound, every sign-in **fails closed** with `social_accounts_unavailable` and the app warns at boot.

## A quick look

```bash
npm install @strutvane/social-login
node strut add social-login --providers google,github
```

The installer writes `config/social-login.ts` for the providers you chose, registers the provider, and adds each provider's keys to `.env` and `.env.example`, **empty**. Client secrets are never generated or written into `.env.example`. Running it twice changes nothing, and `node strut remove social-login` reverses it (keeping your config file).

Then register the callback URL `${APP_URL}/auth/<provider>/callback` with each provider and fill in the keys. A provider whose keys are still empty is skipped with a warning, so your app keeps booting while you set things up.

Building the button link:

```ts
import { socialLoginUrl } from '@strutvane/social-login'

socialLoginUrl('google', { returnTo: '/dashboard' }) // '/auth/google/redirect?return_to=%2Fdashboard'
```

And connecting a provider to an account that already exists, from a settings page:

```ts
socialLoginUrl('github', { intent: 'link', returnTo: '/settings/accounts' })
```

> ⚠️ This is a **0.2 preview**. The APIs may still change before 1.0, planned for January 2027.

## Testing without the internet

`@strutvane/social-login/testing` includes a **fake OpenID Connect provider** that runs entirely offline. It enforces single-use codes, the redirect URI and PKCE, signs real ID tokens with a key from its own published key set, and even serves GitHub-style user and email endpoints. Your sign-in tests don't need real Google credentials or a network connection.

## Built to be trusted

- **247 automated tests**.
- **CI on Linux, Windows and macOS**, on Node 24 and Node 26.
- **Security-reviewed** and secure by default: no auto-linking, registration off, closed when unconfigured.
- **Every release is staged by CI and approved by a human with two-factor authentication**, and published with **npm provenance**.

## What's next

The README notes that `@strutvane/auth` is planned to provide the `SocialAccounts` contract itself. When that lands, social sign-in will work with Strutvane's own identity engine without the adapter you write today.

---

## Try it, and tell me what you think

- ⭐ **GitHub:** [github.com/vishwajit-vm/strutvane-social-login](https://github.com/vishwajit-vm/strutvane-social-login)
- 📦 **npm:** [npmjs.com/package/@strutvane/social-login](https://www.npmjs.com/package/@strutvane/social-login)
- 💬 **Feedback welcome.** Which provider should come next? And if you've ever been bitten by a social-login bug, I'd love to hear the story (and add a test for it).

Next, and last in this round of the series: the tiny `strutvane` package behind `npx strutvane`. Follow along! 🔑

*— Vishwajit VM*

---

*Part of the Strutvane series. Strutvane™ is open source under the MIT licence.*
