siga

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit b66b49f806f7c1601d2be3f587c9e79d4e29723e
parent ce0e258f6c4a4d7bd4a8c35cf6f62ed61f7d4e58
Author: Gabriel G. de Brito <me@gboncoffee.dev.br>
Date:   Sun,  8 Nov 2026 20:56:55 -0300

Logout works.

Diffstat:
Mauth/authenticated.go | 5+----
Mauth/login.go | 5-----
Aauth/logout.go | 19+++++++++++++++++++
Mauth/repository/repository.go | 10+++++++---
Mauth/router.go | 2+-
5 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/auth/authenticated.go b/auth/authenticated.go @@ -1,7 +1,6 @@ package main import ( - "log" "net/http" "uuid" @@ -11,15 +10,13 @@ import ( func authenticated(w http.ResponseWriter, r *http.Request) { token, err := uuid.Parse(r.PathValue("uuid")) if err != nil { - http.Error(w, "invalid token", http.StatusUnauthorized) + http.Error(w, "invalid token", http.StatusBadRequest) return } if err := repository.SessionExists(token); err != nil { - log.Println(err) http.Error(w, "invalid token", http.StatusUnauthorized) return } w.WriteHeader(http.StatusOK) - return } diff --git a/auth/login.go b/auth/login.go @@ -3,7 +3,6 @@ package main import ( "crypto/sha512" "encoding/json" - "log" "net/http" "uuid" @@ -28,15 +27,11 @@ func login(w http.ResponseWriter, r *http.Request) { c, err := repository.GetByUsername(req.Username) if err != nil { - log.Println("here") - log.Println(err) http.Error(w, "invalid username or password", http.StatusUnauthorized) return } if c.Password != sha512.Sum512([]byte(req.Password)) { - log.Println("there") - log.Println(err) http.Error(w, "invalid username or password", http.StatusUnauthorized) return } diff --git a/auth/logout.go b/auth/logout.go @@ -0,0 +1,19 @@ +package main + +import ( + "net/http" + "uuid" + + "github.com/gboncoffee/siga/auth/repository" +) + +func logout(w http.ResponseWriter, r *http.Request) { + token, err := uuid.Parse(r.PathValue("uuid")) + if err != nil { + http.Error(w, "invalid token", http.StatusBadRequest) + return + } + + repository.DeleteSession(token) + w.WriteHeader(http.StatusOK) +} diff --git a/auth/repository/repository.go b/auth/repository/repository.go @@ -3,7 +3,6 @@ package repository import ( "crypto/sha512" "database/sql" - "errors" "log" "uuid" @@ -15,8 +14,6 @@ type Credential struct { Password [sha512.Size]byte } -var ErrNotFound = errors.New("not found") - var db *sql.DB func init() { @@ -76,3 +73,10 @@ func SessionExists(token uuid.UUID) error { ).Scan(&t) return err } + +func DeleteSession(token uuid.UUID) error { + _, err := db.Exec( + `DELETE FROM sessions WHERE token = ?`, token[:], + ) + return err +} diff --git a/auth/router.go b/auth/router.go @@ -4,7 +4,7 @@ import "net/http" func router() { http.HandleFunc("POST /login", login) - // http.HandleFunc("POST /logout", logout) + http.HandleFunc("POST /logout/{uuid}", logout) http.HandleFunc("POST /credentials", credentials) http.HandleFunc("GET /{uuid}", authenticated) }