27 lines
776 B
TypeScript
27 lines
776 B
TypeScript
"use client";
|
|
import { useMutation } from "@tanstack/react-query";
|
|
import { apiClient } from "@/lib/apiClient";
|
|
import { useAuthStore } from "@/stores/authStore";
|
|
import type { RegisterRequest, LoginResponse } from "@/types/auth";
|
|
|
|
async function registerFn(data: RegisterRequest): Promise<LoginResponse> {
|
|
const res = await apiClient.post<LoginResponse>("/api/auth/register", data);
|
|
return res.data;
|
|
}
|
|
|
|
export function useRegister(onSuccess?: () => void) {
|
|
const { login } = useAuthStore();
|
|
|
|
return useMutation({
|
|
mutationFn: registerFn,
|
|
onSuccess: (data) => {
|
|
login(
|
|
{ id: "", name: "", email: "", role: "student" },
|
|
data.token,
|
|
);
|
|
localStorage.setItem("refreshToken", data.refreshToken);
|
|
onSuccess?.();
|
|
},
|
|
});
|
|
}
|