Skip to content

CodeCoupler API Auth Current User

Here an example how to get the current user from within a controller:

import {AuthUserRepository} from '@codecoupler/cc-api-auth/dist/repositories';
import {SecurityBindings, securityId, UserProfile} from '@loopback/security';

export class MyController {
  constructor(
    /* ... */

    //Insert here the integrated user repository
    @repository(AuthUserRepository)
    public authUserRepository: AuthUserRepository,

    //Declare the variable currentUser
    @inject(SecurityBindings.USER)
    private currentUser: UserProfile,

    /* ... */
  ) { }

  /* ... */
  async anyFunction(/* ... */): Promise<MyModel> {

    //Get the current User:
    const me = await this.authUserRepository.findById(parseInt(this.currentUser[securityId]));

    //Now you can access:
    //  me.userId: The id of the integrated user repository
    //  me.externalId: The id specified from the login hook
    //                 Keep in mind, this is a string!

  }

}