-
Notifications
You must be signed in to change notification settings - Fork 2
/
Strategy.ts
46 lines (39 loc) · 892 Bytes
/
Strategy.ts
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
class User {
githubToken: string;
jwtToken: string;
}
interface AuthStrategy {
auth(user: User): boolean;
}
class Auth {
constructor(private strategy: AuthStrategy) {}
setStrategy(strategy: AuthStrategy) {
this.strategy = strategy
}
public authUser(user: User): boolean {
return this.strategy.auth(user);
}
}
class JWTStrategy implements AuthStrategy {
auth(user: User): boolean {
if (user.jwtToken) {
return true
}
return false
}
}
class GitHubStrategy implements AuthStrategy {
auth(user: User): boolean {
if (user.githubToken) {
return true
}
return false
}
}
const user = new User();
user.jwtToken = 'token';
const auth = new Auth(new JWTStrategy())
auth.authUser(user);
console.log(auth)
auth.setStrategy(new GitHubStrategy())
console.log(auth)