Sign in from a Node.js app
A small Node.js server that signs users in with Facebook through OpenAuth.
On this page
This server has two addresses: /login/facebook/ starts the sign-in, and /auth/ receives the user back and reads the profile. It uses only Node.js (18 or newer, for fetch).
The code
Save it as app.js and fill in the three constants at the top:
const http = require('http');
const crypto = require('crypto');
const OPENAUTH = 'https://auth.example.com'; // your OpenAuth address
const TOKEN = 'paste-your-app-token-here'; // a token from Tokens in OpenAuth
const APP = 'https://www.example.com'; // the public address of this app
const server = http.createServer(async function(req, res) {
const url = new URL(req.url, APP);
// 1. The user clicks "Sign in with Facebook"
if (url.pathname === '/login/facebook/') {
const sessionid = crypto.randomUUID();
const response = await fetch(OPENAUTH + '/', {
method: 'POST',
headers: { 'content-type': 'application/json', 'x-token': TOKEN },
body: JSON.stringify({ serviceid: 'facebook', sessionid: sessionid, url: APP + '/auth/' })
});
if (!response.ok) {
res.writeHead(502, { 'content-type': 'text/plain' });
res.end('Sign-in is not available right now.');
return;
}
const address = await response.json();
res.writeHead(302, { location: address, 'set-cookie': 'signin=' + sessionid + '; Path=/; HttpOnly; SameSite=Lax' });
res.end();
return;
}
// 2. OpenAuth sends the user back here with ?sessionid=...
if (url.pathname === '/auth/') {
const sessionid = url.searchParams.get('sessionid');
const cookie = (req.headers.cookie || '').match(/(?:^|;\s*)signin=([^;]+)/);
if (!sessionid || !cookie || cookie[1] !== sessionid) {
res.writeHead(400, { 'content-type': 'text/plain' });
res.end('Invalid sign-in.');
return;
}
const response = await fetch(OPENAUTH + '/sessions/?id=' + encodeURIComponent(sessionid), {
headers: { 'x-token': TOKEN }
});
if (!response.ok) {
res.writeHead(401, { 'content-type': 'text/plain' });
res.end('The sign-in has expired. Please try again.');
return;
}
const profile = await response.json();
// 3. Find or create the user in your database by profile.serviceid + profile.id
res.writeHead(200, { 'content-type': 'text/plain' });
res.end('Hello ' + profile.name + ' (' + profile.email + ')');
return;
}
res.writeHead(404, { 'content-type': 'text/plain' });
res.end('Not found');
});
server.listen(3000);Run it
node app.jsPut a link to /login/facebook/ on your sign-in page. After signing in, the user lands on /auth/ and sees a greeting.
How it works
- The session ID is random and is also saved in a cookie. On
/auth/, the server accepts only the session ID that this browser started, so nobody can finish a sign-in with a link they got from someone else. - The token stays on the server; the browser never sees it.
- If the user takes longer than 15 minutes,
/sessions/answers404and the server asks them to try again.
In a real app, replace the greeting with your own session: look up the user by profile.serviceid and profile.id, then sign them in. See The user profile.