Made registration work

This commit is contained in:
konrad 2018-06-10 11:34:59 +02:00 committed by kolaente
parent 2ca4c521aa
commit 1bee67cac7
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
6 changed files with 14 additions and 20 deletions

View file

@ -37,7 +37,7 @@ func main() {
} }
// Version notification // Version notification
fmt.Println("Library version", Version) fmt.Println("List version", Version)
// Start the webserver // Start the webserver
e := routes.NewEcho() e := routes.NewEcho()
@ -45,7 +45,7 @@ func main() {
// Start server // Start server
go func() { go func() {
if err := e.Start(models.Config.Interface); err != nil { 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 // User holds information about an user
type User struct { type User struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"` 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"` Username string `xorm:"varchar(250) not null unique" json:"username"`
Password string `xorm:"varchar(250) not null" json:"password"` Password string `xorm:"varchar(250) not null" json:"password"`
Email string `xorm:"varchar(250)" json:"email"` Email string `xorm:"varchar(250)" json:"email"`
IsAdmin bool `xorm:"tinyint(1) not null" json:"isAdmin"`
Created int64 `xorm:"created" json:"created"` Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"` Updated int64 `xorm:"updated" json:"updated"`
} }
@ -84,7 +82,6 @@ func GetCurrentUser(c echo.Context) (user User, err error) {
} }
user = User{ user = User{
ID: int64(userID), ID: int64(userID),
Name: claims["name"].(string),
Email: claims["email"].(string), Email: claims["email"].(string),
Username: claims["username"].(string), Username: claims["username"].(string),
} }

View file

@ -5,7 +5,7 @@ import (
) )
// CreateUser creates a new user and inserts it into the database // 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 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 // Check if the user already existst with that username
existingUser, exists, err := GetUser(User{Username: newUser.Username}) existingUser, exists, err := GetUser(User{Username: newUser.Username})
if err != nil { if err != nil && !IsErrUserDoesNotExist(err){
return User{}, err return User{}, err
} }
if exists { 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 // Check if the user already existst with that email
existingUser, exists, err = GetUser(User{Email: newUser.Email}) existingUser, exists, err = GetUser(User{Email: newUser.Email})
if err != nil { if err != nil && !IsErrUserDoesNotExist(err) {
return User{}, err return User{}, err
} }
if exists { if exists {
@ -60,7 +60,7 @@ func hashPassword(password string) (string, error) {
} }
// UpdateUser updates a user // 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 // Check if it exists
theUser, exists, err := GetUserByID(user.ID) theUser, exists, err := GetUserByID(user.ID)

View file

@ -1,4 +1,4 @@
package routes package v1
import ( import (
"crypto/md5" "crypto/md5"
@ -29,11 +29,9 @@ func Login(c echo.Context) error {
// Set claims // Set claims
claims := token.Claims.(jwt.MapClaims) claims := token.Claims.(jwt.MapClaims)
claims["name"] = user.Name
claims["username"] = user.Username claims["username"] = user.Username
claims["email"] = user.Email claims["email"] = user.Email
claims["id"] = user.ID claims["id"] = user.ID
claims["admin"] = user.IsAdmin
claims["exp"] = time.Now().Add(time.Hour * 72).Unix() claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
avatar := md5.Sum([]byte(user.Email)) avatar := md5.Sum([]byte(user.Email))

View file

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"fmt"
) )
// UserAddOrUpdate is the handler to add a user // 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."}) return c.JSON(http.StatusInternalServerError, models.Message{"Could not check if the user exists."})
} }
// Get the doer options fmt.Println(exists)
doer, err := models.GetCurrentUser(c)
if err != nil {
return err
}
// Insert or update the user // Insert or update the user
var newUser models.User var newUser models.User
if exists { if exists {
newUser, err = models.UpdateUser(*datUser, &doer) newUser, err = models.UpdateUser(*datUser)
} else { } else {
newUser, err = models.CreateUser(*datUser, &doer) newUser, err = models.CreateUser(*datUser)
} }
if err != nil { if err != nil {

View file

@ -39,10 +39,12 @@ func RegisterRoutes(e *echo.Echo) {
// CORS_SHIT // CORS_SHIT
a.OPTIONS("/login", SetCORSHeader) a.OPTIONS("/login", SetCORSHeader)
a.OPTIONS("/register", SetCORSHeader)
a.OPTIONS("/users", SetCORSHeader) a.OPTIONS("/users", SetCORSHeader)
a.OPTIONS("/users/:id", SetCORSHeader) a.OPTIONS("/users/:id", SetCORSHeader)
a.POST("/login", Login) a.POST("/login", apiv1.Login)
a.POST("/register", apiv1.UserAddOrUpdate)
// ===== Routes with Authetification ===== // ===== Routes with Authetification =====
// Authetification // Authetification