ToolJet/server/src/controllers/app.controller.ts

70 lines
2.4 KiB
TypeScript
Raw Normal View History

import { Controller, Get, Request, Post, UseGuards, Body, Param, BadRequestException } from '@nestjs/common';
import { User } from 'src/decorators/user.decorator';
import { JwtAuthGuard } from '../../src/modules/auth/jwt-auth.guard';
import { AppAuthenticationDto, AppForgotPasswordDto, AppPasswordResetDto } from '@dto/app-authentication.dto';
import { AuthService } from '../services/auth.service';
import { SignupDisableGuard } from 'src/modules/auth/signup-disable.guard';
import { CreateUserDto } from '@dto/user.dto';
import { AcceptInviteDto } from '@dto/accept-organization-invite.dto';
2021-07-08 05:40:27 +00:00
@Controller()
export class AppController {
2021-07-08 07:39:07 +00:00
constructor(private authService: AuthService) {}
2021-07-08 05:40:27 +00:00
@Post(['authenticate', 'authenticate/:organizationId'])
async login(@Body() appAuthDto: AppAuthenticationDto, @Param('organizationId') organizationId) {
return this.authService.login(appAuthDto.email, appAuthDto.password, organizationId);
}
@UseGuards(JwtAuthGuard)
@Get('switch/:organizationId')
async switch(@Param('organizationId') organizationId, @User() user) {
if (!organizationId) {
throw new BadRequestException();
}
return await this.authService.switchOrganization(organizationId, user);
2021-07-08 07:39:07 +00:00
}
@UseGuards(SignupDisableGuard)
@Post('set-password-from-token')
async create(@Body() userCreateDto: CreateUserDto) {
await this.authService.setupAccountFromInvitationToken(userCreateDto);
return {};
}
@Post('accept-invite')
async acceptInvite(@Body() acceptInviteDto: AcceptInviteDto) {
await this.authService.acceptOrganizationInvite(acceptInviteDto);
return {};
}
@UseGuards(SignupDisableGuard)
2021-07-19 05:42:16 +00:00
@Post('signup')
async signup(@Body() appAuthDto: AppAuthenticationDto) {
return this.authService.signup(appAuthDto.email);
2021-07-19 05:42:16 +00:00
}
@Post('/forgot-password')
async forgotPassword(@Body() appAuthDto: AppForgotPasswordDto) {
await this.authService.forgotPassword(appAuthDto.email);
return {};
2021-07-26 16:02:47 +00:00
}
@Post('/reset-password')
async resetPassword(@Body() appAuthDto: AppPasswordResetDto) {
const { token, password } = appAuthDto;
2021-07-26 16:02:47 +00:00
await this.authService.resetPassword(token, password);
return {};
2021-07-26 16:02:47 +00:00
}
2021-07-31 08:45:03 +00:00
@Get('/health')
async healthCheck(@Request() req) {
return { works: 'yeah' };
}
2022-08-31 15:14:08 +00:00
@Get('/')
async rootPage(@Request() req) {
return { message: 'Instance seems healthy but this is probably not the right URL to access.' };
}
2021-07-08 05:40:27 +00:00
}