Logout
This commit is contained in:
parent
e2e1b3f83a
commit
1bb7e7e157
4 changed files with 61 additions and 40 deletions
|
@ -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
|
||||
}
|
50
pkg/user/userclient.go
Normal file
50
pkg/user/userclient.go
Normal file
|
@ -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
|
||||
}
|
|
@ -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
|
|
@ -14,6 +14,7 @@
|
|||
<li><a href="/register">Register</a></li>
|
||||
<li><a href="/login">Login</a></li>
|
||||
<li><a href="/me">Me</a></li>
|
||||
<li><a href="/logout">Logout</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="col-md-9"> {{ yield }}</div>
|
||||
|
|
Loading…
Reference in a new issue