separate the auth routes from the user routes

This commit is contained in:
Chris Cromer 2023-02-01 16:23:25 -03:00 committed by Martin Araneda
parent 071ba0a986
commit dbad5ed64b
5 changed files with 78 additions and 56 deletions

View File

@ -0,0 +1,64 @@
package controllers
import (
"encoding/json"
"errors"
"net/http"
"git.cromer.cl/Proyecto-Titulo/alai-server/backend/database"
"git.cromer.cl/Proyecto-Titulo/alai-server/backend/models"
"git.cromer.cl/Proyecto-Titulo/alai-server/backend/utils"
"github.com/julienschmidt/httprouter"
)
func Login(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
gdb := database.Connect()
defer database.Close(gdb)
var receivedUser models.User
var user models.User
decoder := json.NewDecoder(request.Body)
err := decoder.Decode(&receivedUser)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
result := gdb.Model(models.User{}).Where(&models.User{Username: receivedUser.Username}).Find(&user)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return
} else if result.RowsAffected == 0 {
utils.JSONErrorOutput(writer, http.StatusBadRequest, errors.New("incorrect user or password").Error())
return
}
err = user.CheckPassword(receivedUser.Password)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, errors.New("incorrect user or password").Error())
return
}
type Token struct {
Token string `json:"token"`
}
tokenString, err := utils.GenerateJWT(user.Email, user.Username)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
token := Token{Token: tokenString}
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
json.NewEncoder(writer).Encode(token)
}
func AuthenticateUser(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusNoContent)
}

View File

@ -2,7 +2,6 @@ package controllers
import (
"encoding/json"
"errors"
"math"
"net/http"
"strconv"
@ -14,58 +13,6 @@ import (
"github.com/julienschmidt/httprouter"
)
func Login(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
gdb := database.Connect()
defer database.Close(gdb)
var receivedUser models.User
var user models.User
decoder := json.NewDecoder(request.Body)
err := decoder.Decode(&receivedUser)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
result := gdb.Model(models.User{}).Where(&models.User{Username: receivedUser.Username}).Find(&user)
if result.Error != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error())
return
} else if result.RowsAffected == 0 {
utils.JSONErrorOutput(writer, http.StatusBadRequest, errors.New("incorrect user or password").Error())
return
}
err = user.CheckPassword(receivedUser.Password)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, errors.New("incorrect user or password").Error())
return
}
type Token struct {
Token string `json:"token"`
}
tokenString, err := utils.GenerateJWT(user.Email, user.Username)
if err != nil {
utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error())
return
}
token := Token{Token: tokenString}
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
json.NewEncoder(writer).Encode(token)
}
func AuthenticateUser(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusNoContent)
}
func ListUser(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
gdb := database.Connect()
defer database.Close(gdb)

13
backend/routes/auth.go Normal file
View File

@ -0,0 +1,13 @@
package routes
import (
"git.cromer.cl/Proyecto-Titulo/alai-server/backend/controllers"
"git.cromer.cl/Proyecto-Titulo/alai-server/backend/middlewares"
"github.com/julienschmidt/httprouter"
)
func AuthRoutes(router *httprouter.Router) {
router.POST("/login", controllers.Login)
router.GET("/auth", middlewares.Authenticate(controllers.AuthenticateUser))
}

View File

@ -17,6 +17,7 @@ import (
func Initialize() *httprouter.Router {
router := httprouter.New()
router.GET("/", index)
AuthRoutes(router)
FrameRoutes(router)
GameRoutes(router)
GodotVersionRoutes(router)

View File

@ -8,9 +8,6 @@ import (
)
func UserRoutes(router *httprouter.Router) {
router.POST("/login", controllers.Login)
router.GET("/auth", middlewares.Authenticate(controllers.AuthenticateUser))
router.GET("/user", middlewares.Authenticate(controllers.ListUser))
router.GET("/user/:id", middlewares.Authenticate(controllers.GetUser))
router.POST("/user", middlewares.Authenticate(controllers.CreateUser))