Node.js Admin SDK

Installation

npm install --save @authembed/admin
# or
yarn add @authembed/admin

Usage

import { AuthembedAdminSDK, AuthembedUser } from '@authembed/admin';

const authembedAdminSdk = new AuthembedAdminSDK({
    url: 'https://authembed.example.com',
    username: 'main_backend',
    password: '...',
});

// ...

const user: AuthembedUser = await authembedAdminSdk.getUserByToken(request.headers.authorization);

console.log(user);
// { _id: '...', name: 'John Doe', email: 'john@example.com', ... }

Get users by IDs

const usersByIds =
  await authembedAdminSdk.getUsersByIds(["609eeba19c7f152a53676191", ...]);
  
console.log(usersByIds);
// { "609eeba19c7f152a53676191": { _id: "...", name: "..." }, ... }

Update a user

const updatedUser =
  await authembedAdminSdk.updateUser({
    _id: "609eeba19c7f152a53676191",
    name: "New name",
  });

Usage with Nest.js framework

The SDK also has built in support for Nest.js framework.

app.module.ts
import { AuthembedAdminSDKModule } from '@authembed/admin/nestjs';
 
@Module({
  imports: [
    AuthembedAdminSDKModule.forRoot({ url: '...', username: '...', password: '...'})
  ]
})
export class AppModule {}
app.module.ts
import { AuthembedAdminSDKModule } from '@authembed/admin/nestjs';
 
@Module({
  imports: [
    AuthembedAdminSDKModule.forRootAsync({
      useFactory: (config: ConfigService) => ({
        url: config.get('AUTHEMBED_URL'),
        ...
      }),
      inject: [ConfigService],
    }),
  ]
})
export class AppModule {}
app.service.ts
import { InjectAuthembedAdminSDK } from '@authembed/admin/nestjs';
import { AuthembedAdminSDK } from '@authembed/admin';
 
@Injectable()
export class AppService {
  constructor(
    @InjectAuthembedAdminSDK()
    private readonly authembedAdminSdk: AuthembedAdminSDK,
  ) {}
 
  async authenticateUserByToken(token: string): Promise<void> {
    await this.authembedAdminSdk.getUserByToken(token);
  }
}

Last updated

Was this helpful?