Horizon

Test Sign In with LinkedIn locally

Test Sign In with LinkedIn using OpenID Connect on localhost with a stable HTTPS redirect URL from a Horizon tunnel and Auth.js.

Sign in with LinkedIn on your laptop while LinkedIn redirects to a stable HTTPS URL.

Before you begin

  • A Horizon account and the CLI (see Getting started)
  • A reserved subdomain for -s. Reserve one on the Subdomains page.
  • A Next.js App Router app with Auth.js installed (npm install next-auth@beta)
  • A LinkedIn Developer Portal app

Start a tunnel

Use -s with a subdomain you reserved. Without it, the subdomain is random and changes every run, so the redirect URL you register in LinkedIn would point at a dead URL after a restart. Reserved subdomains are a paid feature, see Pricing.

hrzn tunnel http://localhost:3000 -s my-app
Output
HORIZON: Tunnel connected
  URL          https://my-app.hrzn.run (reserved)
  Forwarding   http://localhost:3000
  Request log  https://hrzn.run/dashboard/tunnels/my-app

Your public URL is https://my-app.hrzn.run. Keep this terminal open.

Configure the app in LinkedIn

  1. Open the LinkedIn Developer Portal and select your app under My apps.
  2. Select the Products tab and request Sign in with LinkedIn using OpenID Connect. This grants the openid, profile and email scopes.
  3. Select the Auth tab.
  4. Under Authorized redirect URLs for your app, add https://my-app.hrzn.run/api/auth/callback/linkedin.
  5. On the same tab, copy the Client ID and the Client Secret.

LinkedIn requires the redirect URL to be absolute and to use HTTPS. LinkedIn ignores query parameters, and the URL can't contain a #.

Configure Auth.js

Auth.js builds the redirect URI from the request. Horizon sets Host to localhost:3000 and sends the public host in X-Forwarded-Host. AUTH_TRUST_HOST=true tells Auth.js to trust X-Forwarded-Host. AUTH_URL pins the public URL. Auth.js says AUTH_URL is mostly unnecessary in v5, so treat it as a second guard.

Generate the secret.

npx auth secret

Add the environment variables.

.env.local
AUTH_SECRET=replace-with-the-generated-secret
AUTH_URL=https://my-app.hrzn.run
AUTH_TRUST_HOST=true
AUTH_LINKEDIN_ID=replace-with-your-client-id
AUTH_LINKEDIN_SECRET=replace-with-your-client-secret

Configure the provider.

auth.ts
import NextAuth from "next-auth";
import LinkedIn from "next-auth/providers/linkedin";

export const { handlers, signIn, signOut, auth } = NextAuth({
  providers: [LinkedIn],
});

Expose the route handlers. This path makes the callback /api/auth/callback/linkedin.

app/api/auth/[...nextauth]/route.ts
import { handlers } from "@/auth";

export const { GET, POST } = handlers;

Add a sign-in button.

app/page.tsx
import { signIn } from "@/auth";

export default function Home() {
  return (
    <form
      action={async () => {
        "use server";
        await signIn("linkedin");
      }}
    >
      <button type="submit">Sign in with LinkedIn</button>
    </form>
  );
}

Start the app on port 3000.

npm run dev

Sign in through the public URL

Open https://my-app.hrzn.run in your browser. On the first visit, select Continue to site on the Before you continue page. Horizon shows this page once every 7 days per IP address. Then select Sign in with LinkedIn.

Don't start from http://localhost:3000. Your app would then send a different origin to LinkedIn.

Check it works

LinkedIn shows its sign-in and consent screen. After you select Allow, your browser lands back on https://my-app.hrzn.run.

Your Horizon terminal prints a line for the callback. The status is a 3xx, because Auth.js redirects after it sets the session.

Output
  GET     302  /api/auth/callback/linkedin

LinkedIn says the email and email_verified fields are optional and may be missing from a response.

Troubleshooting

LinkedIn returns "Redirect_uri doesn't match"

LinkedIn answers with a 401 when the redirect URI in the request doesn't match the one in your developer app.

  • Compare Authorized redirect URLs for your app with https://my-app.hrzn.run/api/auth/callback/linkedin.
  • Check that the subdomain in LinkedIn matches the one you pass to -s.

The redirect URI in the error contains localhost

Your app built the redirect URI from the Host header, which Horizon sets to localhost:3000. Set AUTH_TRUST_HOST=true and AUTH_URL=https://my-app.hrzn.run in .env.local. Restart npm run dev, because Next.js reads .env.local at start.

LinkedIn returns "Invalid scope"

The app doesn't have the permissions in the request. Check the Products tab and the Auth tab for Sign in with LinkedIn using OpenID Connect.

Auth.js throws an UntrustedHost error

Auth.js requires trustHost to be true because it relies on the request's host header. Set AUTH_TRUST_HOST=true in .env.local and restart npm run dev.

The URL changed after a restart

You started the tunnel without -s, so Horizon gave you a new random subdomain. Restart with -s my-app and the registered URL stays valid. -s needs a subdomain you reserved, see Pricing.

The browser shows a Before you continue page

Horizon shows this page once every 7 days per IP address. Select Continue to site.

Nothing reaches your app

  • Check that the Horizon terminal is still running. If its last line is Connection lost. Reconnecting…, wait for Reconnected.
  • Check that the app listens on port 3000, the port in your tunnel command.

Next steps

On this page