go-sample-webpage/pkg/user/user.go

167 lines
3.5 KiB
Go
Raw Normal View History

2021-11-04 01:14:51 +00:00
package user
import (
2021-11-22 22:11:37 +00:00
"bytes"
2021-11-04 01:14:51 +00:00
"embed"
"fmt"
"github.com/go-chi/chi/v5"
"github.com/go-chi/jwtauth/v5"
"github.com/unrolled/render"
2021-11-22 22:11:37 +00:00
gomail "gopkg.in/mail.v2"
"io"
2021-11-04 01:14:51 +00:00
"net/http"
"time"
)
// content holds our static web server content.
//go:embed templates/*
var webserver embed.FS
var ren *render.Render
2021-11-22 22:11:37 +00:00
var uc UserClient
2021-11-04 01:14:51 +00:00
var tokenAuth *jwtauth.JWTAuth
2021-11-22 22:46:01 +00:00
var d *gomail.Dialer
2021-11-23 19:21:43 +00:00
type UserConfig struct {
RegisterMail *gomail.Message
URLValidationToken string
}
var userconfig UserConfig
func NewUserConfig() UserConfig {
uc := UserConfig{}
m := gomail.NewMessage()
m.SetHeader("From", "test@keks.cloud")
m.SetHeader("To", "to@example.com")
m.SetHeader("Subject", "Activate your Account")
uc.RegisterMail = m
uc.URLValidationToken = "http://localhost/validate/%s"
return uc
}
func Register(router chi.Router, token *jwtauth.JWTAuth, userClient UserClient, dialer *gomail.Dialer, config UserConfig) {
2021-11-22 22:11:37 +00:00
uc = userClient
2021-11-23 19:21:43 +00:00
userconfig = config
2021-11-04 01:14:51 +00:00
ren = render.New(render.Options{
//Layout: "layout",
FileSystem: &render.EmbedFileSystem{
FS: webserver,
},
})
router.Get("/login", loginForm)
router.Post("/login", login)
2021-11-09 18:23:19 +00:00
router.Get("/register", registerForm)
2021-11-04 01:14:51 +00:00
router.Post("/register", register)
2021-11-04 02:04:05 +00:00
router.Get("/logout", logout)
2021-11-04 01:14:51 +00:00
2021-11-08 01:25:36 +00:00
tokenAuth = token
2021-11-04 01:14:51 +00:00
2021-11-22 22:46:01 +00:00
d = dialer
2021-11-04 01:14:51 +00:00
router.Group(func(r chi.Router) {
r.Use(jwtauth.Verifier(tokenAuth))
r.Get("/me", func(w http.ResponseWriter, r *http.Request) {
_, claims, _ := jwtauth.FromContext(r.Context())
if val, ok := claims["username"]; ok {
w.Write([]byte(fmt.Sprintf("hi %v", val)))
} else {
w.Write([]byte("Du bist nicht eingeloggt"))
}
})
})
}
func loginForm(w http.ResponseWriter, r *http.Request) {
ren.HTML(w, http.StatusOK, "login", nil)
}
2021-11-09 18:23:19 +00:00
func registerForm(w http.ResponseWriter, r *http.Request) {
ren.HTML(w, http.StatusOK, "register", nil)
}
2021-11-04 01:14:51 +00:00
func login(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
2021-11-22 22:11:37 +00:00
res, _ := uc.login(r.FormValue("username"), r.FormValue("password"), false)
2021-11-04 01:14:51 +00:00
if res {
_, tokenstring, err := tokenAuth.Encode(map[string]interface{}{"username": r.FormValue("username")})
if err != nil {
panic(err)
}
expiration := time.Now().Add(365 * 24 * time.Hour)
cookie := http.Cookie{Name: "jwt",Value:tokenstring,Expires:expiration}
http.SetCookie(w, &cookie)
w.Write([]byte("Login ok"))
} else {
w.Write([]byte("Login failed"))
}
}
2021-11-04 02:04:05 +00:00
func logout(w http.ResponseWriter, r *http.Request) {
cookie := http.Cookie{
Name: "jwt",
Value: "",
}
http.SetCookie(w, &cookie)
w.Write([]byte("Du wurdest ausgeloggt"))
}
2021-11-04 01:14:51 +00:00
func register(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
2021-11-22 22:11:37 +00:00
res, err := uc.register(r.FormValue("username"), r.FormValue("password"), r.FormValue("email"))
if err != nil || res == false {
w.Write([]byte("Registration Failed"))
return
2021-11-04 01:14:51 +00:00
}
2021-11-22 22:11:37 +00:00
rw := &LayoutMiddlewareResponseWriter{
ResponseWriter: w,
buf: &bytes.Buffer{},
}
2021-11-23 19:21:43 +00:00
token, err := uc.getMailValidationToken(r.FormValue("email"), true)
2021-11-22 22:11:37 +00:00
if err != nil {
panic(err)
}
2021-11-23 19:21:43 +00:00
ren.HTML(rw, http.StatusOK, "token", map[string]string{"username": r.FormValue("username"), "url": fmt.Sprintf(userconfig.URLValidationToken, token)})
2021-11-22 22:46:01 +00:00
2021-11-23 19:21:43 +00:00
content, err := io.ReadAll(rw.buf)
if err != nil {
panic(err)
}
2021-11-22 22:11:37 +00:00
2021-11-23 19:21:43 +00:00
mail := *userconfig.RegisterMail
mail.SetBody("text/html", string(content))
if err := d.DialAndSend(&mail); err != nil {
2021-11-22 22:11:37 +00:00
fmt.Println(err)
panic(err)
}
}
type LayoutMiddlewareResponseWriter struct {
http.ResponseWriter
buf *bytes.Buffer
}
func (myrw *LayoutMiddlewareResponseWriter) Write(p []byte) (int, error) {
return myrw.buf.Write(p)
2021-11-04 01:14:51 +00:00
}