# Sign in with Prodmail

Let people sign in to your app or website with their Prodmail account, using standard OpenID Connect. A five-minute quickstart.

Sign in with Prodmail lets people use their Prodmail account to sign in to your app or website. It is standard OpenID Connect (the authorization code flow with PKCE), so any OpenID Connect library works.

Your app learns who the person is: an account ID, their name and their email address. It can never read, send or delete their mail, and it never sees their password.

## Quickstart

1. **Create an app.** In Prodmail, open Settings, Your apps and choose Create app. Give it a name, choose its type, and add the callback URL your app will receive people at. See [Creating and managing apps](/docs/oauth-apps/).
2. **Save the keys.** You get a client ID and, for a web app, a client secret. The secret is shown once.
3. **Point your library at the discovery address.** Most libraries need only this, your client ID and your secret:

```
https://api.prodmail.io/.well-known/openid-configuration
```

4. **Ask for the scopes you need:** `openid`, plus `profile` for the name and `email` for the address.
5. **Try it.** The Test tab of your app builds a working sign-in address you can open straight away.

## The flow without a library

If you'd rather see every step, this is the whole of it. Your server makes a random `state` and a PKCE verifier, keeps both in the person's session, and sends them to:

```
https://account.prodmail.io/oauth/authorize
  ?client_id=YOUR_CLIENT_ID
  &redirect_uri=https://yourapp.com/auth/callback
  &response_type=code
  &scope=openid%20profile%20email
  &state=RANDOM_STATE
  &code_challenge=BASE64URL_SHA256_OF_VERIFIER
  &code_challenge_method=S256
```

The person signs in to Prodmail if they aren't already, and the first time is asked to allow your app to see their name and address. They are then sent back to your callback URL with `code` and `state`. Check that `state` is the one you made, then exchange the code from your server:

```bash
curl -X POST https://api.prodmail.io/v1/oauth/token \
  -u "YOUR_CLIENT_ID:YOUR_CLIENT_SECRET" \
  -d grant_type=authorization_code \
  -d code=CODE_FROM_THE_CALLBACK \
  --data-urlencode redirect_uri=https://yourapp.com/auth/callback \
  -d code_verifier=YOUR_VERIFIER
```

The answer contains an `id_token`: a signed JWT that says who signed in.

```json
{
  "access_token": "…",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "openid profile email",
  "id_token": "eyJhbGciOiJSUzI1NiIs…"
}
```

Verify the ID token's signature against the published keys, check that `iss` is `https://api.prodmail.io` and `aud` is your client ID, and use `sub` as the person's permanent ID in your database. The [reference](/docs/oauth-reference/) lists every claim.

## The same in JavaScript

A callback handler with no dependencies, for Node 18 or later. In production, verify the ID token with a JWT library and the keys at `jwks_uri`.

```js
// GET /auth/callback?code=…&state=…
async function handleCallback(req, session) {
  const url = new URL(req.url, 'https://yourapp.com');
  if (url.searchParams.get('state') !== session.oauthState) throw new Error('state mismatch');

  const response = await fetch('https://api.prodmail.io/v1/oauth/token', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      Authorization: 'Basic ' + Buffer.from(`${process.env.PRODMAIL_CLIENT_ID}:${process.env.PRODMAIL_CLIENT_SECRET}`).toString('base64'),
    },
    body: new URLSearchParams({
      grant_type: 'authorization_code',
      code: url.searchParams.get('code'),
      redirect_uri: 'https://yourapp.com/auth/callback',
      code_verifier: session.oauthVerifier,
    }),
  });
  if (!response.ok) throw new Error((await response.json()).error);
  const { id_token, access_token } = await response.json();

  // The same facts as the ID token, fetched with the access token.
  const person = await (await fetch('https://api.prodmail.io/v1/oauth/userinfo', {
    headers: { Authorization: `Bearer ${access_token}` },
  })).json();
  return person; // { sub, name, email, email_verified }
}
```

## What people see

A Prodmail page with your app's name, icon and description, the site they'll return to, and links to your privacy policy and terms if you gave them. The first time, it lists exactly what your app will see and asks them to allow it. After that, signing in goes straight back to your app. They can remove your app's access at any time under Settings, Security.

## What it doesn't do

- There is no access to mail, contacts or anything else in the account. The only scopes are `openid`, `profile` and `email`.
- There are no refresh tokens. To check that someone is still signed in to Prodmail, send them through the flow again; once they have allowed your app it returns without a prompt.
