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) => { const register = async (userData) => {
loading.value = true loading.value = true
error.value = '' error.value = ''
try { try {
const response = await axios.post(`${API_BASE_URL}/register`, userData) const response = await axios.post(`${API_BASE_URL}/register`, userData)
// После успешной регистрации автоматически логинимся // После успешной регистрации автоматически логинимся
const loginResponse = await axios.post(`${API_BASE_URL}/login`, { const loginResponse = await axios.post(`${API_BASE_URL}/login`, {
email: userData.email, email: userData.email,
password: userData.password password: userData.password
}) })
const { token: authToken, user: userInfo } = loginResponse.data const { token: authToken, user: userInfo } = loginResponse.data
setToken(authToken) setToken(authToken)
setUser(userInfo) setUser(userInfo)
return { success: true, data: response.data } return { success: true, data: response.data }
} catch (err) { } catch (err) {
error.value = err.response?.data?.message || 'Ошибка регистрации' error.value = err.response?.data?.message || err.message || 'Ошибка регистрации'
return { success: false, error: error.value } return { success: false, error: error.value }
} finally { } finally {
loading.value = false loading.value = false
@@ -73,14 +73,14 @@ export const useAuthStore = defineStore('auth', () => {
const login = async (credentials) => { const login = async (credentials) => {
loading.value = true loading.value = true
error.value = '' error.value = ''
try { try {
const response = await axios.post(`${API_BASE_URL}/login`, credentials) const response = await axios.post(`${API_BASE_URL}/login`, credentials)
const { token: authToken, user: userInfo } = response.data const { token: authToken, user: userInfo } = response.data
setToken(authToken) setToken(authToken)
setUser(userInfo) setUser(userInfo)
return { success: true, data: response.data } return { success: true, data: response.data }
} catch (err) { } catch (err) {
error.value = err.response?.data?.message || 'Ошибка входа' error.value = err.response?.data?.message || 'Ошибка входа'
@@ -93,7 +93,7 @@ export const useAuthStore = defineStore('auth', () => {
// Выход // Выход
const logout = async () => { const logout = async () => {
loading.value = true loading.value = true
try { try {
await axios.post(`${API_BASE_URL}/logout`, {}, { await axios.post(`${API_BASE_URL}/logout`, {}, {
headers: { headers: {
@@ -113,7 +113,7 @@ export const useAuthStore = defineStore('auth', () => {
const fetchProfile = async () => { const fetchProfile = async () => {
loading.value = true loading.value = true
error.value = '' error.value = ''
try { try {
const response = await axios.get(`${API_BASE_URL}/profile`) const response = await axios.get(`${API_BASE_URL}/profile`)
setUser(response.data) setUser(response.data)
@@ -144,11 +144,11 @@ export const useAuthStore = defineStore('auth', () => {
token, token,
loading, loading,
error, error,
// Getters // Getters
isAuthenticated, isAuthenticated,
userFullName, userFullName,
// Actions // Actions
register, register,
login, login,
+37 -8
View File
@@ -83,25 +83,53 @@ type UserResponse struct {
} }
func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) { func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Register request: %+v\n", r)
// Устанавливаем CORS заголовки // Устанавливаем CORS заголовки
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin")) w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true") 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 var req RegisterRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil { 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 return
} }
fmt.Printf("Parsed register request: %+v\n", req)
// Валидация обязательных полей // Валидация обязательных полей
if req.FirstName == "" || req.LastName == "" || req.Email == "" || req.Password == "" { if req.FirstName == "" {
utils.RespondWithError(w, http.StatusBadRequest, "First name, last name, email and password are required") utils.RespondWithError(w, http.StatusBadRequest, "First name is required")
return 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{ user := &models.User{
Email: req.Email, Email: req.Email,
Password: req.Password, Password: req.Password,
@@ -115,8 +143,9 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
CreatedAt: time.Now(), CreatedAt: time.Now(),
UpdatedAt: time.Now(), UpdatedAt: time.Now(),
} }
if err := h.authService.Register(user); err != nil { if err := h.authService.Register(user); err != nil {
fmt.Printf("Auth service error: %v\n", err)
utils.RespondWithError(w, http.StatusBadRequest, err.Error()) utils.RespondWithError(w, http.StatusBadRequest, err.Error())
return return
} }