aboutsummaryrefslogtreecommitdiff
path: root/pkg/view/auth.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/view/auth.go')
-rw-r--r--pkg/view/auth.go77
1 files changed, 41 insertions, 36 deletions
diff --git a/pkg/view/auth.go b/pkg/view/auth.go
index 631cfb3..2a4b95e 100644
--- a/pkg/view/auth.go
+++ b/pkg/view/auth.go
@@ -2,8 +2,7 @@ package view
import (
"encoding/base64"
-
- "github.com/valyala/fasthttp"
+ "net/http"
"git.sr.ht/~gabrielgio/img/pkg/ext"
"git.sr.ht/~gabrielgio/img/pkg/service"
@@ -20,71 +19,77 @@ func NewAuthView(userController *service.AuthController) *AuthView {
}
}
-func (v *AuthView) LoginView(ctx *fasthttp.RequestCtx) error {
- templates.WritePageTemplate(ctx, &templates.LoginPage{})
+func (v *AuthView) LoginView(w http.ResponseWriter, r *http.Request) error {
+ templates.WritePageTemplate(w, &templates.LoginPage{})
return nil
}
-func (v *AuthView) Logout(ctx *fasthttp.RequestCtx) error {
- cook := fasthttp.Cookie{}
- cook.SetKey("auth")
- cook.SetValue("")
- cook.SetMaxAge(-1)
- cook.SetHTTPOnly(true)
- cook.SetSameSite(fasthttp.CookieSameSiteDefaultMode)
- ctx.Response.Header.SetCookie(&cook)
+func (v *AuthView) Logout(w http.ResponseWriter, r *http.Request) error {
+ cook := http.Cookie{
+ Name: "auth",
+ Value: "",
+ MaxAge: -1,
+ HttpOnly: true,
+ SameSite: http.SameSiteDefaultMode,
+ }
+ http.SetCookie(w, &cook)
- ctx.Redirect("/", 307)
+ http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return nil
}
-func (v *AuthView) Login(ctx *fasthttp.RequestCtx) error {
- username := ctx.FormValue("username")
- password := ctx.FormValue("password")
+func (v *AuthView) Login(w http.ResponseWriter, r *http.Request) error {
+ var (
+ username = []byte(r.FormValue("username"))
+ password = []byte(r.FormValue("password"))
+ )
- auth, err := v.userController.Login(ctx, username, password)
+ auth, err := v.userController.Login(r.Context(), username, password)
if err != nil {
return err
}
base64Auth := base64.StdEncoding.EncodeToString(auth)
- cook := fasthttp.Cookie{}
- cook.SetKey("auth")
- cook.SetValue(base64Auth)
- cook.SetHTTPOnly(true)
- cook.SetSameSite(fasthttp.CookieSameSiteDefaultMode)
- ctx.Response.Header.SetCookie(&cook)
+ cook := http.Cookie{
+ Name: "auth",
+ Value: base64Auth,
+ HttpOnly: true,
+ SameSite: http.SameSiteDefaultMode,
+ }
+ http.SetCookie(w, &cook)
- redirect := string(ctx.FormValue("redirect"))
+ redirect := r.FormValue("redirect")
if redirect == "" {
- ctx.Redirect("/", 307)
+ http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
} else {
- ctx.Redirect(redirect, 307)
+ http.Redirect(w, r, redirect, http.StatusTemporaryRedirect)
}
return nil
}
-func Index(ctx *fasthttp.RequestCtx) {
- ctx.Redirect("/login", 307)
+func Index(w http.ResponseWriter, r *http.Request) {
+ http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
}
-func (v *AuthView) InitialRegisterView(ctx *fasthttp.RequestCtx) error {
- templates.WritePageTemplate(ctx, &templates.RegisterPage{})
+func (v *AuthView) InitialRegisterView(w http.ResponseWriter, r *http.Request) error {
+ templates.WritePageTemplate(w, &templates.RegisterPage{})
return nil
}
-func (v *AuthView) InitialRegister(ctx *fasthttp.RequestCtx) error {
- username := ctx.FormValue("username")
- password := ctx.FormValue("password")
- path := ctx.FormValue("path")
+func (v *AuthView) InitialRegister(w http.ResponseWriter, r *http.Request) error {
+ var (
+ username = []byte(r.FormValue("username"))
+ password = []byte(r.FormValue("password"))
+ path = []byte(r.FormValue("path"))
+ )
- err := v.userController.InitialRegister(ctx, username, password, path)
+ err := v.userController.InitialRegister(r.Context(), username, password, path)
if err != nil {
return err
}
- ctx.Redirect("/login", 307)
+ http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
return nil
}