lens @ a9a270ba1e1e9add10d75de3e592efee9728c5b4

  1package view
  2
  3import (
  4	"encoding/base64"
  5	"errors"
  6	"net/http"
  7
  8	"git.sr.ht/~gabrielgio/img/pkg/ext"
  9	"git.sr.ht/~gabrielgio/img/pkg/service"
 10	"git.sr.ht/~gabrielgio/img/templates"
 11)
 12
 13type AuthView struct {
 14	userController *service.AuthController
 15}
 16
 17func NewAuthView(userController *service.AuthController) *AuthView {
 18	return &AuthView{
 19		userController: userController,
 20	}
 21}
 22
 23func (v *AuthView) LoginView(w http.ResponseWriter, r *http.Request) error {
 24	templates.WritePageTemplate(w, &templates.LoginPage{}, false)
 25	return nil
 26}
 27
 28func (v *AuthView) Logout(w http.ResponseWriter, r *http.Request) error {
 29	cook := http.Cookie{
 30		Name:     "auth",
 31		Value:    "",
 32		MaxAge:   -1,
 33		HttpOnly: true,
 34		SameSite: http.SameSiteDefaultMode,
 35	}
 36	http.SetCookie(w, &cook)
 37
 38	http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
 39	return nil
 40}
 41
 42func (v *AuthView) Login(w http.ResponseWriter, r *http.Request) error {
 43	var (
 44		username = []byte(r.FormValue("username"))
 45		password = []byte(r.FormValue("password"))
 46	)
 47
 48	auth, err := v.userController.Login(r.Context(), username, password)
 49	if err != nil {
 50		return err
 51	}
 52
 53	if errors.Is(err, service.InvalidLogin) {
 54		templates.WritePageTemplate(w, &templates.LoginPage{
 55			Username: r.FormValue("username"),
 56			Err:      err.Error(),
 57		}, false)
 58		return nil
 59	}
 60
 61	if err != nil {
 62		return err
 63	}
 64
 65	base64Auth := base64.StdEncoding.EncodeToString(auth)
 66
 67	cook := http.Cookie{
 68		Name:     "auth",
 69		Value:    base64Auth,
 70		HttpOnly: true,
 71		SameSite: http.SameSiteDefaultMode,
 72	}
 73	http.SetCookie(w, &cook)
 74
 75	redirect := r.FormValue("redirect")
 76	if redirect == "" {
 77		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
 78	} else {
 79		http.Redirect(w, r, redirect, http.StatusTemporaryRedirect)
 80	}
 81	return nil
 82}
 83
 84func Index(w http.ResponseWriter, r *http.Request) {
 85	http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
 86}
 87
 88func (v *AuthView) InitialRegisterView(w http.ResponseWriter, r *http.Request) error {
 89	templates.WritePageTemplate(w, &templates.RegisterPage{}, false)
 90	return nil
 91}
 92
 93func (v *AuthView) InitialRegister(w http.ResponseWriter, r *http.Request) error {
 94	var (
 95		username = []byte(r.FormValue("username"))
 96		password = []byte(r.FormValue("password"))
 97		path     = []byte(r.FormValue("path"))
 98	)
 99
100	err := v.userController.InitialRegister(r.Context(), username, password, path)
101	if err != nil {
102		return err
103	}
104
105	http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
106	return nil
107}
108
109func (v *AuthView) SetMyselfIn(r *ext.Router) {
110	r.GET("/login", v.LoginView)
111	r.POST("/login", v.Login)
112
113	r.GET("/logout", v.Logout)
114	r.POST("/logout", v.Logout)
115
116	r.GET("/initial", v.InitialRegisterView)
117	r.POST("/initial", v.InitialRegister)
118}