Skip to content

Commit

Permalink
fix: fix validateUserByPayload last_token_at Compare jwt iattz
Browse files Browse the repository at this point in the history
  • Loading branch information
SolidZORO committed May 26, 2020
1 parent 99c247a commit 9c18c02
Show file tree
Hide file tree
Showing 18 changed files with 153 additions and 122 deletions.
9 changes: 9 additions & 0 deletions packages/_leaa-common/src/dtos/user/update-user.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export class UpdateUserInput {
@IsOptional()
roleIds?: string[];

@IsOptional()
last_login_ip?: string;

@IsOptional()
last_login_at?: Date;

@IsOptional()
last_token_at?: Date;

// @IsOptional()
//
// roleSlugs?: string[];
Expand Down
10 changes: 4 additions & 6 deletions packages/_leaa-common/src/entrys/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ export class User extends Base {
@Column({ type: 'int', default: 0 })
is_admin?: number;

@Exclude({ toPlainOnly: true })
@Column({ type: 'varchar', length: 32, nullable: true, select: false })
@Column({ type: 'varchar', length: 32, nullable: true })
last_login_ip?: string;

@Exclude({ toPlainOnly: true })
@Column({ type: 'timestamp', nullable: true, select: false })
@Column({ type: 'timestamp', nullable: true })
last_login_at?: Date;

@Column({ type: 'timestamp', nullable: true, select: false })
@Column({ type: 'timestamp', nullable: true })
last_token_at?: Date;

@Exclude({ toPlainOnly: true })
// @Exclude({ toPlainOnly: true })
@ManyToMany(() => Role, (role) => role.user)
@JoinTable()
roles?: Role[];
Expand Down
8 changes: 4 additions & 4 deletions packages/_leaa-common/src/interfaces/auth.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface IJwtPayload {
id: string;
iat?: number;
exp?: number;
iattz?: string;
id: string; // 'aac87c0a-887f-468a-9596-4be051ce3510'
exp?: number; // 1593108804
iat?: number; // 1590516804
iattz?: string; // '2020-05-26T18:13:24.639Z'
}
1 change: 1 addition & 0 deletions packages/leaa-api/src/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './not-found-ip.exception';
31 changes: 31 additions & 0 deletions packages/leaa-api/src/exceptions/not-found-ip.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { HttpException, HttpStatus } from '@nestjs/common';

export class NotFoundIpException extends HttpException {
/**
* Instantiate a `NotFoundIpException` Exception.
*
* @example
* `throw new NotFoundIpException()`
*
* @usageNotes
* The HTTP response status code will be 404.
* - The `objectOrError` argument defines the JSON response body or the message string.
* - The `description` argument contains a short description of the HTTP error.
*
* By default, the JSON response body contains two properties:
* - `statusCode`: this will be the value 404.
* - `message`: the string `'Not Found'` by default; override this by supplying
* a string in the `objectOrError` parameter.
*
* If the parameter `objectOrError` is a string, the response body will contain an
* additional property, `error`, with a short description of the HTTP error. To override the
* entire JSON response body, pass an object instead. Nest will serialize the object
* and return it as the JSON response body.
*
* @param objectOrError string or object describing the error condition.
* @param description a short description of the HTTP error.
*/
constructor(objectOrError?: string | object | any, description = 'Not Found IP') {
super(HttpException.createBody(objectOrError, description, HttpStatus.NOT_FOUND), HttpStatus.NOT_FOUND);
}
}
4 changes: 2 additions & 2 deletions packages/leaa-api/src/guards/jwt.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export class JwtGuard extends AuthGuard('jwt') {
}

handleRequest(err: Error, user: any, info: any, context: any) {
const { t } = context.getRequest();
// const { t } = context.getRequest();

if (err || !user) throw new UnauthorizedException(t('_error:unauthorized'));
if (err || !user) throw new UnauthorizedException();

return user;
}
Expand Down
11 changes: 8 additions & 3 deletions packages/leaa-api/src/modules/v1/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Post, Req, Body, HttpCode } from '@nestjs/common';
import { Controller, Post, Req, Body, HttpCode, Ip } from '@nestjs/common';
import { AuthService } from '@leaa/api/src/modules/v1/auth/auth.service';
import { ICrudRequest } from '@leaa/api/src/interfaces';
import { AuthLoginInput } from '@leaa/common/src/dtos/auth';
Expand All @@ -9,7 +9,12 @@ export class AuthController {

@HttpCode(200)
@Post('login')
async login(@Req() req: ICrudRequest, @Body() body: AuthLoginInput): Promise<any> {
return this.authService.login(req, body);
async login(@Req() req: ICrudRequest, @Ip() ip: string, @Body() body: AuthLoginInput): Promise<any> {
return this.authService.login(req, ip, body);
}

@Post('user-by-token')
async userByToken(@Req() req: ICrudRequest, @Body() body: any): Promise<any> {
return this.authService.userByToken(body);
}
}
10 changes: 4 additions & 6 deletions packages/leaa-api/src/modules/v1/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthTokenModule } from '@leaa/api/src/modules/v1/auth-token/auth-token.module';

import { User, Role, Permission, Auth, Verification, Action } from '@leaa/common/src/entrys';

import { UserService } from '@leaa/api/src/modules/v1/user/user.service';
import { RoleService } from '@leaa/api/src/modules/v1/role/role.service';
import { PermissionService } from '@leaa/api/src/modules/v1/permission/permission.service';

Expand All @@ -14,14 +12,17 @@ import { AuthController } from '@leaa/api/src/modules/v1/auth/auth.controller';

import { ActionService } from '@leaa/api/src/modules/v1/action/action.service';

import { GithubStrategy, JwtStrategy } from '@leaa/api/src/strategies';
import { JwtStrategy } from '@leaa/api/src/strategies';
import { AttachmentModule } from '@leaa/api/src/modules/v1/attachment/attachment.module';
import { RoleModule } from '@leaa/api/src/modules/v1/role/role.module';

@Module({
imports: [
TypeOrmModule.forFeature([User, Role, Permission, Auth, Verification, Action]),
AuthTokenModule,
AttachmentModule,
RoleModule,
AuthTokenModule,
],
providers: [
// GithubStrategy,
Expand All @@ -30,9 +31,6 @@ import { AttachmentModule } from '@leaa/api/src/modules/v1/attachment/attachment
//
AuthService,
//
// UserResolver,
UserService,
//
RoleService,
PermissionService,
],
Expand Down

0 comments on commit 9c18c02

Please sign in to comment.