fix: ts compilation errors in auth

This commit is contained in:
Hamza-Ayed
2026-07-16 16:05:28 +03:00
parent 83f2d0854a
commit 4f11580d4a
7 changed files with 68 additions and 2 deletions
@@ -1,6 +1,8 @@
import { Controller, Post, Body, Headers, UnauthorizedException } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { AuthService } from './auth.service';
@ApiTags('auth')
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@@ -23,4 +25,10 @@ export class AuthController {
if (!tenantId) throw new UnauthorizedException('Tenant ID (x-tenant-id) is required');
return this.authService.verifyOtp(tenantId, phone, code);
}
@Post('refresh')
async refresh(@Body('refresh_token') refreshToken: string) {
if (!refreshToken) throw new UnauthorizedException('refresh_token is required');
return this.authService.refresh(refreshToken);
}
}
+1 -1
View File
@@ -17,7 +17,7 @@ import { JwtStrategy } from './strategies/jwt.strategy';
useFactory: async (configService: ConfigService) => ({
secret: configService.get<string>('JWT_SECRET') || 'change_me_jwt_secret',
signOptions: {
expiresIn: configService.get<string>('JWT_EXPIRES') || '15m',
expiresIn: (configService.get<string>('JWT_EXPIRES') || '15m') as any,
},
}),
}),
+1 -1
View File
@@ -69,7 +69,7 @@ export class AuthService {
access_token: this.jwtService.sign(base),
refresh_token: this.jwtService.sign(
{ ...base, type: 'refresh' },
{ expiresIn: this.config.get<string>('jwt.refreshExpires') ?? '30d' },
{ expiresIn: (this.config.get<string>('jwt.refreshExpires') ?? '30d') as any },
),
user,
};
@@ -0,0 +1,16 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
export interface AuthUser {
userId: string;
phone: string;
role: string;
tenantId: string;
}
/**
* @CurrentUser() — يحقن المستخدم المصادَق (من JwtStrategy.validate) في المتحكّم.
*/
export const CurrentUser = createParamDecorator(
(_data: unknown, ctx: ExecutionContext): AuthUser =>
ctx.switchToHttp().getRequest().user,
);
@@ -0,0 +1,27 @@
import { Body, Controller, Get, Patch, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { UsersService } from './users.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import {
CurrentUser,
AuthUser,
} from '../auth/decorators/current-user.decorator';
@ApiTags('users')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('users')
export class UsersController {
constructor(private readonly users: UsersService) {}
// بيانات المستخدم المصادَق حالياً — يثبت أن سلسلة JWT+الحارس تعمل.
@Get('me')
me(@CurrentUser() user: AuthUser) {
return this.users.findById(user.tenantId, user.userId);
}
@Patch('me')
updateMe(@CurrentUser() user: AuthUser, @Body('name') name: string) {
return this.users.updateProfile(user.tenantId, user.userId, { name });
}
}
@@ -2,9 +2,11 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
@Module({
imports: [TypeOrmModule.forFeature([User])],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService],
})
@@ -22,4 +22,17 @@ export class UsersService {
const user = this.userRepository.create({ tenant_id: tenantId, phone, role: role as UserRole });
return this.userRepository.save(user);
}
async listByTenant(tenantId: string): Promise<User[]> {
return this.userRepository.find({ where: { tenant_id: tenantId } });
}
async updateProfile(
tenantId: string,
id: string,
data: Partial<Pick<User, 'name'>>,
): Promise<User | null> {
await this.userRepository.update({ tenant_id: tenantId, id }, { name: data.name });
return this.findById(tenantId, id);
}
}