Skip to content
Closed
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
4 changes: 4 additions & 0 deletions template/nest-next/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { RedisSessionRepository } from './repository/redis-session.repository';
import {
IssueApiTokenService,
IssueTokenSerivce,
RevokeAllUserSessionService,
RevokeApiTokenService,
RevokeTokenService,
} from './commands';
Expand All @@ -20,6 +21,7 @@ import { GetTokenService } from './queries/get-token';
import { TokenRepository } from './repository/token.repository';
import { ApiTokenService } from './api-token.service';
import { RefreshTokenService } from './commands/refresh-token.command';
import { KickoutUserEventHandler } from './event-handler';

@Module({
imports: [MikroOrmModule.forFeature([User])],
Expand All @@ -39,7 +41,9 @@ import { RefreshTokenService } from './commands/refresh-token.command';
GetTokenService,
RevokeTokenService,
GetApiTokenService,
RevokeAllUserSessionService,
RefreshTokenService,
KickoutUserEventHandler,
],
exports: [AuthService, ApiTokenService],
})
Expand Down
1 change: 1 addition & 0 deletions template/nest-next/src/auth/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './issue-api-token.command';
export * from './issue-token.command';
export * from './revoke-api-token.command';
export * from './revoke-token.command';
export * from './revoke-all-user-session.command';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Command, ICommandHandler } from '@nestjs/cqrs';
import { UserId } from '../../user';
import { RedisSessionRepository } from '../repository/redis-session.repository';

export class RevokeAllUserSession extends Command<void> {
constructor(public readonly uid: UserId) {
super();
}
}

export class RevokeAllUserSessionService implements ICommandHandler<RevokeAllUserSession> {
constructor(private readonly sessionRepository: RedisSessionRepository) {}

async execute(command: RevokeAllUserSession) {
await this.sessionRepository.revokeAllSession(command.uid);
}
}
1 change: 1 addition & 0 deletions template/nest-next/src/auth/event-handler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { KickoutUserEventHandler } from './kick-out-user';
11 changes: 11 additions & 0 deletions template/nest-next/src/auth/event-handler/kick-out-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CommandBus, EventsHandler, IEventHandler } from '@nestjs/cqrs';
import { UserPasswordChangedEvent, UserRemovedEvent } from '../../user';
import { RevokeAllUserSession } from '../commands';

@EventsHandler(UserRemovedEvent, UserPasswordChangedEvent)
export class KickoutUserEventHandler implements IEventHandler<UserRemovedEvent> {
constructor(private readonly cb: CommandBus) {}
async handle(event: UserRemovedEvent) {
await this.cb.execute(new RevokeAllUserSession(event.userId));
}
}
1 change: 1 addition & 0 deletions template/nest-next/src/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { User, UserRole } from './user.entity';
export type { UserId } from './user.entity';
export * from './error';
export * from './events';
Loading