mirror of
https://github.com/better-auth/better-auth.git
synced 2026-10-20 20:26:55 -05:00
test: init
This commit is contained in:
Binary file not shown.
@@ -4,3 +4,10 @@ import { auth } from "./auth";
|
||||
export const authClient = createAuthClient<typeof auth>({
|
||||
baseURL: "http://localhost:3000/api/auth",
|
||||
});
|
||||
|
||||
export const {
|
||||
useSession,
|
||||
useActiveOrganization,
|
||||
useInvitation,
|
||||
useListOrganization,
|
||||
} = authClient;
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
||||
|
||||
exports[`init > should match config 1`] = `
|
||||
{
|
||||
"adapter": {
|
||||
"create": [Function],
|
||||
"delete": [Function],
|
||||
"findMany": [Function],
|
||||
"findOne": [Function],
|
||||
"update": [Function],
|
||||
},
|
||||
"authCookies": {
|
||||
"csrfToken": {
|
||||
"name": "better-auth.csrf_token",
|
||||
"options": {
|
||||
"httpOnly": true,
|
||||
"maxAge": 604800,
|
||||
"path": "/",
|
||||
"sameSite": "lax",
|
||||
"secure": false,
|
||||
},
|
||||
},
|
||||
"nonce": {
|
||||
"name": "better-auth.nonce",
|
||||
"options": {
|
||||
"httpOnly": true,
|
||||
"maxAge": 900,
|
||||
"path": "/",
|
||||
"sameSite": "lax",
|
||||
"secure": false,
|
||||
},
|
||||
},
|
||||
"pkCodeVerifier": {
|
||||
"name": "better-auth.pk_code_verifier",
|
||||
"options": {
|
||||
"httpOnly": true,
|
||||
"maxAge": 900,
|
||||
"path": "/",
|
||||
"sameSite": "lax",
|
||||
"secure": false,
|
||||
},
|
||||
},
|
||||
"sessionToken": {
|
||||
"name": "better-auth.session_token",
|
||||
"options": {
|
||||
"httpOnly": true,
|
||||
"maxAge": 604800,
|
||||
"path": "/",
|
||||
"sameSite": "lax",
|
||||
"secure": false,
|
||||
},
|
||||
},
|
||||
"state": {
|
||||
"name": "better-auth.state",
|
||||
"options": {
|
||||
"httpOnly": true,
|
||||
"maxAge": 900,
|
||||
"path": "/",
|
||||
"sameSite": "lax",
|
||||
"secure": false,
|
||||
},
|
||||
},
|
||||
},
|
||||
"baseURL": "http://localhost:3000/api/auth",
|
||||
"createAuthCookie": [Function],
|
||||
"db": Kysely {},
|
||||
"internalAdapter": {
|
||||
"createOAuthUser": [Function],
|
||||
"createSession": [Function],
|
||||
"createUser": [Function],
|
||||
"deleteSession": [Function],
|
||||
"findSession": [Function],
|
||||
"findUserByEmail": [Function],
|
||||
"findUserById": [Function],
|
||||
"linkAccount": [Function],
|
||||
"updatePassword": [Function],
|
||||
"updateSession": [Function],
|
||||
"updateUserByEmail": [Function],
|
||||
},
|
||||
"logger": {
|
||||
"box": [Function],
|
||||
"break": [Function],
|
||||
"debug": [Function],
|
||||
"error": [Function],
|
||||
"info": [Function],
|
||||
"log": [Function],
|
||||
"success": [Function],
|
||||
"warn": [Function],
|
||||
},
|
||||
"options": {
|
||||
"basePath": "/api/auth",
|
||||
"baseURL": "http://localhost:3000",
|
||||
"database": {
|
||||
"provider": "sqlite",
|
||||
"url": ":memory:",
|
||||
},
|
||||
},
|
||||
"secret": "better-auth-secret-123456789",
|
||||
}
|
||||
`;
|
||||
@@ -17,6 +17,7 @@ import { signUpCredential } from "./routes/sign-up";
|
||||
import { parseAccount, parseSession, parseUser } from "../adapters/schema";
|
||||
import { BetterAuthOptions, InferSession, InferUser } from "../types";
|
||||
import { Prettify } from "../types/helper";
|
||||
import { ok } from "./routes/ok";
|
||||
|
||||
export const router = <C extends AuthContext, Option extends BetterAuthOptions>(
|
||||
ctx: C,
|
||||
@@ -92,6 +93,7 @@ export const router = <C extends AuthContext, Option extends BetterAuthOptions>(
|
||||
const endpoints = {
|
||||
...baseEndpoints,
|
||||
...pluginEndpoints,
|
||||
ok,
|
||||
};
|
||||
let api: Record<string, any> = {};
|
||||
for (const [key, value] of Object.entries(endpoints)) {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { createAuthEndpoint } from "../call";
|
||||
|
||||
export const ok = createAuthEndpoint(
|
||||
"/ok",
|
||||
{
|
||||
method: "GET",
|
||||
},
|
||||
async (ctx) => {
|
||||
return ctx.json({
|
||||
ok: true,
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -1,56 +1,24 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { getTestInstance } from "../../test-utils/test-instance";
|
||||
import { createAuthClient } from "../../client";
|
||||
|
||||
describe("signIn", async () => {
|
||||
const app = await getTestInstance();
|
||||
it("should sign up with credential", async () => {
|
||||
const res = await app.api.signUpCredential({
|
||||
body: {
|
||||
email: "test@test.com",
|
||||
password: "test1234",
|
||||
name: "test",
|
||||
},
|
||||
});
|
||||
expect(res).toMatchObject({
|
||||
user: {
|
||||
id: expect.any(String),
|
||||
email: "test@test.com",
|
||||
name: "test",
|
||||
image: undefined,
|
||||
emailVerified: false,
|
||||
createdAt: expect.any(Date),
|
||||
updatedAt: expect.any(Date),
|
||||
},
|
||||
session: {
|
||||
id: expect.any(String),
|
||||
userId: expect.any(String),
|
||||
expiresAt: expect.any(Date),
|
||||
},
|
||||
});
|
||||
const auth = await getTestInstance();
|
||||
const client = createAuthClient<typeof auth>({
|
||||
customFetchImpl: async (url, init) => {
|
||||
const req = new Request(url.toString(), init);
|
||||
const res = await auth.handler(req);
|
||||
return res;
|
||||
},
|
||||
csrfPlugin: false,
|
||||
});
|
||||
|
||||
it("should sign in with credential", async () => {
|
||||
const res = await app.api.signInCredential({
|
||||
body: {
|
||||
email: "test@test.com",
|
||||
password: "test1234",
|
||||
},
|
||||
});
|
||||
expect(res).toMatchObject({
|
||||
user: {
|
||||
id: expect.any(String),
|
||||
email: "test@test.com",
|
||||
name: "test",
|
||||
image: null,
|
||||
emailVerified: false,
|
||||
createdAt: expect.any(Date),
|
||||
updatedAt: expect.any(Date),
|
||||
},
|
||||
session: {
|
||||
id: expect.any(String),
|
||||
userId: expect.any(String),
|
||||
expiresAt: expect.any(Date),
|
||||
},
|
||||
});
|
||||
it("should sign up with email and password", async () => {
|
||||
// const res = await client.signUp.credential({
|
||||
// email: "test@test.com",
|
||||
// password: "test",
|
||||
// name: "test",
|
||||
// });
|
||||
// console.log(res);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,7 +13,6 @@ export const betterAuth = <O extends BetterAuthOptions>(options: O) => {
|
||||
: {}
|
||||
: {}
|
||||
>;
|
||||
|
||||
const { handler, endpoints } = router(authContext, options);
|
||||
type Endpoint = typeof endpoints;
|
||||
return {
|
||||
|
||||
@@ -10,7 +10,7 @@ import { ClientOptions, HasPlugin } from "./type";
|
||||
import { getBaseURL } from "../utils/base-url";
|
||||
import type { router } from "../api";
|
||||
|
||||
export const createVanillaClient = <Auth extends BetterAuth = never>(
|
||||
export const createAuthClient = <Auth extends BetterAuth = never>(
|
||||
options?: ClientOptions,
|
||||
) => {
|
||||
type BAuth = Auth extends never ? BetterAuth : Auth;
|
||||
@@ -18,9 +18,15 @@ export const createVanillaClient = <Auth extends BetterAuth = never>(
|
||||
? ReturnType<typeof router>["endpoints"]
|
||||
: BAuth["api"];
|
||||
const $fetch = createFetch({
|
||||
method: "GET",
|
||||
...options,
|
||||
baseURL: getBaseURL(options?.baseURL).withPath,
|
||||
plugins: [redirectPlugin, addCurrentURL, csrfPlugin],
|
||||
plugins: [
|
||||
redirectPlugin,
|
||||
addCurrentURL,
|
||||
...(options?.csrfPlugin !== false ? [csrfPlugin] : []),
|
||||
...(options?.plugins || []),
|
||||
],
|
||||
});
|
||||
const { $session, $sessionSignal } = getSessionAtom<Auth>($fetch);
|
||||
const { signInPasskey, register } = getPasskeyActions($fetch);
|
||||
|
||||
@@ -39,6 +39,8 @@ export const csrfPlugin = {
|
||||
csrfToken: string;
|
||||
}>("/csrf", {
|
||||
baseURL: options.baseURL,
|
||||
...options,
|
||||
plugins: [],
|
||||
});
|
||||
if (error?.status === 404) {
|
||||
throw new BetterAuthError(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useStore } from "@nanostores/react";
|
||||
export const useAuthStore = useStore;
|
||||
|
||||
import { createVanillaClient } from "./base";
|
||||
import { createAuthClient as createVanillaClient } from "./base";
|
||||
import { BetterFetchOption } from "@better-fetch/fetch";
|
||||
|
||||
export const createAuthClient = (options?: BetterFetchOption) => {
|
||||
|
||||
@@ -65,10 +65,6 @@ export function createDynamicPathProxy<T extends Record<string, any>>(
|
||||
method,
|
||||
onSuccess() {
|
||||
const signal = $signal?.find((s) => s.matcher(routePath));
|
||||
console.log({
|
||||
signal,
|
||||
routePath,
|
||||
});
|
||||
if (signal) {
|
||||
signal.atom.set(!signal.atom.get());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useStore } from "@nanostores/react";
|
||||
import { createVanillaClient } from "./base";
|
||||
import { createAuthClient as createVanillaClient } from "./base";
|
||||
import { BetterFetchOption } from "@better-fetch/fetch";
|
||||
import { BetterAuth } from "../auth";
|
||||
import { InferSession, InferUser } from "../types";
|
||||
|
||||
@@ -5,7 +5,12 @@ import type { useAuthStore as reactStore } from "./react";
|
||||
import type { useAuthStore as vueStore } from "./vue";
|
||||
|
||||
export type AuthStore = typeof reactStore | typeof vueStore;
|
||||
export interface ClientOptions extends BetterFetchOption {}
|
||||
export interface ClientOptions extends BetterFetchOption {
|
||||
/**
|
||||
* csrf plugin is enabled by default
|
||||
*/
|
||||
csrfPlugin?: boolean;
|
||||
}
|
||||
|
||||
export type HasPlugin<
|
||||
PluginId extends string,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useStore } from "@nanostores/vue";
|
||||
import { BetterFetchOption } from "@better-fetch/fetch";
|
||||
import { createVanillaClient } from "./base";
|
||||
import { createAuthClient as createVanillaClient } from "./base";
|
||||
|
||||
export const createAuthClient = (options?: BetterFetchOption) => {
|
||||
const client = createVanillaClient(options);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { init } from "./init";
|
||||
import { createAuthClient } from "./client";
|
||||
|
||||
describe("init", async () => {
|
||||
const client = createAuthClient();
|
||||
|
||||
it("should match config", () => {
|
||||
const res = init({
|
||||
database: {
|
||||
provider: "sqlite",
|
||||
url: ":memory:",
|
||||
},
|
||||
});
|
||||
expect(res).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("should respond ok endpoint", async () => {
|
||||
const res = await client.$fetch<{ ok: boolean }>("/ok");
|
||||
expect(res.data?.ok).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -19,6 +19,7 @@ export const init = (options: BetterAuthOptions) => {
|
||||
options: {
|
||||
...options,
|
||||
baseURL: baseURL,
|
||||
basePath: options.basePath || "/api/auth",
|
||||
},
|
||||
baseURL: withPath,
|
||||
secret:
|
||||
|
||||
Reference in New Issue
Block a user