feat: migrate authentication to Appwrite and remove Better-Auth references

This commit is contained in:
2025-09-03 20:03:13 -03:00
parent 7b0526ebee
commit 2dc472c60e
15 changed files with 380 additions and 317 deletions

View File

@@ -1,5 +1,67 @@
import { createAuthClient } from "better-auth/react";
import { useQuery } from "@tanstack/react-query";
import type { Models } from "appwrite";
import { Account, Client, ID } from "appwrite";
export const authClient = createAuthClient({
baseURL: import.meta.env.VITE_SERVER_URL,
});
// Initialize Appwrite web client
export const appwriteClient = new Client()
.setEndpoint(import.meta.env.VITE_APPWRITE_ENDPOINT)
.setProject(import.meta.env.VITE_APPWRITE_PROJECT_ID);
export const account = new Account(appwriteClient);
// Simple session hook using React Query
export function useSession() {
return useQuery({
queryKey: ["session", "me"],
queryFn: async () => {
try {
const me = await account.get();
return me as Models.User<Models.Preferences>;
} catch {
return null;
}
},
staleTime: 30_000,
});
}
// Sign up, sign in, sign out helpers
export const authClient = {
useSession,
signUp: {
email: async ({
email,
password,
name,
}: {
email: string;
password: string;
name?: string;
}) => {
await account.create(ID.unique(), email, password, name);
// Immediately create session after sign up
await account.createEmailPasswordSession(email, password);
},
},
signIn: {
email: async ({ email, password }: { email: string; password: string }) => {
await account.createEmailPasswordSession(email, password);
},
},
signOut: async () => {
try {
await account.deleteSessions();
} catch {
// ignore
}
},
// Get a short-lived JWT for server-side calls (15 min)
getJWT: async (): Promise<string | null> => {
try {
const jwt = await account.createJWT();
return jwt.jwt ?? null;
} catch {
return null;
}
},
};