mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 12:20:31 +00:00
refactor(nestjs): hook up auth endpoints
This commit is contained in:
@@ -1,21 +1,22 @@
|
||||
// @ts-nocheck
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Inject,
|
||||
Param,
|
||||
Post,
|
||||
Request,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiBody, ApiParam } from '@nestjs/swagger';
|
||||
import { JwtAuthGuard, PublicRoute } from './guards/jwt.guard';
|
||||
import { PublicRoute } from './guards/jwt.guard';
|
||||
import { AuthenticationApplication } from './AuthApplication.sevice';
|
||||
import { AuthSignupDto } from './dtos/AuthSignup.dto';
|
||||
import { AuthSigninDto } from './dtos/AuthSignin.dto';
|
||||
import { LocalAuthGuard } from './guards/local.guard';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { AuthSigninService } from './commands/AuthSignin.service';
|
||||
import { TenantModel } from '../System/models/TenantModel';
|
||||
import { SystemUser } from '../System/models/SystemUser';
|
||||
|
||||
@Controller('/auth')
|
||||
@ApiTags('Auth')
|
||||
@@ -24,15 +25,25 @@ export class AuthController {
|
||||
constructor(
|
||||
private readonly authApp: AuthenticationApplication,
|
||||
private readonly authSignin: AuthSigninService,
|
||||
|
||||
@Inject(TenantModel.name)
|
||||
private readonly tenantModel: typeof TenantModel,
|
||||
) {}
|
||||
|
||||
@Post('/signin')
|
||||
@UseGuards(LocalAuthGuard)
|
||||
@ApiOperation({ summary: 'Sign in a user' })
|
||||
@ApiBody({ type: AuthSigninDto })
|
||||
signin(@Request() req: Request, @Body() signinDto: AuthSigninDto) {
|
||||
async signin(@Request() req: Request & { user: SystemUser }, @Body() signinDto: AuthSigninDto) {
|
||||
const { user } = req;
|
||||
return { access_token: this.authSignin.signToken(user) };
|
||||
const tenant = await this.tenantModel.query().findById(user.tenantId);
|
||||
|
||||
return {
|
||||
accessToken: this.authSignin.signToken(user),
|
||||
organizationId: tenant.organizationId,
|
||||
tenantId: tenant.id,
|
||||
userId: user.id,
|
||||
};
|
||||
}
|
||||
|
||||
@Post('/signup')
|
||||
|
||||
@@ -28,11 +28,14 @@ import { MailModule } from '../Mail/Mail.module';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { InjectSystemModel } from '../System/SystemModels/SystemModels.module';
|
||||
import { GetAuthMetaService } from './queries/GetAuthMeta.service';
|
||||
import { AuthedController } from './Authed.controller';
|
||||
import { GetAuthenticatedAccount } from './queries/GetAuthedAccount.service';
|
||||
import { TenancyModule } from '../Tenancy/Tenancy.module';
|
||||
|
||||
const models = [InjectSystemModel(PasswordReset)];
|
||||
|
||||
@Module({
|
||||
controllers: [AuthController],
|
||||
controllers: [AuthController, AuthedController],
|
||||
imports: [
|
||||
MailModule,
|
||||
PassportModule.register({ defaultStrategy: 'jwt' }),
|
||||
@@ -45,9 +48,9 @@ const models = [InjectSystemModel(PasswordReset)];
|
||||
}),
|
||||
}),
|
||||
TenantDBManagerModule,
|
||||
TenancyModule,
|
||||
BullModule.registerQueue({ name: SendResetPasswordMailQueue }),
|
||||
BullModule.registerQueue({ name: SendSignupVerificationMailQueue }),
|
||||
|
||||
],
|
||||
exports: [...models],
|
||||
providers: [
|
||||
@@ -65,6 +68,7 @@ const models = [InjectSystemModel(PasswordReset)];
|
||||
SendResetPasswordMailProcessor,
|
||||
SendSignupVerificationMailProcessor,
|
||||
GetAuthMetaService,
|
||||
GetAuthenticatedAccount,
|
||||
{
|
||||
provide: APP_GUARD,
|
||||
useClass: JwtAuthGuard,
|
||||
|
||||
23
packages/server/src/modules/Auth/Authed.controller.ts
Normal file
23
packages/server/src/modules/Auth/Authed.controller.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { GetAuthenticatedAccount } from './queries/GetAuthedAccount.service';
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { IgnoreTenantSeededRoute } from '../Tenancy/EnsureTenantIsSeeded.guards';
|
||||
import { IgnoreTenantInitializedRoute } from '../Tenancy/EnsureTenantIsInitialized.guard';
|
||||
|
||||
@Controller('/auth')
|
||||
@ApiTags('Auth')
|
||||
@IgnoreTenantSeededRoute()
|
||||
@IgnoreTenantInitializedRoute()
|
||||
export class AuthedController {
|
||||
constructor(
|
||||
private readonly getAuthedAccountService: GetAuthenticatedAccount,
|
||||
) {}
|
||||
|
||||
@Get('/account')
|
||||
@ApiOperation({ summary: 'Retrieve the authenticated account' })
|
||||
async getAuthedAcccount() {
|
||||
const data = await this.getAuthedAccountService.getAccount();
|
||||
|
||||
return { data };
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ export class AuthSigninService {
|
||||
}
|
||||
if (!user.verified) {
|
||||
throw new UnauthorizedException(
|
||||
`The user is not verified yet, check out your mail inbox.`
|
||||
`The user is not verified yet, check out your mail inbox.`,
|
||||
);
|
||||
}
|
||||
return user;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { TenancyContext } from '@/modules/Tenancy/TenancyContext.service';
|
||||
import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectable.service';
|
||||
import { GetAuthedAccountTransformer } from './GetAuthedAccount.transformer';
|
||||
|
||||
@Injectable()
|
||||
export class GetAuthenticatedAccount {
|
||||
constructor(
|
||||
private readonly tenancyContext: TenancyContext,
|
||||
private readonly transformer: TransformerInjectable,
|
||||
) {}
|
||||
|
||||
async getAccount() {
|
||||
const account = await this.tenancyContext.getSystemUser();
|
||||
|
||||
return this.transformer.transform(
|
||||
account,
|
||||
new GetAuthedAccountTransformer(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Transformer } from '@/modules/Transformer/Transformer';
|
||||
|
||||
export class GetAuthedAccountTransformer extends Transformer {
|
||||
/**
|
||||
* Include these attributes to sale invoice object.
|
||||
* @returns {Array}
|
||||
*/
|
||||
public includeAttributes = (): string[] => {
|
||||
return [
|
||||
'firstName',
|
||||
'lastName',
|
||||
'email',
|
||||
'active',
|
||||
'language',
|
||||
'tenantId',
|
||||
'verified',
|
||||
];
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user