//Login returns a JWT token if a user exists with given credentials
Login returns a JWT token if a user exists with given credentials
// Login returns a JWT token if a user exists with given credentials
func (service *UserService) Login(_ context.Context, lr *pb.LoginRequest) (*pb.AuthenticationResponse, error) {
	err := lr.Validate()
	if err != nil {
		return nil, err
	}
	var user UserDBModel
	result := service.DB.Take(&user, "email = ?", lr.Email)
	if errors.Is(result.Error, gorm.ErrRecordNotFound) {
		return nil, errors.New("user with email " + lr.Email + " not found")
	}
	if !server.IsCorrectPassword(user.Password, lr.Password) {
		return nil, errors.New("password is incorrect")
	}
	tokenString, err := service.authManager.GenerateToken(user.ID)
	if err != nil {
		return nil, err
	}
	return &pb.AuthenticationResponse{
		Token: tokenString,
		User:  user.AsResponse(),
	}, nil
}
func ( service * UserService ) Login ( _ context . Context , lr * pb . LoginRequest ) ( * pb . AuthenticationResponse , error ) { err := lr . Validate ( ) if err != nil { return nil , err } var user UserDBModel result := service . DB . Take ( & user , " " , lr . Email ) if errors . Is ( result . Error , gorm . ErrRecordNotFound ) { return nil , errors . New ( " " + lr . Email + " " ) } if ! server . IsCorrectPassword ( user . Password , lr . Password ) { return nil , errors . New ( " " ) } tokenString , err := service . authManager . GenerateToken ( user . ID ) if err != nil { return nil , err } return & pb . AuthenticationResponse { Token : tokenString , User : user . AsResponse ( ) , } , nil }
Login returns a JWT token if a user exists with given credentials
Login returns a JWT token if a user exists with given credentials