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