-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticator.go
41 lines (34 loc) · 923 Bytes
/
authenticator.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
37
38
39
40
41
package user
import (
"fmt"
"github.com/boliev/wordy/internal/domain"
"github.com/boliev/wordy/internal/repository"
)
// Authenticator struct
type Authenticator struct {
userRepository repository.User
jwtCreator JwtCreator
}
// CreateUserAuthenticator Authenticator constructor
func CreateUserAuthenticator(userRepository repository.User, jwtCreator JwtCreator) *Authenticator {
return &Authenticator{
userRepository: userRepository,
jwtCreator: jwtCreator,
}
}
// Auth authenticates user
func (a Authenticator) Auth(email string, password string) (*domain.UserAuth, error) {
usr, err := a.userRepository.FindByEmail(email)
if err != nil {
return nil, fmt.Errorf("no such user")
}
err = checkPassword(usr.Password, password)
if err != nil {
return nil, fmt.Errorf("no such user")
}
auth, err := a.jwtCreator.Create(int(usr.ID))
if err != nil {
return nil, err
}
return auth, nil
}