new file: begushiybashkir/bbvue/.env

modified:   begushiybashkir/bbvue/src/stores/auth.js
	modified:   serv_nginx/api_bb/internal/handlers/auth.go
fix auth api_bb for debug info
This commit is contained in:
2025-10-10 05:14:00 +05:00
parent 38c1e43ec2
commit 30de2eafdf
3 changed files with 50 additions and 20 deletions
+1
View File
@@ -0,0 +1 @@
VITE_APP_DEBUG=true
+12 -12
View File
@@ -46,23 +46,23 @@ export const useAuthStore = defineStore('auth', () => {
const register = async (userData) => {
loading.value = true
error.value = ''
try {
const response = await axios.post(`${API_BASE_URL}/register`, userData)
// После успешной регистрации автоматически логинимся
const loginResponse = await axios.post(`${API_BASE_URL}/login`, {
email: userData.email,
password: userData.password
})
const { token: authToken, user: userInfo } = loginResponse.data
setToken(authToken)
setUser(userInfo)
return { success: true, data: response.data }
} catch (err) {
error.value = err.response?.data?.message || 'Ошибка регистрации'
error.value = err.response?.data?.message || err.message || 'Ошибка регистрации'
return { success: false, error: error.value }
} finally {
loading.value = false
@@ -73,14 +73,14 @@ export const useAuthStore = defineStore('auth', () => {
const login = async (credentials) => {
loading.value = true
error.value = ''
try {
const response = await axios.post(`${API_BASE_URL}/login`, credentials)
const { token: authToken, user: userInfo } = response.data
setToken(authToken)
setUser(userInfo)
return { success: true, data: response.data }
} catch (err) {
error.value = err.response?.data?.message || 'Ошибка входа'
@@ -93,7 +93,7 @@ export const useAuthStore = defineStore('auth', () => {
// Выход
const logout = async () => {
loading.value = true
try {
await axios.post(`${API_BASE_URL}/logout`, {}, {
headers: {
@@ -113,7 +113,7 @@ export const useAuthStore = defineStore('auth', () => {
const fetchProfile = async () => {
loading.value = true
error.value = ''
try {
const response = await axios.get(`${API_BASE_URL}/profile`)
setUser(response.data)
@@ -144,11 +144,11 @@ export const useAuthStore = defineStore('auth', () => {
token,
loading,
error,
// Getters
isAuthenticated,
userFullName,
// Actions
register,
login,
+37 -8
View File
@@ -83,25 +83,53 @@ type UserResponse struct {
}
func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Register request: %+v\n", r)
// Устанавливаем CORS заголовки
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
// Логируем тело запроса для отладки
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
utils.RespondWithError(w, http.StatusBadRequest, "Failed to read request body: "+err.Error())
return
}
// Восстанавливаем тело для дальнейшего использования
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
fmt.Printf("Raw request body: %s\n", string(bodyBytes))
var req RegisterRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
utils.RespondWithError(w, http.StatusBadRequest, "Invalid request payload: "+err.Error())
fmt.Printf("JSON decode error: %v\n", err)
utils.RespondWithError(w, http.StatusBadRequest, "Invalid JSON payload: "+err.Error())
return
}
fmt.Printf("Parsed register request: %+v\n", req)
// Валидация обязательных полей
if req.FirstName == "" || req.LastName == "" || req.Email == "" || req.Password == "" {
utils.RespondWithError(w, http.StatusBadRequest, "First name, last name, email and password are required")
if req.FirstName == "" {
utils.RespondWithError(w, http.StatusBadRequest, "First name is required")
return
}
if req.LastName == "" {
utils.RespondWithError(w, http.StatusBadRequest, "Last name is required")
return
}
if req.Email == "" {
utils.RespondWithError(w, http.StatusBadRequest, "Email is required")
return
}
if req.Password == "" {
utils.RespondWithError(w, http.StatusBadRequest, "Password is required")
return
}
if len(req.Password) < 6 {
utils.RespondWithError(w, http.StatusBadRequest, "Password must be at least 6 characters")
return
}
user := &models.User{
Email: req.Email,
Password: req.Password,
@@ -115,8 +143,9 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
if err := h.authService.Register(user); err != nil {
fmt.Printf("Auth service error: %v\n", err)
utils.RespondWithError(w, http.StatusBadRequest, err.Error())
return
}