separate the auth routes from the user routes
This commit is contained in:
parent
071ba0a986
commit
dbad5ed64b
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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -14,58 +13,6 @@ import (
|
|||||||
"github.com/julienschmidt/httprouter"
|
"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) {
|
func ListUser(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
|
||||||
gdb := database.Connect()
|
gdb := database.Connect()
|
||||||
defer database.Close(gdb)
|
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 {
|
func Initialize() *httprouter.Router {
|
||||||
router := httprouter.New()
|
router := httprouter.New()
|
||||||
router.GET("/", index)
|
router.GET("/", index)
|
||||||
|
AuthRoutes(router)
|
||||||
FrameRoutes(router)
|
FrameRoutes(router)
|
||||||
GameRoutes(router)
|
GameRoutes(router)
|
||||||
GodotVersionRoutes(router)
|
GodotVersionRoutes(router)
|
||||||
|
@ -8,9 +8,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func UserRoutes(router *httprouter.Router) {
|
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", middlewares.Authenticate(controllers.ListUser))
|
||||||
router.GET("/user/:id", middlewares.Authenticate(controllers.GetUser))
|
router.GET("/user/:id", middlewares.Authenticate(controllers.GetUser))
|
||||||
router.POST("/user", middlewares.Authenticate(controllers.CreateUser))
|
router.POST("/user", middlewares.Authenticate(controllers.CreateUser))
|
||||||
|
Loading…
Reference in New Issue
Block a user