@kaaarel/auth
Docs: Testing and dev login
View .md

Testing and dev login

In tests

import { signInAs } from "@kaaarel/auth/testing";
import { auth } from "~/auth";

test("the deals page needs a session", async () => {
  const { user, cookie, request } = await signInAs(auth, "ann@example.com");
  const response = await app.fetch(request("/app/deals"));
  expect(response.status).toBe(200);
});
  • signInAs creates the user when the e-mail is new, and a session for it.
  • request(path, init) returns a Request with the session cookie, and a same-origin Origin header for non-GET requests.
  • cookie is the Cookie header value, for other HTTP clients.

Use rateLimit: false in tests that sign in many times. Use a separate schema per test file to keep tests apart. E-mails go out in the background: await auth.settled() before you read them.

Local development

devLogin: process.env.DEV_LOGIN, // e.g. "me@example.com"

Open http://localhost:3000/auth/dev?next=/admin to sign in as that e-mail. It creates the user when needed. Use it when Google login cannot work locally, for example on a review proxy.

Dev login works only when all of these hold:

  • NODE_ENV is not production;
  • the url host is localhost, 127.0.0.1, *.localhost or *.test.

In every other case /auth/dev answers 404. doctor warns while it is on.