forked from ybkuroki/go-webapp-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
36 lines (30 loc) · 1.03 KB
/
account.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 service
import (
"github.com/maju6406/go-webapp-sample/model"
"github.com/maju6406/go-webapp-sample/mycontext"
"golang.org/x/crypto/bcrypt"
)
// AccountService is a service for managing user account.
type AccountService struct {
context mycontext.Context
}
// NewAccountService is constructor.
func NewAccountService(context mycontext.Context) *AccountService {
return &AccountService{context: context}
}
// AuthenticateByUsernameAndPassword authenticates by using username and plain text password.
func (a *AccountService) AuthenticateByUsernameAndPassword(username string, password string) (bool, *model.Account) {
rep := a.context.GetRepository()
logger := a.context.GetLogger()
account := model.Account{}
result, err := account.FindByName(rep, username)
if err != nil {
logger.GetZapLogger().Errorf(err.Error())
return false, nil
}
if err := bcrypt.CompareHashAndPassword([]byte(result.Password), []byte(password)); err != nil {
logger.GetZapLogger().Errorf(err.Error())
return false, nil
}
return true, result
}