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.
What it shows
- Your form collects the email address and the password.
- The app sends both to the JSON login endpoint. LumoAuth answers with a status and a session cookie, never with a token.
- The app starts the normal sign-in with PKCE and sends the cookie. LumoAuth answers with the code and shows no sign-in page.
- 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.
| Variable | Value |
|---|---|
LUMOAUTH_URL | The address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region. |
LUMOAUTH_ORG_ID | Your organization, for example acme-corp. |
LUMOAUTH_CLIENT_ID | The client ID of a public application, for example the Starter App. |
USER_EMAIL | The email address that your form collected. |
USER_PASSWORD | The password that your form collected. |
REDIRECT_URI | Optional. 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.
- Node.js
- curl
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
sign-in/form-in-your-app/http/sign-in.sh
#!/bin/sh
# Sign a person in with an email and a password that your own form collected,
# with curl and openssl.
#
# sh sign-in/form-in-your-app/http/sign-in.sh
set -eu
API="$LUMOAUTH_URL/orgs/$LUMOAUTH_ORG_ID/api/v1"
REDIRECT_URI="${REDIRECT_URI:-http://localhost:3000/auth/callback}"
# curl keeps the session cookie in this file, as a browser would keep it.
COOKIES="$(mktemp)"
trap 'rm -f "$COOKIES"' EXIT
# 1. Send what the form collected. The answer is a status and a session
# cookie, never a token.
LOGIN="$(curl --silent --show-error "$API/oauth/login/json" \
--cookie-jar "$COOKIES" \
--header "Content-Type: application/json" \
--data "{\"email\": \"$USER_EMAIL\", \"password\": \"$USER_PASSWORD\"}")"
STATUS="$(printf '%s' "$LOGIN" | sed -n 's/.*"status" *: *"\([^"]*\)".*/\1/p')"
echo "login: $STATUS"
[ "$STATUS" = "complete" ] || 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.
VERIFIER="$(openssl rand -hex 32)"
CHALLENGE="$(printf '%s' "$VERIFIER" | openssl dgst -sha256 -binary | openssl base64 -A | tr '+/' '-_' | tr -d '=')"
STATE="$(openssl rand -hex 16)"
CALLBACK="$(curl --silent --show-error --output /dev/null --write-out '%{redirect_url}' \
--cookie "$COOKIES" --get "$API/oauth/authorize" \
--data-urlencode "response_type=code" \
--data-urlencode "client_id=$LUMOAUTH_CLIENT_ID" \
--data-urlencode "redirect_uri=$REDIRECT_URI" \
--data-urlencode "scope=openid profile email" \
--data-urlencode "code_challenge=$CHALLENGE" \
--data-urlencode "code_challenge_method=S256" \
--data-urlencode "state=$STATE")"
CODE="$(printf '%s' "$CALLBACK" | sed -n 's/.*[?&]code=\([^&]*\).*/\1/p')"
case "$CALLBACK" in
*"state=$STATE"*) ;;
*) CODE="" ;;
esac
if [ -z "$CODE" ]; then
# The first sign-in of a person to an application shows the consent page.
echo "authorize: no code. LumoAuth shows a page that needs a browser, for example the consent page." >&2
exit 1
fi
echo "authorize: code received, no sign-in page"
# 3. Exchange the code for tokens, as the callback page of a browser app does.
TOKENS="$(curl --silent --show-error "$API/oauth/token" \
--data-urlencode "grant_type=authorization_code" \
--data-urlencode "code=$CODE" \
--data-urlencode "redirect_uri=$REDIRECT_URI" \
--data-urlencode "client_id=$LUMOAUTH_CLIENT_ID" \
--data-urlencode "code_verifier=$VERIFIER")"
ACCESS_TOKEN="$(printf '%s' "$TOKENS" | sed -n 's/.*"access_token" *: *"\([^"]*\)".*/\1/p')"
EMAIL="$(curl --silent --show-error "$API/oauth/userinfo" --header "Authorization: Bearer $ACCESS_TOKEN" \
| sed -n 's/.*"email" *: *"\([^"]*\)".*/\1/p')"
echo "signed in as $EMAIL"
sh sign-in/form-in-your-app/http/sign-in.sh
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.loginevent 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
400with the statusinvalid_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 answersunsupported_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_requiredand achallenge_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.