flamingo.me/flamingo-commerce/v3@v3.11.0/customer/application/service.go (about) 1 package application 2 3 import ( 4 "context" 5 "errors" 6 7 "flamingo.me/flamingo/v3/core/auth" 8 "flamingo.me/flamingo/v3/framework/web" 9 10 "flamingo.me/flamingo-commerce/v3/customer/domain" 11 ) 12 13 var ( 14 // ErrNoIdentity user is considered to be not logged in 15 ErrNoIdentity = errors.New("no identity") 16 ) 17 18 // Service for customer management 19 type Service struct { 20 customerIdentityService domain.CustomerIdentityService 21 22 webIdentityService *auth.WebIdentityService 23 } 24 25 // Inject dependencies 26 func (s *Service) Inject( 27 webIdentityService *auth.WebIdentityService, 28 customerIdentityService domain.CustomerIdentityService, 29 ) *Service { 30 s.webIdentityService = webIdentityService 31 s.customerIdentityService = customerIdentityService 32 33 return s 34 } 35 36 // GetForIdentity returns the authenticated user if logged in 37 func (s *Service) GetForIdentity(ctx context.Context, request *web.Request) (domain.Customer, error) { 38 identity := s.webIdentityService.Identify(ctx, request) 39 if identity == nil { 40 return nil, ErrNoIdentity 41 } 42 43 return s.customerIdentityService.GetByIdentity(ctx, identity) 44 } 45 46 // GetUserID returns the current user ID if logged in 47 // 48 // Can be used to check if user is logged in. Returns ErrNoIdentity if user is considered to be not logged in. 49 func (s *Service) GetUserID(ctx context.Context, request *web.Request) (string, error) { 50 identity := s.webIdentityService.Identify(ctx, request) 51 if identity != nil { 52 return identity.Subject(), nil 53 } 54 55 return "", ErrNoIdentity 56 }