Implemented method to accept user login as json
This commit is contained in:
parent
9a745916b2
commit
b8738e9b52
|
@ -3,6 +3,7 @@ package models
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
"git.mowie.cc/konrad/Library/routes"
|
||||||
)
|
)
|
||||||
|
|
||||||
// User holds information about an user
|
// User holds information about an user
|
||||||
|
@ -28,11 +29,10 @@ func HashPassword(password string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckUserCredentials checks user credentials
|
// CheckUserCredentials checks user credentials
|
||||||
func CheckUserCredentials(username, password string) (User, error) {
|
func CheckUserCredentials(u routes.UserLogin) (User, error) {
|
||||||
|
|
||||||
fmt.Println(username, password)
|
|
||||||
// Check if the user exists
|
// Check if the user exists
|
||||||
var user = User{Username: username}
|
var user = User{Username: u.Username}
|
||||||
exists, err := x.Get(&user)
|
exists, err := x.Get(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return User{}, err
|
return User{}, err
|
||||||
|
@ -43,7 +43,7 @@ func CheckUserCredentials(username, password string) (User, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the users password
|
// Check the users password
|
||||||
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
|
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(u.Password))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return User{}, err
|
return User{}, err
|
||||||
|
|
|
@ -8,13 +8,21 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// User Login-Object to recive user credentials in JSON format
|
||||||
|
type UserLogin struct {
|
||||||
|
Username string `json:"username" form:"username"`
|
||||||
|
Password string `json:"password" form:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
// Login is the login handler
|
// Login is the login handler
|
||||||
func Login(c echo.Context) error {
|
func Login(c echo.Context) error {
|
||||||
username := c.FormValue("username")
|
u := new(UserLogin)
|
||||||
password := c.FormValue("password")
|
if err := c.Bind(u); err != nil {
|
||||||
|
return c.JSON(http.StatusBadRequest, models.Message{"Please provide a username and password."})
|
||||||
|
}
|
||||||
|
|
||||||
// Check user
|
// Check user
|
||||||
user, err := models.CheckUserCredentials(username, password)
|
user, err := models.CheckUserCredentials(u)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusUnauthorized, models.Message{"Wrong username or password."})
|
return c.JSON(http.StatusUnauthorized, models.Message{"Wrong username or password."})
|
||||||
|
|
Loading…
Reference in New Issue