really basic login

This commit is contained in:
Florian Hoss 2022-04-04 10:16:29 +02:00
parent 177a26a2e9
commit 539a36dffe
6 changed files with 116 additions and 50 deletions

View file

@ -2,66 +2,94 @@ package webpage
import (
"app/database"
"fmt"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func (wp *Webpage) redirectHome(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, "/")
}
func (wp *Webpage) userIsLoggedIn(c *gin.Context) bool {
if wp.isLoggedIn {
wp.redirectHome(c)
return true
}
return false
}
func (wp *Webpage) defineRoutes() {
wp.Router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "SuperSafe",
"title": "SuperSafe",
"loggedIn": wp.isLoggedIn,
})
})
wp.Router.GET("/login", func(c *gin.Context) {
if wp.userIsLoggedIn(c) {
return
}
c.HTML(http.StatusOK, "login.tmpl", gin.H{
"title": "Login",
"title": "Login",
"loggedIn": wp.isLoggedIn,
})
})
wp.Router.GET("/logout", func(c *gin.Context) {
wp.isLoggedIn = false
wp.redirectHome(c)
})
wp.Router.GET("/register", func(c *gin.Context) {
wp.userIsLoggedIn(c)
c.HTML(http.StatusOK, "register.tmpl", gin.H{
"title": "Register",
"title": "Register",
"loggedIn": wp.isLoggedIn,
})
})
wp.Router.GET("/health", func(c *gin.Context) {
currentTime := time.Now().UnixMilli()
c.JSON(http.StatusOK, gin.H{
"timestamp": currentTime,
})
})
wp.Router.NoRoute(func(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, "/")
})
wp.Router.POST("/login", func(c *gin.Context) {
username, existing := c.GetPostForm("username")
password, existing := c.GetPostForm("password")
if existing == false || username == "" || password == "" {
username, uExisting := c.GetPostForm("username")
password, pExisting := c.GetPostForm("password")
if uExisting == false || pExisting == false || username == "" || password == "" {
c.JSON(400, gin.H{"message": "bad post form"})
return
}
fmt.Println(username, password)
c.JSON(200, gin.H{"message": "login successfull"})
success, err := wp.Database.LoginUser(username, password)
if err != nil {
c.JSON(500, gin.H{"message": err.Error()})
return
}
if success == true {
wp.isLoggedIn = true
c.JSON(200, gin.H{"message": "login successfull"})
return
}
wp.isLoggedIn = false
c.JSON(401, gin.H{"message": "user or password not found"})
})
wp.Router.POST("/register", func(c *gin.Context) {
username, existing := c.GetPostForm("username")
password, existing := c.GetPostForm("password")
if existing == false || username == "" || password == "" {
username, uExisting := c.GetPostForm("username")
password, pExisting := c.GetPostForm("password")
if uExisting == false || pExisting == false || username == "" || password == "" {
c.JSON(400, gin.H{"message": "bad post form"})
return
}
user := database.User{Username: username, Password: password}
result := wp.Database.ORM.Create(&user)
if result.Error != nil {
c.JSON(400, gin.H{"message": result.Error.Error()})
err := wp.Database.CreateUser(username, password)
if err != nil {
c.JSON(500, gin.H{"message": err.Error()})
return
}
c.JSON(200, gin.H{"message": "user registered"})
})
wp.Router.NoRoute(func(c *gin.Context) {
wp.redirectHome(c)
})
}
func (wp *Webpage) initialize() {
@ -73,7 +101,6 @@ func (wp *Webpage) Run() {
wp.initialize()
wp.Router = gin.New()
wp.Router.Use(gin.Recovery())
wp.Router.Use(gin.Logger())
wp.Router.SetTrustedProxies(nil)
wp.Router.Use(static.Serve("/static", static.LocalFile("./static", false)))
wp.Router.LoadHTMLGlob("templates/*")