-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.module.ts
More file actions
115 lines (98 loc) · 2 KB
/
auth.module.ts
File metadata and controls
115 lines (98 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {
IsEmail,
IsString,
MinLength,
} from "class-validator";
import {
Body,
Controller,
Injectable,
Middleware,
Module,
Post,
Returns,
Route,
Summary,
Tags,
} from "../../src/index.js";
import {
DatabaseModule,
DatabaseService,
} from "./database.module.js";
import { rateLimiter } from "./server.js";
// --- Services ---
@Injectable()
export class JwtService {
sign(_payload: object) {
return "token";
}
verify(_token: string) {
return {};
}
}
@Injectable()
export class AuthService {
constructor(
private readonly db: DatabaseService,
private readonly jwt: JwtService,
) {}
login(_email: string, _password: string) {
void this.db;
return { token: this.jwt.sign({ sub: "1" }) };
}
register(_email: string, _password: string) {
return { id: "1" };
}
}
// --- Controller ---
class LoginBody {
@IsEmail()
email!: string;
@IsString()
@MinLength(1)
password!: string;
}
class TokenResponse {
@IsString()
token!: string;
}
class MessageResponse {
@IsString()
message!: string;
}
@Route("/auth")
@Tags("Auth")
@Middleware(rateLimiter)
export class AuthController extends Controller {
constructor(
private readonly authService: AuthService,
) {
super();
}
@Post("/login")
@Summary("Login with email and password")
@Returns(200, TokenResponse, "JWT token")
login(@Body(LoginBody) body: { email: string; password: string; }) {
return this.authService.login(body.email, body.password);
}
@Post("/register")
@Summary("Register a new account")
@Returns(201, MessageResponse, "Created")
register(@Body(LoginBody) body: { email: string; password: string; }) {
void this.authService.register(body.email, body.password);
this.setStatus(201);
return { message: "Created" };
}
@Post("/logout")
@Summary("Logout and invalidate session")
@Returns(204, undefined, "No content")
logout() {}
}
// --- Module ---
@Module({
imports: [DatabaseModule],
controllers: [AuthController],
providers: [AuthService, JwtService],
exports: [AuthService],
})
export class AuthModule {}