User sessions
Exercise email sign-in, rejected credentials, and session refresh through your application
Test how your application handles sign-in and session renewal. The authentication mocks supply upstream responses; your application still handles forms, cookies, and navigation.
Before you start
Complete the Blueprint Auth tutorial. These examples assume:
/loginhas Email and Password fields and a Sign in button/dashboarddisplays the authenticated viewer’s name- Failed sign-in displays an alert, and failed refresh redirects to
/login
Adapt those routes, labels, and messages to your app. For an OAuth redirect flow, use the OAuth guide.
Start a test file with the viewer-data mock from the tutorial. It supplies the dashboard data after authentication succeeds:
import { HttpResponse } from "msw";
import { expect, test } from "../../lib/playwright/test";
test.beforeEach(({ msw }) => {
msw.use(
msw.graphql
.link("https://api.kraken.test/v1/graphql/")
.query("viewer", () => HttpResponse.json({ data: { viewer: { preferredName: "Alice" } } })),
);
});Test successful sign-in
Register the response before submitting the form. The credentials in the mock must match those entered by the user:
test("signs in with email", async ({ page, blueprintAuth }) => {
await blueprintAuth.mock.email.login({
credentials: { email: "alice@example.test", password: "test_password" },
session: { subject: "alice" },
});
await page.goto("/login");
await page.getByLabel("Email").fill("alice@example.test");
await page.getByLabel("Password").fill("test_password");
await page.getByRole("button", { name: "Sign in" }).click();
await expect(page).toHaveURL("/dashboard");
await expect(page.getByRole("heading", { name: "Alice", exact: true })).toBeVisible();
});Test rejected credentials
Use an error outcome instead of a session, then check the application’s error UI. This example uses Kraken’s incorrect-credentials code:
test("reports incorrect credentials", async ({ page, blueprintAuth }) => {
await blueprintAuth.mock.email.login({
credentials: { email: "alice@example.test", password: "wrong_password" },
errorCode: "KT-CT-1138",
});
await page.goto("/login");
await page.getByLabel("Email").fill("alice@example.test");
await page.getByLabel("Password").fill("wrong_password");
await page.getByRole("button", { name: "Sign in" }).click();
await expect(page).toHaveURL("/login");
await expect(page.getByRole("alert")).toContainText("Unable to sign in");
});Refresh an expired session
Seed an expired session, then configure a replacement for its refresh token. Set expires: -1 to keep the browser cookies alive: the application needs to receive the expired access token to attempt refresh.
test("refreshes an expired session", async ({ page, blueprintAuth }) => {
const tokens = await blueprintAuth.login(
{
authMethod: "email",
subject: "alice",
issuedAt: new Date(Date.now() - 7_200_000),
expiresAt: new Date(Date.now() - 3_600_000),
},
{ expires: -1 },
);
await blueprintAuth.mock.email.refresh({
refreshToken: tokens.refreshToken,
session: { subject: "alice" },
});
await page.goto("/dashboard");
await expect(page.getByRole("heading", { name: "Alice", exact: true })).toBeVisible();
});Handle a rejected refresh token
Start with the same expired session, but return Kraken’s invalid-refresh-token error. Assert your app’s signed-out behavior instead of the dashboard:
test("returns to sign-in when refresh fails", async ({ page, blueprintAuth }) => {
const tokens = await blueprintAuth.login(
{
authMethod: "email",
subject: "alice",
issuedAt: new Date(Date.now() - 7_200_000),
expiresAt: new Date(Date.now() - 3_600_000),
},
{ expires: -1 },
);
await blueprintAuth.mock.email.refresh({
refreshToken: tokens.refreshToken,
errorCode: "KT-CT-1135",
});
await page.goto("/dashboard");
await expect(page).toHaveURL("/login");
});