128 lines
2.6 KiB
Go
128 lines
2.6 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.tek.govt.hu/dowerx/chat/server/config"
|
|
"git.tek.govt.hu/dowerx/chat/server/controller"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const SESSION_COOKIE string = "session"
|
|
|
|
func isLoggedIn(c *gin.Context) {
|
|
token, err := c.Cookie(SESSION_COOKIE)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": "missing token",
|
|
})
|
|
c.Abort()
|
|
return
|
|
}
|
|
|
|
c.Set(SESSION_COOKIE, token)
|
|
c.Next()
|
|
}
|
|
|
|
func register(c *gin.Context) {
|
|
type registerTransaction struct {
|
|
Username string `form:"username" json:"username"`
|
|
Password string `form:"password" json:"password"`
|
|
RepeatPassword string `form:"repeatPassword" json:"repeatPassword"`
|
|
}
|
|
|
|
transaction := registerTransaction{}
|
|
if err := c.Bind(&transaction); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
authController, err := controller.MakeAuthController()
|
|
if err != nil {
|
|
sendError(c, err)
|
|
return
|
|
}
|
|
|
|
err = authController.Register(transaction.Username, transaction.Password, transaction.RepeatPassword)
|
|
if err != nil {
|
|
sendError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "sucessful register",
|
|
})
|
|
}
|
|
|
|
func login(c *gin.Context) {
|
|
type loginTransaction struct {
|
|
Username string `form:"username" json:"username"`
|
|
Password string `form:"password" json:"password"`
|
|
}
|
|
transaction := loginTransaction{}
|
|
if err := c.Bind(&transaction); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
authController, err := controller.MakeAuthController()
|
|
if err != nil {
|
|
sendError(c, err)
|
|
return
|
|
}
|
|
|
|
token, err := authController.Login(transaction.Username, transaction.Password)
|
|
if err != nil {
|
|
sendError(c, err)
|
|
return
|
|
}
|
|
|
|
c.SetCookie(SESSION_COOKIE, token, config.GetConfig().API.TokenLife, "", "", false, false)
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "sucessful login",
|
|
"session": token,
|
|
})
|
|
}
|
|
|
|
func logout(c *gin.Context) {
|
|
authController, err := controller.MakeAuthController()
|
|
if err != nil {
|
|
sendError(c, err)
|
|
return
|
|
}
|
|
|
|
token, _ := c.Get(SESSION_COOKIE) // must exist after isLoggedIn
|
|
err = authController.Logout(token.(string))
|
|
if err != nil {
|
|
sendError(c, err)
|
|
return
|
|
}
|
|
|
|
c.SetCookie(SESSION_COOKIE, "", 0, "", "", false, false)
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "sucessful logout",
|
|
})
|
|
}
|
|
|
|
func bump(c *gin.Context) {
|
|
authController, err := controller.MakeAuthController()
|
|
if err != nil {
|
|
sendError(c, err)
|
|
return
|
|
}
|
|
|
|
token, _ := c.Get(SESSION_COOKIE)
|
|
if err = authController.Bump(token.(string)); err != nil {
|
|
sendError(c, err)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "bumped",
|
|
})
|
|
}
|