143 lines
3.1 KiB
Go
143 lines
3.1 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.javil.eu/jacob1123/budgeteer"
|
|
"git.javil.eu/jacob1123/budgeteer/postgres"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (h *Handler) verifyLogin(c *gin.Context) (budgeteer.Token, error) {
|
|
tokenString, err := c.Cookie(authCookie)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
token, err := h.TokenVerifier.VerifyToken(tokenString)
|
|
if err != nil {
|
|
c.SetCookie(authCookie, "", -1, "", "", false, false)
|
|
return nil, err
|
|
}
|
|
|
|
return token, nil
|
|
}
|
|
|
|
func (h *Handler) getImportantData(c *gin.Context) {
|
|
budgetID := c.Param("budgetid")
|
|
budgetUUID, err := uuid.Parse(budgetID)
|
|
if err != nil {
|
|
c.Redirect(http.StatusTemporaryRedirect, "/login")
|
|
return
|
|
}
|
|
|
|
accounts, err := h.Service.DB.GetAccountsWithBalance(c.Request.Context(), budgetUUID)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
base := AlwaysNeededData{
|
|
Accounts: accounts,
|
|
}
|
|
|
|
c.Set("data", base)
|
|
c.Next()
|
|
}
|
|
|
|
func (h *Handler) verifyLoginWithRedirect(c *gin.Context) {
|
|
token, err := h.verifyLogin(c)
|
|
if err != nil {
|
|
c.Redirect(http.StatusTemporaryRedirect, "/login")
|
|
return
|
|
}
|
|
|
|
c.Set("token", token)
|
|
c.Next()
|
|
}
|
|
|
|
func (h *Handler) login(c *gin.Context) {
|
|
if _, err := h.verifyLogin(c); err == nil {
|
|
c.Redirect(http.StatusTemporaryRedirect, "/dashboard")
|
|
return
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "login.html", nil)
|
|
}
|
|
|
|
func (h *Handler) register(c *gin.Context) {
|
|
if _, err := h.verifyLogin(c); err == nil {
|
|
c.Redirect(http.StatusTemporaryRedirect, "/dashboard")
|
|
return
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "register.html", nil)
|
|
}
|
|
|
|
func logout(c *gin.Context) {
|
|
clearLogin(c)
|
|
}
|
|
|
|
func clearLogin(c *gin.Context) {
|
|
c.SetCookie(authCookie, "", -1, "", "", false, true)
|
|
}
|
|
|
|
func (h *Handler) loginPost(c *gin.Context) {
|
|
username, _ := c.GetPostForm("username")
|
|
password, _ := c.GetPostForm("password")
|
|
|
|
user, err := h.Service.DB.GetUserByUsername(context.Background(), username)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusUnauthorized, err)
|
|
return
|
|
}
|
|
|
|
if err = h.CredentialsVerifier.Verify(password, user.Password); err != nil {
|
|
c.AbortWithError(http.StatusUnauthorized, err)
|
|
return
|
|
}
|
|
|
|
t, err := h.TokenVerifier.CreateToken(&user)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusUnauthorized, err)
|
|
}
|
|
|
|
maxAge := (int)((expiration * time.Hour).Seconds())
|
|
c.SetCookie(authCookie, t, maxAge, "", "", false, true)
|
|
|
|
c.JSON(http.StatusOK, map[string]string{
|
|
"token": t,
|
|
})
|
|
}
|
|
|
|
func (h *Handler) registerPost(c *gin.Context) {
|
|
email, _ := c.GetPostForm("email")
|
|
password, _ := c.GetPostForm("password")
|
|
name, _ := c.GetPostForm("name")
|
|
|
|
_, err := h.Service.DB.GetUserByUsername(context.Background(), email)
|
|
if err == nil {
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
hash, err := h.CredentialsVerifier.Hash(password)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusUnauthorized, err)
|
|
return
|
|
}
|
|
|
|
createUser := postgres.CreateUserParams{
|
|
Name: name,
|
|
Password: hash,
|
|
Email: email,
|
|
}
|
|
_, err = h.Service.DB.CreateUser(context.Background(), createUser)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
}
|
|
}
|