forked from ebhomengo/niki
1
0
Fork 0
niki/service/admin/admin/login.go

41 lines
1.5 KiB
Go

package adminservice
import (
"context"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) LoginWithPhoneNumber(ctx context.Context, req adminserviceparam.LoginWithPhoneNumberRequest) (adminserviceparam.LoginWithPhoneNumberResponse, error) {
const op = richerror.Op("adminservice.LoginWithPhoneNumber")
admin, err := s.repo.GetAdminByPhoneNumber(ctx, req.PhoneNumber)
if err != nil {
return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
if cErr := CompareHash(admin.GetPassword(), req.Password); cErr != nil {
return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(cErr).WithMessage(errmsg.ErrorMsgPhoneNumberOrPassIsIncorrect).WithKind(richerror.KindForbidden)
}
accessToken, aErr := s.auth.CreateAccessToken(admin)
if aErr != nil {
return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(aErr).WithKind(richerror.KindUnexpected)
}
refreshToken, rErr := s.auth.CreateRefreshToken(admin)
if rErr != nil {
return adminserviceparam.LoginWithPhoneNumberResponse{}, richerror.New(op).WithErr(rErr).WithKind(richerror.KindUnexpected)
}
return adminserviceparam.LoginWithPhoneNumberResponse{
Admin: admin,
Tokens: adminserviceparam.Tokens{
AccessToken: accessToken,
RefreshToken: refreshToken,
},
}, nil
}