FIX: warnings
This commit is contained in:
parent
67807992bc
commit
c32ecbff8f
57 changed files with 1898 additions and 21 deletions
48
out/production/iroon/internal/auth/auth.go
Normal file
48
out/production/iroon/internal/auth/auth.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/Bronku/iroon/internal/models"
|
||||
"github.com/Bronku/iroon/internal/store"
|
||||
)
|
||||
|
||||
type Authenticator struct {
|
||||
sessions map[string]models.Token
|
||||
s *store.Store
|
||||
}
|
||||
|
||||
func New(s *store.Store) *Authenticator {
|
||||
var out Authenticator
|
||||
var err error
|
||||
out.s = s
|
||||
out.sessions, err = s.GetSessions()
|
||||
//fmt.Println(out.sessions)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return &out
|
||||
}
|
||||
|
||||
func (a *Authenticator) ensureAuth(in http.Handler) http.Handler {
|
||||
fmt.Println("ensureAuth called")
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := a.getSession(r)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
in.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (a *Authenticator) Middleware(in http.Handler) http.Handler {
|
||||
handler := http.NewServeMux()
|
||||
handler.HandleFunc("GET /login", getLogin)
|
||||
handler.HandleFunc("POST /login", a.login)
|
||||
handler.Handle("/", a.ensureAuth(in))
|
||||
return handler
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue