-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_controller.go
36 lines (29 loc) · 1019 Bytes
/
auth_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package controllers
import (
"log"
"github.com/mhogar/amber/common"
passwordhelpers "github.com/mhogar/amber/controllers/password_helpers"
"github.com/mhogar/amber/models"
)
type CoreAuthController struct {
PasswordHasher passwordhelpers.PasswordHasher
}
func (c CoreAuthController) AuthenticateUserWithPassword(CRUD AuthControllerCRUD, username string, password string) (*models.User, common.CustomError) {
//get the user
user, err := CRUD.GetUserByUsername(username)
if err != nil {
log.Println(common.ChainError("error getting user by username", err))
return nil, common.InternalError()
}
//check if user was found
if user == nil {
return nil, common.ClientError("invalid username and/or password")
}
//validate the password
err = c.PasswordHasher.ComparePasswords(user.PasswordHash, password)
if err != nil {
log.Println(common.ChainError("error comparing password hashes", err))
return nil, common.ClientError("invalid username and/or password")
}
return user, common.NoError()
}