lens @ 869f50db2fbc24826762fb61d35e5f8de79296c1

  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, _ *http.Request) error {
 24	templates.WritePageTemplate(w, &templates.LoginPage{})
 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
 50	if errors.Is(err, service.InvalidLogin) {
 51		templates.WritePageTemplate(w, &templates.LoginPage{
 52			Username: r.FormValue("username"),
 53			Err:      err.Error(),
 54		})
 55		return nil
 56	}
 57
 58	if err != nil {
 59		return err
 60	}
 61
 62	base64Auth := base64.StdEncoding.EncodeToString(auth)
 63
 64	cook := http.Cookie{
 65		Name:     "auth",
 66		Value:    base64Auth,
 67		HttpOnly: true,
 68		SameSite: http.SameSiteDefaultMode,
 69	}
 70	http.SetCookie(w, &cook)
 71
 72	redirect := r.FormValue("redirect")
 73	if redirect == "" {
 74		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
 75	} else {
 76		http.Redirect(w, r, redirect, http.StatusTemporaryRedirect)
 77	}
 78	return nil
 79}
 80
 81func Index(w http.ResponseWriter, r *http.Request) {
 82	http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
 83}
 84
 85func (v *AuthView) InitialRegisterView(w http.ResponseWriter, _ *http.Request) error {
 86	templates.WritePageTemplate(w, &templates.RegisterPage{})
 87	return nil
 88}
 89
 90func (v *AuthView) InitialRegister(w http.ResponseWriter, r *http.Request) error {
 91	var (
 92		username = []byte(r.FormValue("username"))
 93		password = []byte(r.FormValue("password"))
 94		path     = []byte(r.FormValue("path"))
 95	)
 96
 97	err := v.userController.InitialRegister(r.Context(), username, password, path)
 98	if err != nil {
 99		return err
100	}
101
102	http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
103	return nil
104}
105
106func (v *AuthView) SetMyselfIn(r *ext.Router) {
107	r.GET("/login", v.LoginView)
108	r.POST("/login", v.Login)
109
110	r.GET("/logout", v.Logout)
111	r.POST("/logout", v.Logout)
112
113	r.GET("/initial", v.InitialRegisterView)
114	r.POST("/initial", v.InitialRegister)
115}