feat: log staff sign-in and sign-out and force logout at a set time

This commit is contained in:
2026-10-19 13:34:55 +03:30
parent 3863c5e7d1
commit 8590579605
25 changed files with 588 additions and 15 deletions
+6
View File
@@ -80,6 +80,12 @@ export class AuthController {
return this.authService.listMyOrganizations(user.userId);
}
@UseGuards(JwtAuthGuard)
@Post('logout')
logout(@CurrentUser() user: Actor) {
return this.authService.logout(user);
}
@UseGuards(JwtAuthGuard)
@Get('me')
me(@CurrentUser() user: Actor) {
+7
View File
@@ -5,6 +5,8 @@ import { PassportModule } from '@nestjs/passport';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MembershipsModule } from '../memberships/memberships.module';
import { BillingModule } from '../billing/billing.module';
import { AttendanceModule } from '../attendance/attendance.module';
import { WorkdayInterceptor } from '../attendance/workday.interceptor';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { JwtStrategy } from './jwt.strategy';
@@ -17,6 +19,7 @@ import { SmsService } from '../common/sms.service';
ConfigModule,
MembershipsModule,
BillingModule,
AttendanceModule,
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.registerAsync({
imports: [ConfigModule],
@@ -39,6 +42,10 @@ import { SmsService } from '../common/sms.service';
provide: APP_INTERCEPTOR,
useClass: ViewAsReadOnlyInterceptor,
},
{
provide: APP_INTERCEPTOR,
useClass: WorkdayInterceptor,
},
],
exports: [AuthService, RolesGuard, JwtModule],
})
+7
View File
@@ -26,6 +26,7 @@ import { SmsService } from '../common/sms.service';
import { LocalFileStorage } from '../storage/local-file.storage';
import { MembershipsService } from '../memberships/memberships.service';
import { BillingService } from '../billing/billing.service';
import { AttendanceService } from '../attendance/attendance.service';
import type { Actor } from '../common/record-scope';
import {
LoginDto,
@@ -110,6 +111,7 @@ export class AuthService {
private readonly files: LocalFileStorage,
private readonly memberships: MembershipsService,
private readonly billing: BillingService,
private readonly attendance: AttendanceService,
) {}
/**
@@ -407,6 +409,10 @@ export class AuthService {
return this.finishLogin(user.id);
}
async logout(actor: Actor) {
return this.attendance.recordLogout(actor);
}
async me(userId: string, organizationId: string, actor?: Actor): Promise<AuthUserDto> {
await this.memberships.requireActive(userId, organizationId);
const user = await this.prisma.user.findUnique({ where: { id: userId } });
@@ -727,6 +733,7 @@ export class AuthService {
where: { id: userId },
data: { lastLoginAt: new Date() },
});
await this.attendance.recordLogin(organizationId, userId, membership.role);
}
const accessToken = this.jwt.sign({
sub: userId,
+2 -1
View File
@@ -34,7 +34,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
});
}
async validate(payload: JwtPayload & { typ?: string; mobile?: string }) {
async validate(payload: JwtPayload & { typ?: string; mobile?: string; iat?: number }) {
if (
payload.typ === 'portal' ||
payload.typ === 'org-select' ||
@@ -135,6 +135,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
priorGranted && membership.organization.priorArchiveEnabledAt
? membership.organization.priorArchiveEnabledAt
: null,
sessionStartedAt: payload.iat ? new Date(payload.iat * 1000) : new Date(),
...(viewAsReadOnly && impersonatorUserId
? {
impersonatorUserId,