Newer
Older
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
import { Args, Mutation, Resolver } from '@nestjs/graphql';
import { LoginModel } from './models/login.model';
import { PrismaService } from '../prisma/prisma.service';
import { randomBytes } from 'crypto';
import { AgentFlags } from './models/agent.model';
@Resolver()
export class AuthResolver {
constructor(private readonly prisma: PrismaService) {}
@Mutation(() => LoginModel)
async login(@Args('slug') slug: string) {
const [group, agent] = await Promise.all([
this.prisma.group.findFirst({
where: { slug },
}),
this.prisma.agent.findFirst({
where: { slug },
}),
]);
let entityId, role;
if (agent) {
entityId = agent.id;
if ((agent.flags & AgentFlags.ADMIN) === AgentFlags.ADMIN)
role = 'admin';
else role = 'agent';
} else if (group) {
entityId = group.id;
role = 'group';
}
return await this.prisma.accessToken.create({
data: {
entityId,
role,
authToken: randomBytes(36).toString('base64url'),
},
});
}
}