mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
Implements user registration endpoint
This commit is contained in:
parent
6289b859bd
commit
8041ab2bc4
2 changed files with 54 additions and 6 deletions
|
@ -2,6 +2,7 @@ package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
//"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
|
//"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
|
||||||
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
@ -12,9 +13,9 @@ func VisitorAuthenticate(r *gin.RouterGroup) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func UsersRegister(r *gin.RouterGroup) {
|
func UsersRegister(r *gin.RouterGroup) {
|
||||||
r.POST("/users", userRegistrationEp)
|
r.POST("", userRegistrationEp)
|
||||||
r.PUT("/:UserID", userUpdateEp)
|
r.PUT("/:UserID", userUpdateEp)
|
||||||
r.GET("/", usersReadEp)
|
r.GET("", usersReadEp)
|
||||||
r.GET("/:UserID", userReadEp)
|
r.GET("/:UserID", userReadEp)
|
||||||
//r.GET("/me", userSelfEp) // TODO: this conflicts with GET /:userID
|
//r.GET("/me", userSelfEp) // TODO: this conflicts with GET /:userID
|
||||||
r.DELETE("/:UserID", userDeleteEp)
|
r.DELETE("/:UserID", userDeleteEp)
|
||||||
|
@ -84,8 +85,48 @@ func usersReadEp(c *gin.Context) {
|
||||||
|
|
||||||
func userRegistrationEp(c *gin.Context) {
|
func userRegistrationEp(c *gin.Context) {
|
||||||
|
|
||||||
|
// Bind the response (context) with the User struct
|
||||||
|
var newUser User
|
||||||
|
if err := c.BindJSON(&newUser); err != nil {
|
||||||
|
// TODO: do something other than panic ...
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: validate the User for:
|
||||||
|
// - username
|
||||||
|
// - email
|
||||||
|
// - role
|
||||||
|
// and in case of error raise 422
|
||||||
|
|
||||||
|
// Check that the username is NOT taken
|
||||||
|
_, err := FindUserByUsername(newUser.Username)
|
||||||
|
if err == nil {
|
||||||
|
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||||
|
"message": "Username is already taken",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash the password before saving it to the DB
|
||||||
|
err = newUser.SetPassword(newUser.Password)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||||
|
"message": "Unable to encrypt the password",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the user in the DB
|
||||||
|
err = newUser.save()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
|
"message": "Unable to create new user",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": "NOT implemented",
|
"user": fmt.Sprintf(newUser.Username),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,12 @@ func FindUserByUsername(username string) (User, error) {
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *User) save() error {
|
||||||
|
db := common.GetDB()
|
||||||
|
err := db.Create(u).Error
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (u *User) SetPassword(password string) error {
|
func (u *User) SetPassword(password string) error {
|
||||||
if len(password) == 0 {
|
if len(password) == 0 {
|
||||||
return fmt.Errorf("Password cannot be empty")
|
return fmt.Errorf("Password cannot be empty")
|
||||||
|
@ -49,7 +55,8 @@ func (u *User) validatePassword(password string) error {
|
||||||
return bcrypt.CompareHashAndPassword(hashedPassword, loginPassword)
|
return bcrypt.CompareHashAndPassword(hashedPassword, loginPassword)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) update(data interface{}) error {
|
func (u *User) update(modifiedUser User) error {
|
||||||
// TODO: Not implemented
|
db := common.GetDB()
|
||||||
return nil
|
err := db.Model(u).Update(modifiedUser).Error
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue