#Why is my service override not being called?

10 messages · Page 1 of 1 (latest)

thorny valve
#

I am trying to override a specific function in auth.ts.


import {
    AuthService as OverrideMedusaAPITokenAuthService,
    User,
} from "@medusajs/medusa";
import { AuthenticateResult } from "@medusajs/medusa/dist/types/auth";

class AuthService extends OverrideMedusaAPITokenAuthService {
    async authenticateAPIToken(token: string): Promise<AuthenticateResult> {
        return await this.atomicPhase_(async (transactionManager) => {
            if (process.env.NODE_ENV?.startsWith("dev")) {
                try {
                    const user: User = await this.userService_
                        .withTransaction(transactionManager)
                        .retrieve(token);
                    return {
                        success: true,
                        user,
                    };
                } catch (error) {
                    // ignore
                }
            }

            try {
                console.log("HAHAHAH HERE");
                const user: User = await this.userService_
                    .withTransaction(transactionManager)
                    .retrieveByApiToken(token);
                return {
                    success: true,
                    user,
                };
            } catch (error) {
                return {
                    success: false,
                    error: "Invalid API Token",
                };
            }
        });
    }
}

export default AuthService;

Unfortunately this is not being called. I know the functionality is the same but the console.log is not being called.

gritty forge
#

Are you doing this in a plugin or in the src folder of your medusa project? Checking because a common issue with developing plugins locally is not using yalc or yarn link or some other way of handling the circular reference issue with medusa packages.

thorny valve
gritty forge
#

The next thing I would try is making sure you add a constructor function.

class AuthService extends OverrideMedusaAPITokenAuthService {
   constructor() {
      // @ts-ignore
      super(...arguments)
   }
...
thorny valve
#

The constructor is not being called.

#

maybe I am getting this wrong, but when an admin route is called you put in an API token

#

what checks that API token?

thorny valve