Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap 3206 2 - updated swagger interface #439

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/users/dto/create-custom-jwt.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { JwtSignOptions } from "@nestjs/jwt";
import { ApiProperty } from "@nestjs/swagger";
import { Type } from "class-transformer";
import { IsOptional, IsString } from "class-validator";

export class CreateCustomJwt implements JwtSignOptions {
@ApiProperty({
required: false,
description:
"When the token is going to expire. It can be expressed as the number of ms or as a string according to the documentation available at https://github.com/auth0/node-jsonwebtoken#readme",
})
@IsOptional()
@Type(() => String)
@IsString()
readonly expiresIn?: string;
}
9 changes: 7 additions & 2 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { LocalAuthGuard } from "src/auth/guards/local-auth.guard";
import { DatasetClass } from "src/datasets/schemas/dataset.schema";
import { JwtAuthGuard } from "src/auth/guards/jwt-auth.guard";
import { JwtSignOptions } from "@nestjs/jwt";
import { CreateCustomJwt } from "./dto/create-custom-jwt.dto";
//import { AuthController } from "src/auth/auth.controller";

@ApiBearerAuth()
Expand Down Expand Up @@ -286,6 +287,7 @@ export class UsersController {
description:
"It creates a new jwt token for the user specified. Only users in admin groups can create use this endpoint. Token expiration can be custom. Use 'expiresIn: never' for tokens that have no expiration.",
})
@ApiBody({ type: CreateCustomJwt })
@ApiResponse({
status: 201,
type: CreateUserJWT,
Expand All @@ -295,12 +297,15 @@ export class UsersController {
async createCustomJWT(
@Req() request: Request,
@Param("id") id: string,
@Body() jwtProperties: JwtSignOptions,
@Body() jwtProperties: CreateCustomJwt,
): Promise<CreateUserJWT | null> {
const viewedUser = (await this.usersService.findById2JWTUser(
id,
)) as JWTUser;

return this.usersService.createCustomJWT(viewedUser, jwtProperties);
return this.usersService.createCustomJWT(
viewedUser,
jwtProperties as JwtSignOptions,
);
}
}
10 changes: 9 additions & 1 deletion src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,17 @@ export class UsersService implements OnModuleInit {
user: JWTUser,
jwtProperties: JwtSignOptions,
): Promise<CreateUserJWT | null> {
const signAndVerifyOptions = { ...jwtProperties };
const signAndVerifyOptions: JwtSignOptions = {
...jwtProperties,
} as JwtSignOptions;
if (signAndVerifyOptions.expiresIn == "never") {
delete signAndVerifyOptions.expiresIn;
} else if (
typeof signAndVerifyOptions.expiresIn === "string" &&
signAndVerifyOptions.expiresIn &&
!isNaN(+signAndVerifyOptions.expiresIn)
) {
signAndVerifyOptions.expiresIn = parseInt(signAndVerifyOptions.expiresIn);
}
signAndVerifyOptions.secret = this.configService.get<string>("jwt.secret");
const payload = {
Expand Down