diff --git a/main.go b/main.go index 0c243632..e7031814 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ func main() { } // Version notification - fmt.Println("Library version", Version) + fmt.Println("List version", Version) // Start the webserver e := routes.NewEcho() @@ -45,7 +45,7 @@ func main() { // Start server go func() { if err := e.Start(models.Config.Interface); err != nil { - e.Logger.Info("shutting down the server") + e.Logger.Info("shutting down...") } }() diff --git a/models/user.go b/models/user.go index 7b80c9e4..3775f636 100644 --- a/models/user.go +++ b/models/user.go @@ -15,11 +15,9 @@ type UserLogin struct { // User holds information about an user type User struct { ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"` - Name string `xorm:"varchar(250)" json:"name"` Username string `xorm:"varchar(250) not null unique" json:"username"` Password string `xorm:"varchar(250) not null" json:"password"` Email string `xorm:"varchar(250)" json:"email"` - IsAdmin bool `xorm:"tinyint(1) not null" json:"isAdmin"` Created int64 `xorm:"created" json:"created"` Updated int64 `xorm:"updated" json:"updated"` } @@ -84,7 +82,6 @@ func GetCurrentUser(c echo.Context) (user User, err error) { } user = User{ ID: int64(userID), - Name: claims["name"].(string), Email: claims["email"].(string), Username: claims["username"].(string), } diff --git a/models/user_add_update.go b/models/user_add_update.go index 7319bd11..b4e9582b 100644 --- a/models/user_add_update.go +++ b/models/user_add_update.go @@ -5,7 +5,7 @@ import ( ) // CreateUser creates a new user and inserts it into the database -func CreateUser(user User, doer *User) (newUser User, err error) { +func CreateUser(user User) (newUser User, err error) { newUser = user @@ -16,7 +16,7 @@ func CreateUser(user User, doer *User) (newUser User, err error) { // Check if the user already existst with that username existingUser, exists, err := GetUser(User{Username: newUser.Username}) - if err != nil { + if err != nil && !IsErrUserDoesNotExist(err){ return User{}, err } if exists { @@ -25,7 +25,7 @@ func CreateUser(user User, doer *User) (newUser User, err error) { // Check if the user already existst with that email existingUser, exists, err = GetUser(User{Email: newUser.Email}) - if err != nil { + if err != nil && !IsErrUserDoesNotExist(err) { return User{}, err } if exists { @@ -60,7 +60,7 @@ func hashPassword(password string) (string, error) { } // UpdateUser updates a user -func UpdateUser(user User, doer *User) (updatedUser User, err error) { +func UpdateUser(user User) (updatedUser User, err error) { // Check if it exists theUser, exists, err := GetUserByID(user.ID) diff --git a/routes/login.go b/routes/api/v1/login.go similarity index 93% rename from routes/login.go rename to routes/api/v1/login.go index ee3c7a00..3237a764 100644 --- a/routes/login.go +++ b/routes/api/v1/login.go @@ -1,4 +1,4 @@ -package routes +package v1 import ( "crypto/md5" @@ -29,11 +29,9 @@ func Login(c echo.Context) error { // Set claims claims := token.Claims.(jwt.MapClaims) - claims["name"] = user.Name claims["username"] = user.Username claims["email"] = user.Email claims["id"] = user.ID - claims["admin"] = user.IsAdmin claims["exp"] = time.Now().Add(time.Hour * 72).Unix() avatar := md5.Sum([]byte(user.Email)) diff --git a/routes/api/v1/user_add_update.go b/routes/api/v1/user_add_update.go index 99970f53..f88dc64c 100644 --- a/routes/api/v1/user_add_update.go +++ b/routes/api/v1/user_add_update.go @@ -7,6 +7,7 @@ import ( "net/http" "strconv" "strings" + "fmt" ) // UserAddOrUpdate is the handler to add a user @@ -51,18 +52,14 @@ func UserAddOrUpdate(c echo.Context) error { return c.JSON(http.StatusInternalServerError, models.Message{"Could not check if the user exists."}) } - // Get the doer options - doer, err := models.GetCurrentUser(c) - if err != nil { - return err - } + fmt.Println(exists) // Insert or update the user var newUser models.User if exists { - newUser, err = models.UpdateUser(*datUser, &doer) + newUser, err = models.UpdateUser(*datUser) } else { - newUser, err = models.CreateUser(*datUser, &doer) + newUser, err = models.CreateUser(*datUser) } if err != nil { diff --git a/routes/routes.go b/routes/routes.go index 15e80e25..aa1df201 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -39,10 +39,12 @@ func RegisterRoutes(e *echo.Echo) { // CORS_SHIT a.OPTIONS("/login", SetCORSHeader) + a.OPTIONS("/register", SetCORSHeader) a.OPTIONS("/users", SetCORSHeader) a.OPTIONS("/users/:id", SetCORSHeader) - a.POST("/login", Login) + a.POST("/login", apiv1.Login) + a.POST("/register", apiv1.UserAddOrUpdate) // ===== Routes with Authetification ===== // Authetification