loginos

Guides

Link existing accounts

Already have users? Let them link their account to loginos from your settings page, with no migration.

Your app already has people with their own accounts. Linking lets each of them connect that account to loginos once, from your settings page. From then on they sign in with loginos and land in the same account, and every token carries your own user id as external_id. No migration, no new column, no duplicate accounts.

your-app.example/settings

J

Jane Doe

id u_1842

loginosLink loginos

loginos.io

Your app account

jane@example.com

Your loginos account

Jane Doe @jane

What your server reads from /oauth/token

{  "sub": "9b1f6c2e-…",  "preferred_username": "jane",  "roles": [    "member"  ]}
1/5Jane already has an account on your site. In its settings she clicks “Link loginos”.

How it works

  1. A signed-in user clicks Link loginos in your account settings.
  2. Your server sends the request to loginos first, over the back channel, with your id for that user (external_id). This is a pushed authorization request (PAR): the browser never holds the id, so nobody can swap it.
  3. The person signs in to loginos, or creates an account on the spot, and sees both accounts side by side before approving.
  4. loginos redirects to your callback with a code. When your server redeems it, the link is made and the token says "external_id": "your-user-id".
  5. From then on, “Sign in with loginos” on any of your domains returns that external_id: sign in that user.

When the signed-in user clicks the button, call /oauth/par from your server with the usual authorization parameters plus external_id:

curl -X POST https://loginos.io/oauth/par \
  -u "YOUR_APP_KEY:YOUR_APP_SECRET" \
  -d response_type=code \
  -d redirect_uri=https://app.example.com/api/auth/login/single/callback \
  -d "scope=openid profile email" \
  -d state=RANDOM_STATE \
  -d code_challenge=BASE64URL_SHA256_OF_VERIFIER \
  -d code_challenge_method=S256 \
  -d external_id=u_1842 \
  -d external_label=jane@example.com

Keep state, the code_verifier and the user’s id in an HttpOnly cookie or your session, then redirect the browser to authorize_url.

Parameter
external_idYour id for the signed-in user. 1 to 128 visible characters. Required for linking.
external_labelHow the person recognizes that account, like their email or username on your site. Shown above their loginos account. Optional, up to 120 characters.
code_challengeRequired when linking, with code_challenge_method=S256.
everything elseThe same as /oauth/authorize: response_type, redirect_uri, scope, state, nonce, prompt, ui_locales.

The request can be used for 30 minutes, long enough to create a loginos account on the way, and it yields one code.

2. Handle the callback

It is the same callback as sign-in. Check state, redeem the code with the verifier, and make sure the token belongs to the account that started the link:

// app/settings/loginos/link/route.ts
import { createHash, randomBytes } from "node:crypto";
import { NextResponse, type NextRequest } from "next/server";
import { currentUser } from "@/lib/session"; // your own

const LOGINOS = "https://loginos.io";
const b64url = (b: Buffer) => b.toString("base64url");
const basic = "Basic " + Buffer.from(`${process.env.LOGINOS_APP_KEY}:${process.env.LOGINOS_APP_SECRET}`).toString("base64");

export async function GET(req: NextRequest) {
  const me = await currentUser(req);
  if (!me) return NextResponse.redirect(new URL("/login", req.url));
  const state = b64url(randomBytes(16));
  const verifier = b64url(randomBytes(32));
  const res = await fetch(`${LOGINOS}/oauth/par`, {
    method: "POST",
    headers: { authorization: basic },
    body: new URLSearchParams({
      response_type: "code",
      redirect_uri: `${req.nextUrl.origin}/api/auth/login/single/callback`,
      scope: "openid profile email",
      state,
      code_challenge: b64url(createHash("sha256").update(verifier).digest()),
      code_challenge_method: "S256",
      external_id: me.id,
      external_label: me.email,
    }),
  });
  if (!res.ok) return new Response("Couldn't start the link.", { status: 502 });
  const { authorize_url } = await res.json();
  const go = NextResponse.redirect(authorize_url);
  go.cookies.set("lo_link", `${state}.${verifier}`, { httpOnly: true, secure: true, sameSite: "lax", maxAge: 1800 });
  return go;
}

The member now carries your id everywhere loginos describes them: in ID tokens, in user from the token endpoint, in /oauth/userinfo and in the Management API.

3. Sign in with loginos

Your login page gets a “Sign in with loginos” button that uses the normal flow. After the code exchange:

After the exchange
const { user } = await exchange(req, code, verifier);
if (user.external_id) return signIn(await db.users.find(user.external_id));

// No link yet. Don't create a second account for someone who may already have one:
// ask them to sign in the old way once, then link from settings.
return NextResponse.redirect(new URL("/login?loginos=not-linked", req.url));

If you also accept brand-new people through loginos, create their account at that point and tell loginos your new id (see below), so their next tokens carry it.

People who join with loginos first

For people with no account on your side yet, create the account on their first sign-in and give loginos your new id:

curl
curl -X PUT https://loginos.io/api/v1/users/LOGINOS_SUB/external-id \
  -u "YOUR_APP_KEY:YOUR_APP_SECRET" \
  -H "content-type: application/json" \
  -d '{"external_id": "u_1843", "external_label": "sam@example.com"}'

It answers 409 external_id_taken when another member already has that id, so a bug can never move someone’s account.

  • GET /api/v1/users?external_id=u_1842 finds the member linked to one of your users, so you need no table of your own.
  • DELETE /api/v1/users/{sub}/external-id unlinks them; they stay a member.
  • The console’s Users tab shows your id on every linked member and can unlink them; the Overview counts linked accounts.
  • When someone removes your platform’s access from their loginos dashboard, the link goes too. Their next sign-in with loginos has no external_id: ask them to sign in the old way and link again.

What the person sees

Your platform’s name and logo, the account you named in external_label above their loginos account, what linking changes, and what you will get. On approval the two accounts lock together and they are back on your site. If their loginos account is already linked to a different account of yours, loginos stops there and offers to use another loginos account.

Edge cases

SituationWhat happens
Your external_id was linked to another loginos accountThe new link replaces it: you started the request, so your id now belongs to the person who approved it.
The person cancelsYour callback gets error=access_denied. Nothing changes.
The request is older than 30 minutes, or already usedloginos shows “This link has expired”. Start again from your settings page.
The person is blocked on your platformloginos stops before the link screen.
Your platform doesn’t take new membersPeople who already joined can link; others are told sign-ups are paused.
prompt=noneLinking always needs the person, so you get error=consent_required.