Skip to content

Commit ded56fd

Browse files
authored
Merge pull request #60 from feyishola/fix-type-safety
type safety enhancement
2 parents f0ad81f + e2bdd1b commit ded56fd

17 files changed

Lines changed: 2112 additions & 45 deletions

src/api-keys/api-key.types.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// API Key type definitions
2+
3+
export interface ApiKey {
4+
id: string;
5+
name: string;
6+
key: string;
7+
keyPrefix: string;
8+
scopes: string[];
9+
requestCount: bigint;
10+
lastUsedAt?: Date;
11+
isActive: boolean;
12+
rateLimit?: number;
13+
createdAt: Date;
14+
updatedAt: Date;
15+
}
16+
17+
export interface CreateApiKeyDto {
18+
name: string;
19+
scopes: string[];
20+
rateLimit?: number;
21+
}
22+
23+
export interface UpdateApiKeyDto {
24+
name?: string;
25+
scopes?: string[];
26+
rateLimit?: number;
27+
isActive?: boolean;
28+
}
29+
30+
export interface ApiKeyQueryDto {
31+
page?: number;
32+
limit?: number;
33+
isActive?: boolean;
34+
search?: string;
35+
}
36+
37+
export interface ApiKeyResponseDto {
38+
id: string;
39+
name: string;
40+
keyPrefix: string;
41+
scopes: string[];
42+
requestCount: string;
43+
lastUsedAt?: Date;
44+
isActive: boolean;
45+
rateLimit?: number;
46+
createdAt: Date;
47+
updatedAt: Date;
48+
}
49+
50+
export interface ApiKeyValidationResult {
51+
isValid: boolean;
52+
apiKey?: ApiKey;
53+
error?: string;
54+
remainingRequests?: number;
55+
resetTime?: number;
56+
}
57+
58+
export interface ApiKeyRateLimitInfo {
59+
limit: number;
60+
remaining: number;
61+
resetTime: number;
62+
window: number;
63+
}
64+
65+
export interface ApiKeyUsageStats {
66+
totalRequests: number;
67+
requestsToday: number;
68+
requestsThisMonth: number;
69+
averageDailyRequests: number;
70+
peakHour: number;
71+
lastUsedAt?: Date;
72+
}
73+
74+
export interface ApiKeyScope {
75+
resource: string;
76+
action: string;
77+
description: string;
78+
}
79+
80+
export interface ApiKeyWithUsage extends ApiKey {
81+
usageStats: ApiKeyUsageStats;
82+
rateLimitInfo: ApiKeyRateLimitInfo;
83+
}
84+
85+
export interface ApiKeyRequestContext {
86+
apiKey?: ApiKey;
87+
ipAddress: string;
88+
userAgent: string;
89+
timestamp: Date;
90+
endpoint: string;
91+
method: string;
92+
}

src/auth/auth.service.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import * as bcrypt from 'bcrypt';
77
import { RedisService } from '../common/services/redis.service';
88
import { v4 as uuidv4 } from 'uuid';
99
import { StructuredLoggerService } from '../common/logging/logger.service';
10+
import { AuthUser, JwtPayload, AuthTokens } from './auth.types';
11+
import { PrismaUser } from '../types/prisma.types';
12+
import { isObject, isString } from '../types/guards';
1013

1114
@Injectable()
1215
export class AuthService {
@@ -28,8 +31,9 @@ export class AuthService {
2831
return {
2932
message: 'User registered successfully. Please check your email for verification.',
3033
};
31-
} catch (error) {
32-
this.logger.error('User registration failed', error.stack, {
34+
} catch (error: unknown) {
35+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
36+
this.logger.error('User registration failed', errorMessage, {
3337
email: createUserDto.email,
3438
});
3539
throw error;
@@ -81,8 +85,9 @@ export class AuthService {
8185

8286
this.logger.logAuth('User login successful', { userId: user.id });
8387
return this.generateTokens(user);
84-
} catch (error) {
85-
this.logger.error('User login failed', error.stack, {
88+
} catch (error: unknown) {
89+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
90+
this.logger.error('User login failed', errorMessage, {
8691
email: credentials.email,
8792
});
8893
throw error;

src/auth/auth.types.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Authentication type definitions
2+
3+
export interface AuthUser {
4+
id: string;
5+
email: string;
6+
walletAddress?: string;
7+
password?: string;
8+
firstName?: string;
9+
lastName?: string;
10+
isVerified: boolean;
11+
role: string;
12+
createdAt: Date;
13+
updatedAt: Date;
14+
}
15+
16+
export interface JwtPayload {
17+
sub: string;
18+
email: string;
19+
jti?: string;
20+
iat?: number;
21+
exp?: number;
22+
}
23+
24+
export interface AuthTokens {
25+
access_token: string;
26+
refresh_token: string;
27+
user: {
28+
id: string;
29+
email: string;
30+
walletAddress?: string;
31+
isVerified: boolean;
32+
};
33+
}
34+
35+
export interface LoginRequest {
36+
email: string;
37+
password: string;
38+
}
39+
40+
export interface Web3LoginRequest {
41+
walletAddress: string;
42+
signature: string;
43+
}
44+
45+
export interface RefreshTokenRequest {
46+
refresh_token: string;
47+
}
48+
49+
export interface RegisterRequest {
50+
email: string;
51+
password: string;
52+
firstName?: string;
53+
lastName?: string;
54+
walletAddress?: string;
55+
}
56+
57+
export interface PasswordResetRequest {
58+
email: string;
59+
}
60+
61+
export interface PasswordResetConfirmRequest {
62+
token: string;
63+
newPassword: string;
64+
}
65+
66+
export interface MfaSetupRequest {
67+
method: 'totp' | 'sms' | 'email';
68+
phoneNumber?: string;
69+
email?: string;
70+
}
71+
72+
export interface MfaVerifyRequest {
73+
method: string;
74+
code: string;
75+
}
76+
77+
export interface SessionInfo {
78+
userId: string;
79+
jti: string;
80+
createdAt: string;
81+
userAgent: string;
82+
ip: string;
83+
lastActivity?: string;
84+
}
85+
86+
export interface LoginAttempt {
87+
email: string;
88+
ip: string;
89+
timestamp: Date;
90+
success: boolean;
91+
userAgent?: string;
92+
}
93+
94+
export interface AccountLockInfo {
95+
email: string;
96+
ip: string;
97+
lockoutUntil: Date;
98+
failedAttempts: number;
99+
lastAttempt: Date;
100+
}
101+
102+
export interface TokenBlacklistEntry {
103+
jti: string;
104+
userId: string;
105+
blacklistedAt: Date;
106+
reason?: string;
107+
}
108+
109+
export interface AuthRequestContext {
110+
user?: AuthUser;
111+
session?: SessionInfo;
112+
ip: string;
113+
userAgent: string;
114+
timestamp: Date;
115+
}

0 commit comments

Comments
 (0)