Skip to content

CodeCoupler API Auth Current User

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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!

  }

}
Back to top