v1.2.0: back: use gorm in login
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
a2288ec90a
commit
8f3d9da55e
2 changed files with 7 additions and 13 deletions
|
|
@ -21,7 +21,7 @@ func login(c echo.Context) error {
|
|||
return c.JSON(http.StatusBadRequest, err)
|
||||
}
|
||||
|
||||
if response, err := logins.CreateLogin(db, request); err != nil {
|
||||
if response, err := logins.CreateLogin(dbGor, request); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, "Not a valide user username or password")
|
||||
} else {
|
||||
return c.JSON(http.StatusOK, response)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package logins
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
|
|
@ -11,6 +10,7 @@ import (
|
|||
"github.com/biscuit-auth/biscuit-go/v2/parser"
|
||||
"gitlab.g3e.fr/h6n/users/lib"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func CheckPasswordHash(password, hash string) bool {
|
||||
|
|
@ -18,20 +18,14 @@ func CheckPasswordHash(password, hash string) bool {
|
|||
return err == nil
|
||||
}
|
||||
|
||||
func CreateLogin(db *sql.DB, request RequestLogin) (ResponseLogin, error) {
|
||||
type (
|
||||
User struct {
|
||||
Username string
|
||||
EncryptedPassword string
|
||||
}
|
||||
)
|
||||
var user User
|
||||
func CreateLogin(db *gorm.DB, request RequestLogin) (ResponseLogin, error) {
|
||||
var response ResponseLogin
|
||||
|
||||
if err := db.QueryRow("SELECT username, password FROM users WHERE email = $1", request.Username).Scan(&user.Username, &user.EncryptedPassword); err != nil {
|
||||
return response, err
|
||||
var user = Users{Email: request.Username}
|
||||
if err := db.First(&user); err.Error != nil {
|
||||
return response, err.Error
|
||||
}
|
||||
if !CheckPasswordHash(request.Password, user.EncryptedPassword) {
|
||||
if !CheckPasswordHash(request.Password, user.Password) {
|
||||
return response, errors.ErrUnsupported
|
||||
}
|
||||
now := time.Now()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue