feat: Implement email/password and X OAuth authentication, replacing the dev-login mechanism.
This commit is contained in:
@@ -5,20 +5,13 @@ const assert = require("node:assert/strict");
|
||||
const { createBetterAuthAdapter } = require("../src/integrations/better-auth");
|
||||
|
||||
function createMockAuthHandler() {
|
||||
const signedIn = new Set();
|
||||
|
||||
return async function handler(request) {
|
||||
const url = new URL(request.url);
|
||||
const path = url.pathname;
|
||||
|
||||
if (path.endsWith("/sign-in/email")) {
|
||||
return new Response(JSON.stringify({ ok: false }), { status: 401 });
|
||||
}
|
||||
|
||||
if (path.endsWith("/sign-up/email")) {
|
||||
const body = await request.json();
|
||||
const userId = String(body.name);
|
||||
signedIn.add(userId);
|
||||
const userId = String(body.email);
|
||||
return new Response(JSON.stringify({ ok: true }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
@@ -28,6 +21,28 @@ function createMockAuthHandler() {
|
||||
});
|
||||
}
|
||||
|
||||
if (path.endsWith("/sign-up/email")) {
|
||||
const body = await request.json();
|
||||
const userId = String(body.email);
|
||||
return new Response(JSON.stringify({ ok: true }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"set-cookie": `xartaudio_better_auth=${userId}; Path=/; HttpOnly`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (path.endsWith("/sign-in/social")) {
|
||||
return new Response(JSON.stringify({ url: "https://x.com/i/oauth2/authorize?state=test" }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
"set-cookie": "xartaudio_oauth_state=test; Path=/; HttpOnly",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (path.endsWith("/get-session")) {
|
||||
const cookie = request.headers.get("cookie") || "";
|
||||
const match = cookie.match(/xartaudio_better_auth=([^;]+)/);
|
||||
@@ -62,15 +77,33 @@ function createMockAuthHandler() {
|
||||
};
|
||||
}
|
||||
|
||||
test("signInDevUser signs up and returns cookie when user does not exist", async () => {
|
||||
test("signInWithEmail returns session cookie", async () => {
|
||||
const adapter = createBetterAuthAdapter({
|
||||
appBaseUrl: "http://localhost:3000",
|
||||
secret: "test-secret",
|
||||
authHandler: createMockAuthHandler(),
|
||||
});
|
||||
|
||||
const result = await adapter.signInDevUser("alice");
|
||||
assert.match(String(result.setCookie), /xartaudio_better_auth=alice/);
|
||||
const result = await adapter.signInWithEmail({
|
||||
email: "alice@example.com",
|
||||
password: "password123",
|
||||
});
|
||||
assert.match(String(result.setCookie), /xartaudio_better_auth=alice@example\.com/);
|
||||
});
|
||||
|
||||
test("signUpWithEmail returns session cookie", async () => {
|
||||
const adapter = createBetterAuthAdapter({
|
||||
appBaseUrl: "http://localhost:3000",
|
||||
secret: "test-secret",
|
||||
authHandler: createMockAuthHandler(),
|
||||
});
|
||||
|
||||
const result = await adapter.signUpWithEmail({
|
||||
name: "Alice",
|
||||
email: "alice@example.com",
|
||||
password: "password123",
|
||||
});
|
||||
assert.match(String(result.setCookie), /xartaudio_better_auth=alice@example\.com/);
|
||||
});
|
||||
|
||||
test("getAuthenticatedUserId resolves session user from better-auth endpoint", async () => {
|
||||
@@ -86,6 +119,23 @@ test("getAuthenticatedUserId resolves session user from better-auth endpoint", a
|
||||
assert.equal(userId, "bob");
|
||||
});
|
||||
|
||||
test("getXAuthorizationUrl returns provider url when x oauth is configured", async () => {
|
||||
const adapter = createBetterAuthAdapter({
|
||||
appBaseUrl: "http://localhost:3000",
|
||||
secret: "test-secret",
|
||||
xOAuthClientId: "x-client-id",
|
||||
xOAuthClientSecret: "x-client-secret",
|
||||
authHandler: createMockAuthHandler(),
|
||||
});
|
||||
|
||||
const result = await adapter.getXAuthorizationUrl({
|
||||
callbackURL: "http://localhost:3000/app",
|
||||
});
|
||||
|
||||
assert.match(result.url, /^https:\/\/x\.com\/i\/oauth2\/authorize/);
|
||||
assert.match(String(result.setCookie), /xartaudio_oauth_state=test/);
|
||||
});
|
||||
|
||||
test("handleRoute proxies requests into better-auth handler", async () => {
|
||||
const adapter = createBetterAuthAdapter({
|
||||
appBaseUrl: "http://localhost:3000",
|
||||
|
||||
Reference in New Issue
Block a user