Added method to add first user on first run
This commit is contained in:
parent
f4ba392f93
commit
4715459d95
|
@ -6,3 +6,10 @@ User = root
|
|||
Password = jup2000
|
||||
Host = 127.0.0.1
|
||||
Database = library2
|
||||
|
||||
; First user to be created, on every startup the program checks if he exists, if not it creates it
|
||||
[User]
|
||||
Name = Nie Mand
|
||||
Username = konrad
|
||||
Password = 1234
|
||||
Email = k@knt.li
|
|
@ -3,10 +3,11 @@ package models
|
|||
import (
|
||||
"github.com/go-ini/ini"
|
||||
"os"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Config holds the config struct
|
||||
var Config struct {
|
||||
type ConfigStruct struct {
|
||||
Database struct {
|
||||
Host string
|
||||
User string
|
||||
|
@ -15,8 +16,12 @@ var Config struct {
|
|||
}
|
||||
|
||||
JWTLoginSecret []byte
|
||||
|
||||
FirstUser User `ini:"User"`
|
||||
}
|
||||
|
||||
var Config = new(ConfigStruct)
|
||||
|
||||
// SetConfig initianlises the config and publishes it for other functions to use
|
||||
func SetConfig() error {
|
||||
|
||||
|
@ -30,8 +35,25 @@ func SetConfig() error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Database
|
||||
cfg.Section("Database").MapTo(Config.Database)
|
||||
err = cfg.MapTo(Config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Check if the first user already exists, aka a user with the ID = 1. If not, insert it
|
||||
_, exists, err := GetUserByID(1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If it doesn't exist, create it
|
||||
if !exists {
|
||||
_, err = CreateUser(Config.FirstUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Created new user " + Config.FirstUser.Username)
|
||||
}
|
||||
|
||||
// JWT secret
|
||||
Config.JWTLoginSecret = []byte(cfg.Section("General").Key("JWTSecret").String())
|
||||
|
|
|
@ -15,9 +15,9 @@ type UserLogin struct {
|
|||
type User struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
||||
Name string `xorm:"varchar(250)"`
|
||||
Username string `xorm:"varchar(250) not null"`
|
||||
Username string `xorm:"varchar(250) not null unique"`
|
||||
Password string `xorm:"varchar(250) not null"`
|
||||
Email string `xorm:"varchar(250) not null"`
|
||||
Email string `xorm:"varchar(250)"`
|
||||
Created int64 `xorm:"created"`
|
||||
Updated int64 `xorm:"updated"`
|
||||
}
|
||||
|
@ -27,8 +27,54 @@ func (User) TableName() string {
|
|||
return "users"
|
||||
}
|
||||
|
||||
// GetUserByID gets informations about a user by its ID
|
||||
func GetUserByID(id int64) (user User, exists bool, err error) {
|
||||
exists, err = x.Id(id).Get(&user)
|
||||
return user, exists, err
|
||||
}
|
||||
|
||||
func GetUserByUsername(username string) (user User, exists bool, err error) {
|
||||
user.Username = username
|
||||
exists, err = x.Get(&user)
|
||||
return user, exists, err
|
||||
}
|
||||
|
||||
// CreateUser creates a new user and inserts it into the database
|
||||
func CreateUser(user User) (newUser User, err error) {
|
||||
|
||||
newUser = user
|
||||
|
||||
// Check if we have all needed informations
|
||||
if newUser.Password == "" || newUser.Username == "" {
|
||||
return User{}, fmt.Errorf("You need to specify at least a username and a password!")
|
||||
}
|
||||
|
||||
// Check if the user already exists
|
||||
_, exists, err := GetUserByUsername(newUser.Username)
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
if exists {
|
||||
return User{}, fmt.Errorf("This username is already taken. Please use another.")
|
||||
}
|
||||
|
||||
// Hash the password
|
||||
newUser.Password, err = hashPassword(user.Password)
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
// Insert it
|
||||
_, err = x.Insert(newUser)
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
return newUser, nil
|
||||
}
|
||||
|
||||
// HashPassword hashes a password
|
||||
func HashPassword(password string) (string, error) {
|
||||
func hashPassword(password string) (string, error) {
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
||||
return string(bytes), err
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
})
|
||||
|
||||
// Basic Route
|
||||
e.Static("/", "assets/index.html")
|
||||
e.Static("/", "frontend/dist/")
|
||||
|
||||
// Login Route
|
||||
e.POST("/login", Login)
|
||||
|
|
Loading…
Reference in New Issue