-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclerk-auth-user.provider.ts
More file actions
53 lines (46 loc) · 1.78 KB
/
Copy pathclerk-auth-user.provider.ts
File metadata and controls
53 lines (46 loc) · 1.78 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import 'dotenv/config';
import { AppConfigService } from '@/app-config/app-config.service';
import { AuthRepository } from '@/auth/auth.repository';
import { AuthProvider } from '@/auth/providers/auth-provider';
import { User } from '@/common/types/user';
import { users } from '@clerk/clerk-sdk-node';
import { Injectable } from '@nestjs/common';
@Injectable()
export class ClerkAuthUserProvider implements AuthProvider {
constructor(
private readonly appConfigService: AppConfigService,
private readonly authRepository: AuthRepository
) {}
async findUser(id: string): Promise<User | undefined> {
const userRoles = await this.authRepository.findUserRolesForSingleUser(id);
const user = await users.getUser(id);
return {
...user,
roles: userRoles.roles,
};
}
async findUsers(ids: string[] | undefined): Promise<User[]> {
const userRolesList = await this.authRepository.findUserRolesForMultiUsers(
ids
);
// FIXME: get around the limit imposed by clerk when pagination is implemented
const userList = await users.getUserList(
ids ? { userId: ids, limit: 100 } : { limit: 100 }
);
return userList.map((user) => ({
...user,
roles:
userRolesList.find((userRoles) => userRoles.userId === user.id)
?.roles ?? [],
}));
}
async assignRolesToUser(id: string, roles: string[]): Promise<void> {
const allRoles = await this.appConfigService.getAppRoles();
await this.authRepository.assignRolesToUser(id, roles, allRoles);
}
async assignDefaultRoleToUser(id: string): Promise<void> {
const allRoles = await this.appConfigService.getAppRoles();
const defaultRole = this.appConfigService.getDefaultRole();
await this.authRepository.assignRolesToUser(id, [defaultRole], allRoles);
}
}