Made registration work

This commit is contained in:
konrad 2018-06-10 11:34:59 +02:00 committed by kolaente
parent 2ca4c521aa
commit 1bee67cac7
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
6 changed files with 14 additions and 20 deletions

View File

@ -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...")
}
}()

View File

@ -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),
}

View File

@ -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)

View File

@ -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))

View File

@ -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 {

View File

@ -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