This commit is contained in:
Kekskurse 2021-11-04 03:04:05 +01:00
parent e2e1b3f83a
commit 1bb7e7e157
Signed by: kekskurse
GPG key ID: 728ACCB59341E7E4
4 changed files with 61 additions and 40 deletions

View file

@ -2,7 +2,6 @@ package user
import ( import (
"embed" "embed"
"errors"
"fmt" "fmt"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"github.com/go-chi/jwtauth/v5" "github.com/go-chi/jwtauth/v5"
@ -32,6 +31,7 @@ func Register(router chi.Router) {
router.Post("/login", login) router.Post("/login", login)
router.Get("/register", loginForm) router.Get("/register", loginForm)
router.Post("/register", register) router.Post("/register", register)
router.Get("/logout", logout)
tokenAuth = jwtauth.New("HS256", []byte("secret"), nil) 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) { func register(w http.ResponseWriter, r *http.Request) {
r.ParseForm() 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
View 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
}

View file

@ -12,6 +12,5 @@ next steps:
* auth über mehrer pkg sharen z.b. durch config object statt chi router in die Register funktion reichen * 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 user client bauen
* tests für http functionen bauen, rausfinden wie das geht * tests für http functionen bauen, rausfinden wie das geht
* logout
* mysql migrationen oder andere datenbank für user einbauen * mysql migrationen oder andere datenbank für user einbauen
* ci/Cd bis docker image bauen * ci/Cd bis docker image bauen

View file

@ -14,6 +14,7 @@
<li><a href="/register">Register</a></li> <li><a href="/register">Register</a></li>
<li><a href="/login">Login</a></li> <li><a href="/login">Login</a></li>
<li><a href="/me">Me</a></li> <li><a href="/me">Me</a></li>
<li><a href="/logout">Logout</a></li>
</ul> </ul>
</div> </div>
<div class="col-md-9"> {{ yield }}</div> <div class="col-md-9"> {{ yield }}</div>