From 1bb7e7e157b81e4ae26591152e654f5123c39a83 Mon Sep 17 00:00:00 2001 From: kekskurse Date: Thu, 4 Nov 2021 03:04:05 +0100 Subject: [PATCH] Logout --- pkg/user/user.go | 49 +++++++++-------------------------------- pkg/user/userclient.go | 50 ++++++++++++++++++++++++++++++++++++++++++ readme.md | 1 - templates/layout.tmpl | 1 + 4 files changed, 61 insertions(+), 40 deletions(-) create mode 100644 pkg/user/userclient.go diff --git a/pkg/user/user.go b/pkg/user/user.go index 1d54dd1..bb4afd6 100644 --- a/pkg/user/user.go +++ b/pkg/user/user.go @@ -2,7 +2,6 @@ package user import ( "embed" - "errors" "fmt" "github.com/go-chi/chi/v5" "github.com/go-chi/jwtauth/v5" @@ -32,6 +31,7 @@ func Register(router chi.Router) { router.Post("/login", login) router.Get("/register", loginForm) router.Post("/register", register) + router.Get("/logout", logout) tokenAuth = jwtauth.New("HS256", []byte("secret"), nil) @@ -76,6 +76,15 @@ func login(w http.ResponseWriter, r *http.Request) { } } +func logout(w http.ResponseWriter, r *http.Request) { + cookie := http.Cookie{ + Name: "jwt", + Value: "", + } + http.SetCookie(w, &cookie) + w.Write([]byte("Du wurdest ausgeloggt")) +} + func register(w http.ResponseWriter, r *http.Request) { r.ParseForm() @@ -89,41 +98,3 @@ func register(w http.ResponseWriter, r *http.Request) { } } -type UserClient struct { - users map[string]string -} - -var u *UserClient - -func GetUserClient() *UserClient { - if u == nil { - uc := UserClient{} - uc.users = make(map[string]string) - uc.users["admin"]="password" - u = &uc - } - - return u -} - -func (uc *UserClient) register(username, password string) (bool, error) { - if _, ok := uc.users[username]; ok { - return false, errors.New("Username already used") - } - - uc.users[username] = password - return true, nil -} - -func (uc UserClient) login(username, password string) (bool, error) { - if val, ok := uc.users[username]; ok { - fmt.Println("Login for valide user") - if val == password { - return true, nil - } - } else { - fmt.Printf("User %v not found", username) - } - - return false, nil -} \ No newline at end of file diff --git a/pkg/user/userclient.go b/pkg/user/userclient.go new file mode 100644 index 0000000..13b7df8 --- /dev/null +++ b/pkg/user/userclient.go @@ -0,0 +1,50 @@ +package user + +import ( + "errors" + "fmt" +) + +type UserClient interface { + register(username, password string) (bool, error) + login(username, password string) (bool, error) +} + +type UserClientMemory struct { + users map[string]string +} + +var u *UserClientMemory + +func GetUserClient() *UserClientMemory { + if u == nil { + uc := UserClientMemory{} + uc.users = make(map[string]string) + uc.users["admin"]="password" + u = &uc + } + + return u +} + +func (uc *UserClientMemory) register(username, password string) (bool, error) { + if _, ok := uc.users[username]; ok { + return false, errors.New("Username already used") + } + + uc.users[username] = password + return true, nil +} + +func (uc UserClientMemory) login(username, password string) (bool, error) { + if val, ok := uc.users[username]; ok { + fmt.Println("Login for valide user") + if val == password { + return true, nil + } + } else { + fmt.Printf("User %v not found", username) + } + + return false, nil +} \ No newline at end of file diff --git a/readme.md b/readme.md index 9e45449..192a6f0 100644 --- a/readme.md +++ b/readme.md @@ -12,6 +12,5 @@ next steps: * auth über mehrer pkg sharen z.b. durch config object statt chi router in die Register funktion reichen * tests für user client bauen * tests für http functionen bauen, rausfinden wie das geht -* logout * mysql migrationen oder andere datenbank für user einbauen * ci/Cd bis docker image bauen \ No newline at end of file diff --git a/templates/layout.tmpl b/templates/layout.tmpl index 6de5a51..bb4af3f 100644 --- a/templates/layout.tmpl +++ b/templates/layout.tmpl @@ -14,6 +14,7 @@
  • Register
  • Login
  • Me
  • +
  • Logout
  • {{ yield }}