Implemented user via db
Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
parent
406044b060
commit
c5592ad45b
|
@ -26,8 +26,9 @@ func SetEngine() (err error) {
|
||||||
|
|
||||||
x.SetMapper(core.GonicMapper{})
|
x.SetMapper(core.GonicMapper{})
|
||||||
|
|
||||||
// Sync
|
// Sync dat shit
|
||||||
x.Sync(&Book{})
|
x.Sync(&Book{})
|
||||||
|
x.Sync(&User{})
|
||||||
|
|
||||||
x.ShowSQL(true)
|
x.ShowSQL(true)
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
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"`
|
||||||
|
Password string `xorm:"varchar(250) not null"`
|
||||||
|
Email string `xorm:"varchar(250) not null"`
|
||||||
|
Created int64 `xorm:"created"`
|
||||||
|
Updated int64 `xorm:"updated"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (User) TableName() string {
|
||||||
|
return "users"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash a password
|
||||||
|
func HashPassword(password string) (string, error) {
|
||||||
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
|
||||||
|
return string(bytes), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check user credentials
|
||||||
|
func CheckUserCredentials (username, password string) (User, error) {
|
||||||
|
|
||||||
|
// Check if the user exists
|
||||||
|
var user = User{Username:username}
|
||||||
|
exists, err := x.Get(&user)
|
||||||
|
if err != nil {
|
||||||
|
return User{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
return User{}, fmt.Errorf("User does not exist!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the users password
|
||||||
|
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return User{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
|
@ -12,25 +12,31 @@ func Login(c echo.Context) error {
|
||||||
username := c.FormValue("username")
|
username := c.FormValue("username")
|
||||||
password := c.FormValue("password")
|
password := c.FormValue("password")
|
||||||
|
|
||||||
if username == "jon" && password == "shhh!" {
|
// Check user
|
||||||
// Create token
|
user, err := models.CheckUserCredentials(username, password)
|
||||||
token := jwt.New(jwt.SigningMethodHS256)
|
|
||||||
|
|
||||||
// Set claims
|
if err != nil {
|
||||||
claims := token.Claims.(jwt.MapClaims)
|
return c.JSON(http.StatusUnauthorized, models.Message{"Wrong username or password."})
|
||||||
claims["name"] = "Jon Snow"
|
|
||||||
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
|
|
||||||
|
|
||||||
// Generate encoded token and send it as response.
|
|
||||||
t, err := token.SignedString(models.Config.JWTLoginSecret)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(http.StatusOK, map[string]string{
|
|
||||||
"token": t,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return echo.ErrUnauthorized
|
// Create token
|
||||||
}
|
token := jwt.New(jwt.SigningMethodHS256)
|
||||||
|
|
||||||
|
// Set claims
|
||||||
|
claims := token.Claims.(jwt.MapClaims)
|
||||||
|
claims["name"] = user.Name
|
||||||
|
claims["username"] = user.Username
|
||||||
|
claims["email"] = user.Email
|
||||||
|
claims["id"] = user.ID
|
||||||
|
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
|
||||||
|
|
||||||
|
// Generate encoded token and send it as response.
|
||||||
|
t, err := token.SignedString(models.Config.JWTLoginSecret)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(http.StatusOK, map[string]string{
|
||||||
|
"token": t,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -148,6 +148,18 @@
|
||||||
"revision": "9419663f5a44be8b34ca85f08abc5fe1be11f8a3",
|
"revision": "9419663f5a44be8b34ca85f08abc5fe1be11f8a3",
|
||||||
"revisionTime": "2017-09-30T17:45:11Z"
|
"revisionTime": "2017-09-30T17:45:11Z"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"checksumSHA1": "UWjVYmoHlIfHzVIskELHiJQtMOI=",
|
||||||
|
"path": "golang.org/x/crypto/bcrypt",
|
||||||
|
"revision": "9419663f5a44be8b34ca85f08abc5fe1be11f8a3",
|
||||||
|
"revisionTime": "2017-09-30T17:45:11Z"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"checksumSHA1": "oVPHWesOmZ02vLq2fglGvf+AMgk=",
|
||||||
|
"path": "golang.org/x/crypto/blowfish",
|
||||||
|
"revision": "9419663f5a44be8b34ca85f08abc5fe1be11f8a3",
|
||||||
|
"revisionTime": "2017-09-30T17:45:11Z"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "tY+5thYxjKDUQyQXYcBqogmMS5U=",
|
"checksumSHA1": "tY+5thYxjKDUQyQXYcBqogmMS5U=",
|
||||||
"path": "golang.org/x/sys/unix",
|
"path": "golang.org/x/sys/unix",
|
||||||
|
|
Loading…
Reference in New Issue