Skip to content

Commit 08e437a

Browse files
committed
add getCreateInstrumentToken
1 parent c06cd80 commit 08e437a

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

apps/api/src/auth/auth.controller.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { Body, Controller, HttpCode, HttpStatus, Post } from '@nestjs/common';
1+
import { CurrentUser } from '@douglasneuroinformatics/libnest';
2+
import type { RequestUser } from '@douglasneuroinformatics/libnest';
3+
import { Body, Controller, Get, HttpCode, HttpStatus, Post } from '@nestjs/common';
24
import { ApiOperation } from '@nestjs/swagger';
35
import { $LoginCredentials } from '@opendatacapture/schemas/auth';
46

@@ -10,6 +12,13 @@ import { AuthService } from './auth.service.js';
1012
export class AuthController {
1113
constructor(private readonly authService: AuthService) {}
1214

15+
@Get('create-instrument-token')
16+
@HttpCode(HttpStatus.OK)
17+
@RouteAccess({ action: 'create', subject: 'Instrument' })
18+
async getCreateInstrumentToken(@CurrentUser() currentUser: RequestUser): Promise<{ accessToken: string }> {
19+
return this.authService.getCreateInstrumentToken(currentUser);
20+
}
21+
1322
@ApiOperation({ summary: 'Login' })
1423
@HttpCode(HttpStatus.OK)
1524
@Post('login')

apps/api/src/auth/auth.service.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CryptoService } from '@douglasneuroinformatics/libnest';
2-
import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
2+
import type { RequestUser } from '@douglasneuroinformatics/libnest';
3+
import { ForbiddenException, Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common';
34
import { JwtService } from '@nestjs/jwt';
45
import type { $LoginCredentials, TokenPayload } from '@opendatacapture/schemas/auth';
56
import type { Group, User } from '@prisma/client';
@@ -17,6 +18,18 @@ export class AuthService {
1718
private readonly usersService: UsersService
1819
) {}
1920

21+
async getCreateInstrumentToken(currentUser: RequestUser) {
22+
if (!currentUser.ability.can('create', 'Instrument')) {
23+
throw new ForbiddenException();
24+
}
25+
26+
const limitedAbility = this.abilityFactory.createForPermissions([{ action: 'create', subject: 'Instrument' }]);
27+
28+
return {
29+
accessToken: await this.jwtService.signAsync({ permissions: limitedAbility.rules }, { expiresIn: '1h' })
30+
};
31+
}
32+
2033
async login(credentials: $LoginCredentials): Promise<{ accessToken: string }> {
2134
let user: User & {
2235
groups: Group[];

0 commit comments

Comments
 (0)