Spartacus Extend AuthService

Viewed 204

Is it possible to extend the Auth Service and add additional parameters to it?

Currently trying to extend but encounter error such as below:

ERROR in src/app/service/auth/auth-extend.service.ts(15,3): error TS2416: Property 'authorize' in type 'AuthExtendService' is not assignable to the same property in base type 'AuthService'.

Type '(userId: string, password: string, countryCode: string, businessType: string) => void' is not assignable to type '(userId: string, password: string) => void'.

src/app/service/auth/auth-extend.service.ts(21,7): error TS2345: Argument of type '{ userId: string; password: string; countryCode: string; businessType: string; }' is not assignable to parameter of type '{ userId: string; password: string; countryCode: string; businessType: string; }'.

Object literal may only specify known properties, but 'businessType' does not exist in type '{ userId: string; password: string; countryCode: string; businessType: string; }'. Did you mean to write 'businessType'? "

Any one here to tries to do this, like adding new parameters.

1 Answers

Typescript does not support such an overloading out of the box. One way of doing it would be by using optional parameters such as:

export class AuthExtendService extends AuthService {
  authorize(userId: string, password: string, businessType?: string): void {
    console.log(businessType);
    this.store.dispatch(
      new AuthActions.LoadUserToken({
        userId: userId,
        password: password,
      })
    );
  }  
}

This is how you should add extra parameters.

Related