feat(auth): implement registration page and logic
This commit is contained in:
39
src/stores/auth.ts
Normal file
39
src/stores/auth.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { ref } from "vue";
|
||||
import { registerUser as apiRegisterUser } from "@/api/auth";
|
||||
import type { RegisterRequest } from "@/types";
|
||||
import router from "@/router";
|
||||
|
||||
export const useAuthStore = defineStore("auth", () => {
|
||||
// State
|
||||
const isLoading = ref(false);
|
||||
const error = ref<string | null>(null);
|
||||
|
||||
// Actions
|
||||
async function handleRegister(userData: RegisterRequest) {
|
||||
isLoading.value = true;
|
||||
error.value = null;
|
||||
try {
|
||||
await apiRegisterUser(userData);
|
||||
// После успешной регистрации перенаправляем на страницу входа
|
||||
await router.push({ name: "login" });
|
||||
// Можно добавить сообщение об успехе ("тост")
|
||||
// alert('Регистрация прошла успешно! Теперь вы можете войти.');
|
||||
} catch (e: any) {
|
||||
// Обрабатываем ошибки от Axios
|
||||
if (e.response && e.response.data) {
|
||||
error.value = e.response.data;
|
||||
} else {
|
||||
error.value = "Произошла неизвестная ошибка";
|
||||
}
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
isLoading,
|
||||
error,
|
||||
handleRegister,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user