ch11.4
This commit is contained in:
@@ -45,7 +45,27 @@ func (m *UserModel) Insert(name, email, password string) error {
|
||||
}
|
||||
|
||||
func (m *UserModel) Authenticate(email, password string) (int, error) {
|
||||
return 0, nil
|
||||
var id int
|
||||
var hashedPassword []byte
|
||||
|
||||
stmt := `SELECT id, hashed_password FROM users WHERE email = ?`
|
||||
|
||||
err := m.DB.QueryRow(stmt, email).Scan(&id, &hashedPassword)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return 0, ErrInvalidCredentials
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
err = bcrypt.CompareHashAndPassword(hashedPassword, []byte(password))
|
||||
if err != nil {
|
||||
if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
|
||||
return 0, ErrInvalidCredentials
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (m *UserModel) Exists(id int) (bool, error) {
|
||||
|
||||
Reference in New Issue
Block a user