-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
50 lines (40 loc) · 1.14 KB
/
service.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
42
43
44
45
46
47
48
49
50
package auth
import (
"context"
"github.com/lazzytchik/council/internal/data"
"github.com/lazzytchik/council/internal/model"
"github.com/lazzytchik/council/internal/session"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log"
)
type Server struct {
UserStorageServer
identifier data.Identifier
registrar data.Registrar
session session.Session
errLogger *log.Logger
}
func (s *Server) Auth(ctx context.Context, cred *Credentials) (*Session, error) {
user, err := s.identifier.Identify(cred.Email, cred.Password)
if err != nil {
return nil, status.Errorf(codes.Internal, "Error while identifying user", err)
}
token, err := s.session.Save(session.Token{
User: user,
})
return &Session{Token: token}, status.New(codes.OK, "").Err()
}
func (s *Server) Register(ctx context.Context, user *User) (*UserId, error) {
u := &model.User{
Email: user.Email,
Username: user.Username,
Password: user.Password,
}
u.Hashed()
id, err := s.registrar.Register(*u)
if err != nil {
return nil, status.Errorf(codes.Internal, "Cannot register user", err)
}
return &UserId{Id: uint32(id)}, status.New(codes.OK, "").Err()
}