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-appHORIZON: Tunnel connected
URL https://my-app.hrzn.run (reserved)
Forwarding http://localhost:3000
Request log https://hrzn.run/dashboard/tunnels/my-appYour public URL is https://my-app.hrzn.run. Keep this terminal open.
Configure the app in LinkedIn
- Open the LinkedIn Developer Portal and select your app under My apps.
- Select the Products tab and request Sign in with LinkedIn using OpenID Connect. This grants the
openid,profileandemailscopes. - Select the Auth tab.
- Under Authorized redirect URLs for your app, add
https://my-app.hrzn.run/api/auth/callback/linkedin. - 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 secretAdd the environment variables.
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-secretConfigure the provider.
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.
import { handlers } from "@/auth";
export const { GET, POST } = handlers;Add a sign-in button.
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 devSign 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.
GET 302 /api/auth/callback/linkedinLinkedIn 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 forReconnected. - Check that the app listens on port 3000, the port in your tunnel command.
Next steps
- Read the shared notes on OAuth redirect URLs and the Auth.js guide.
- Read LinkedIn's guide to Sign In with LinkedIn using OpenID Connect.