Release v0.20: регистрация, авторизация, личный кабинет
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"shop/internal/auth"
|
||||
"shop/internal/models"
|
||||
)
|
||||
|
||||
type Layout struct {
|
||||
Title string
|
||||
Nav string
|
||||
User *models.User
|
||||
Error string
|
||||
Success string
|
||||
}
|
||||
|
||||
type Pages struct {
|
||||
tmpl *template.Template
|
||||
auth *auth.Service
|
||||
}
|
||||
|
||||
func NewPages(tmpl *template.Template, authSvc *auth.Service) *Pages {
|
||||
return &Pages{tmpl: tmpl, auth: authSvc}
|
||||
}
|
||||
|
||||
func (p *Pages) layout(r *http.Request, title, nav string) Layout {
|
||||
user, _ := p.auth.UserFromRequest(r.Context(), r)
|
||||
return Layout{Title: title, Nav: nav, User: user}
|
||||
}
|
||||
|
||||
func (p *Pages) render(w http.ResponseWriter, name string, data any) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := p.tmpl.ExecuteTemplate(w, name, data); err != nil {
|
||||
http.Error(w, "ошибка шаблона", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func flashMsg(r *http.Request, key string) string {
|
||||
switch r.URL.Query().Get(key) {
|
||||
case "registered":
|
||||
return "Регистрация успешна. Войдите в аккаунт."
|
||||
case "login":
|
||||
return "Вы успешно вошли."
|
||||
case "logout":
|
||||
return "Вы вышли из аккаунта."
|
||||
case "profile":
|
||||
return "Профиль обновлён."
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user