-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added profile handler #28
Conversation
marrriamme
commented
Mar 7, 2025
- Добавила ручку профиля
- Обновила модели
- Добавила тесты и моки для ручки профиля
internal/transport/user.go
Outdated
|
||
utils.SendSuccessResponse(w, http.StatusOK, user) | ||
} | ||
|
||
var ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Переменные обычно объявляются вверху файла, до функций
internal/transport/user.go
Outdated
user := models.User{ | ||
ID: userRepo.ID, | ||
Email: userRepo.Email, | ||
Name: userRepo.Name, | ||
Surname: userRepo.Surname, | ||
PhoneNumber: userRepo.PhoneNumber, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Давай для этого сделаем отдельную функцию(или метод) в файле models/user.go
Подобный уже есть в файле models/product.go, можешь посмотреть как там
internal/transport/user.go
Outdated
return | ||
} | ||
|
||
userRepo, err := h.repo.GetUserByID(uid) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я бы просто в user переименовал, так понятнее
internal/transport/user.go
Outdated
} | ||
|
||
// Проверяем, совпадает ли версия пользователя | ||
if userRepo.Version != version { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тут тоже можно сделать метод для модельки UserRepo, который принимает version и делает это сравнение, возвращает соответственно bool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Комменты оставил, плюс как договорились с фронтом - при авторизации будем ставить HttpOnly cookie, вместо того, чтобы отправлять токен в теле ответа. Выглядеть это будет примерно так:
cookie := &http.Cookie{
Name: "token",
Value: "<токен, который отправлялся в теле ответа>",
HttpOnly: true,
Path: "/", // чтобы кука отправлялась на все запросы к нашему домену
MaxAge: <время жизни куки в секундах, равно времени жизни токена, можно вынести в константу>,
SameSite: http.SameSiteStrictMode, // чтобы могла отправляться только с нашего сайта
}
Соответственно в middleware авторизации надо будет получать эту куку и проверять токен, который в ней хранится(не сильно код должен поменяться)
internal/transport/user.go
Outdated
@@ -168,14 +169,49 @@ func (h *AuthHandler) Logout(w http.ResponseWriter, r *http.Request) { | |||
return | |||
} | |||
|
|||
utils.Cookie(w, "", string(utils.Token)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тут бы лучше присвоить куке время из прошлого, чтобы она браузером удалилась
Expires: time.Now().UTC().AddDate(0, 0, -1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Спасибо, поправила
package utils | ||
|
||
type CookieKeys string | ||
|
||
const ( | ||
Token CookieKeys = "token" | ||
UserIDKey = "userID" | ||
UserVersionKey = "userVersion" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Перенеси это в файл utils/cookie.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Перенесла
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Два мини коммента, поправь и вливай