separate the auth routes from the user routes
This commit is contained in:
parent
3960bba3eb
commit
31ac6b106d
64
backend/controllers/auth.go
Normal file
64
backend/controllers/auth.go
Normal 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)
|
||||
}
|
@ -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
13
backend/routes/auth.go
Normal 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))
|
||||
}
|
@ -17,6 +17,7 @@ import (
|
||||
func Initialize() *httprouter.Router {
|
||||
router := httprouter.New()
|
||||
router.GET("/", index)
|
||||
AuthRoutes(router)
|
||||
FrameRoutes(router)
|
||||
GameRoutes(router)
|
||||
GodotVersionRoutes(router)
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user