mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
Improves authenticate endpoint:
- Deals with JSON bind error properly. Returs http code 422 and error message - Renames the handler function from authenticationEp to authenticate
This commit is contained in:
parent
73691f9595
commit
c1fcf7e0c0
1 changed files with 10 additions and 8 deletions
|
@ -28,9 +28,8 @@ type AuthResponse struct {
|
|||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// `/authenticate` endpoint does not require Authentication
|
||||
func VisitorAuthenticate(r *gin.RouterGroup) {
|
||||
r.POST("", authenticationEp)
|
||||
r.POST("", authenticate)
|
||||
}
|
||||
|
||||
func RegisterUserEndpoints(r *gin.RouterGroup) {
|
||||
|
@ -41,9 +40,9 @@ func RegisterUserEndpoints(r *gin.RouterGroup) {
|
|||
r.DELETE("/:UserID", deleteUser)
|
||||
}
|
||||
|
||||
// authenticationEp godoc
|
||||
// authenticate godoc
|
||||
// @Summary Authentication for user
|
||||
// @ID authenticationEp
|
||||
// @ID authenticate
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Tags users
|
||||
|
@ -53,13 +52,16 @@ func RegisterUserEndpoints(r *gin.RouterGroup) {
|
|||
// @Failure 404 "Not found"
|
||||
// @Failure 422 "Unprocessable entity."
|
||||
// @Router /authenticate [post]
|
||||
func authenticationEp(c *gin.Context) {
|
||||
func authenticate(c *gin.Context) {
|
||||
|
||||
// Bind the response (context) with the Credentials struct
|
||||
var loginRequest Credentials
|
||||
if err := c.BindJSON(&loginRequest); err != nil {
|
||||
// TODO: do something other than panic ...
|
||||
panic(err)
|
||||
if err := c.ShouldBindJSON(&loginRequest); err != nil {
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||
"success": false,
|
||||
"message": fmt.Sprintf("%v", err),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the Username or Password are empty
|
||||
|
|
Loading…
Add table
Reference in a new issue