Skip to main content

Sign in with a form in your app

Send an email and a password that your own form collected to LumoAuth. This is the legacy option, for apps that are not a browser page.

RecipeReadyView source
Works with
JavaScript / TypeScriptPlain HTTP
Standards
RFC 7636

What it shows​

  1. Your form collects the email address and the password.
  2. The app sends both to the JSON login endpoint. LumoAuth answers with a status and a session cookie, never with a token.
  3. The app starts the normal sign-in with PKCE and sends the cookie. LumoAuth answers with the code and shows no sign-in page.
  4. The app exchanges the code for tokens and reads the profile.

The programs stand for an app that is not a browser page: a native app, a command-line tool, or a backend that keeps the cookies itself. A browser app on its own address cannot make this call, see Limits.

Before you start​

  • A public application with a redirect URI. The Starter App of a new organization is one. Copy its client ID from Applications in the portal.
  • A user who signs in with a password and has no second factor.
  • That user has approved the application once. The first sign-in of a person to an application shows the consent page Authorize with the name of the application, and that page needs a browser. Sign in once with the hosted page recipe and click Authorize.
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_CLIENT_IDThe client ID of a public application, for example the Starter App.
USER_EMAILThe email address that your form collected.
USER_PASSWORDThe password that your form collected.
REDIRECT_URIOptional. A redirect URI of the application. The default is http://localhost:3000/auth/callback, which the Starter App has. Nothing needs to listen there.

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization.

sign-in/form-in-your-app/js/sign-in.mjs
// Sign a person in with an email and a password that your own form collected.
// This program stands for an app that is not a browser page: a native app, a
// command-line tool, or a backend that keeps the cookies itself.
//
// node sign-in/form-in-your-app/js/sign-in.mjs
import { AuthModule } from '@lumoauth/client';

const redirectUri = process.env.REDIRECT_URI ?? 'http://localhost:3000/auth/callback';

// A browser keeps the session cookie by itself. This program keeps it here,
// and it reads each redirect instead of following it.
const cookies = new Map();
async function fetchWithCookies(url, init = {}) {
const headers = new Headers(init.headers);
if (cookies.size > 0) headers.set('Cookie', [...cookies].map(([name, value]) => `${name}=${value}`).join('; '));
const response = await fetch(url, { ...init, headers, redirect: 'manual' });
for (const line of response.headers.getSetCookie()) {
const pair = line.split(';')[0];
cookies.set(pair.slice(0, pair.indexOf('=')), pair.slice(pair.indexOf('=') + 1));
}
return response;
}

const auth = new AuthModule({
baseUrl: process.env.LUMOAUTH_URL,
orgId: process.env.LUMOAUTH_ORG_ID,
clientId: process.env.LUMOAUTH_CLIENT_ID,
fetch: fetchWithCookies,
});

// 1. Send what the form collected. The answer is a status and a session
// cookie, never a token.
const login = await auth.loginWithPassword({
email: process.env.USER_EMAIL,
password: process.env.USER_PASSWORD,
});
console.log(`login: ${login.status}`);
if (login.status !== 'complete') process.exit(1);

// 2. Start the normal sign-in with PKCE. The session exists, so LumoAuth
// answers with the code and shows no sign-in page.
const { url, codeVerifier, state } = await auth.buildAuthorizationUrl({ redirectUri });
const answer = await fetchWithCookies(url);
const callback = new URL(answer.headers.get('location') ?? 'about:blank');
const code = callback.searchParams.get('code');
if (!code || callback.searchParams.get('state') !== state) {
// The first sign-in of a person to an application shows the consent page.
console.error('authorize: no code. LumoAuth shows a page that needs a browser, for example the consent page.');
process.exit(1);
}
console.log('authorize: code received, no sign-in page');

// 3. Exchange the code for tokens, as the callback page of a browser app does.
const tokens = await auth.exchangeCodeForTokens({ code, codeVerifier, redirectUri });
const profile = await auth.getUserInfo(tokens.access_token);
console.log(`signed in as ${profile.email}`);
node sign-in/form-in-your-app/js/sign-in.mjs

Check the result​

Output
login: complete
authorize: code received, no sign-in page
signed in as priya.nair@example.com
  • Run it with a wrong password. The first line is login: invalid_credentials, and the program stops. An unknown address and a locked account get the same answer.
  • Run it for a user who has not approved the application. The second line is authorize: no code, because LumoAuth shows the consent page.
  • The audit log has a user.login event for each run. Open Audit logs in the portal.

Limits​

  • A page in a browser can call the JSON login only from the address of LumoAuth's own pages: the hosted domain or your custom domain. From another origin the answer is 400 with the status invalid_request, also when the origin is under Browser CORS origins of the application. A browser app on its own address uses the hosted page.
  • LumoAuth does not offer the password grant (grant_type=password). The token endpoint answers unsupported_grant_type.
  • Sign-in allows 5 failed attempts for each address in 15 minutes. After that the account is locked for 15 minutes.
  • A user with a second factor gets the status mfa_required and a challenge_url. That page needs a browser.
  • Passkeys, social sign-in and single sign-on are parts of the hosted page. A form in your app does not offer them.