xtablo-source/packages/shared/src/hooks/auth.ts
Arthur Belleville 6081ada013 refactor(main): remove all Stream Chat dependencies and components
- Delete ChatProvider, ChannelPreview, CustomChannelHeader, hooks/channel.ts
- Replace TabloDiscussionSection with chatscope-based implementation using useChat
- Update tablo-details.tsx to use useChatUnread instead of useTabloDiscussionUnread
- Remove streamToken field from User type in UserStoreProvider
- Remove useSignUpToStream from shared auth hooks
- Remove stream-chat and stream-chat-react packages
- Remove stream-chat-react CSS import from main.tsx
- Clean up all streamToken references from test mocks and helpers
- Update chat.test.tsx and tablo-details.layout.test.tsx for new implementation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 13:49:57 +02:00

79 lines
2.3 KiB
TypeScript

import type { Session, SupabaseClient, User as SupabaseUser } from "@supabase/supabase-js";
import { useMutation } from "@tanstack/react-query";
import type { AxiosInstance } from "axios";
import { useState } from "react";
import { match } from "ts-pattern";
import { toast } from "../lib/toast";
interface AuthResponse {
user: SupabaseUser | null;
session: Session | null;
}
export function useSignUpWithoutPassword(supabase: SupabaseClient, api: AxiosInstance) {
const [errors, setErrors] = useState<Record<string, string>>({});
const { mutateAsync, isPending } = useMutation<
AuthResponse,
{ message: string; code: string },
{ email: string; name: string }
>({
mutationFn: async (data: { email: string; name: string }) => {
// Generate a temporary password for the user
const tempPassword =
Math.random().toString(36).slice(-8) + Math.random().toString(36).slice(-8);
const { data: response, error } = await supabase.auth.signUp({
email: data.email.trim(),
password: tempPassword,
options: {
data: {
first_name: data.name.trim().split(" ")[0] || "",
last_name: data.name.trim().split(" ").slice(1).join(" ") || "",
business_name: "",
},
},
});
if (error) throw error;
// Mark the user as temporary
if (response.session?.access_token) {
await api.post(
"/api/v1/users/mark-temporary",
{
temporary_password: tempPassword,
},
{
headers: {
Authorization: `Bearer ${response.session.access_token}`,
},
}
);
}
return response;
},
onError: (error) => {
const errMap: Record<string, string> = {};
match(error.code)
.with("user_already_exists", () => {
errMap.email = "Cette adresse email est déjà utilisée";
})
.otherwise(() => {
toast.add(
{
title: "Erreur",
description: error.message,
type: "error",
position: "top-left",
},
{
timeout: 5000,
}
);
});
setErrors(errMap);
},
});
return { mutateAsync, isPending, errors };
}