This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
/
auth.route.ts
137 lines (126 loc) · 3.46 KB
/
auth.route.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {UserObject} from '../objects/user.object';
import {HelperService} from '../services/helper.service';
import {AuthService} from '../services/auth.service';
export class AuthRoute {
endpoint = '/auth';
disabled = ['post', 'put', 'delete'];
errors = {
'auth/invalid-input': 'Invalid input.',
'auth/invalid-token': 'Invalid token.',
'auth/invalid-email': 'Invalid email.',
'auth/invalid-password': 'Invalid password.',
'auth/user-exists': 'User already exists.',
'auth/user-not-exists': 'No user.',
};
constructor(
private helperService: HelperService,
private authService: AuthService
) {}
/**
* Create new account
*/
put(req: {
body: {
email?: string;
password?: string;
};
}) {
const {email, password} = req.body;
let user: UserObject;
if (!email && !password) {
user = this.authService.getUserAnonymously();
} else {
if (!this.helperService.isValidEmail(email as string)) {
throw new Error('auth/invalid-email');
}
if (!this.authService.isValidPassword(password as string)) {
throw new Error('auth/invalid-password');
}
user = this.authService.getUserByEmailAndPassword(
email as string,
password as string
);
}
// check if user exists
const {isNewUser} = user.getInfo();
if (!isNewUser) {
throw new Error('auth/user-exists');
}
// result
user.setlastLogin().save(); // update last login
const {refreshToken} = user.getData();
return {
info: user.getInfo(),
idToken: user.getIdToken(),
refreshToken,
};
}
/**
* Log a user in
*/
post(req: {
body: {
email?: string;
password?: string;
customToken?: string;
offlineAccess?: boolean;
};
}) {
const {email, password, customToken, offlineAccess = false} = req.body;
// get user
let user: UserObject | undefined;
if (!email && !customToken) {
throw new Error('auth/invalid-input');
} else if (customToken) {
user = this.authService.getUserByCustomToken(customToken);
} else if (email) {
if (!this.helperService.isValidEmail(email)) {
throw new Error('auth/invalid-email');
}
if (!password || !this.authService.isValidPassword(password)) {
throw new Error('auth/invalid-password');
}
user = this.authService.getUserByEmailAndPassword(email, password);
}
// no user
if (!user || user.getInfo().isNewUser) {
throw new Error('auth/user-not-exists');
}
// result
user.setlastLogin().save(); // update last login
const response = {
info: user.getInfo(),
idToken: user.getIdToken(),
} as Record<string, unknown>;
if (offlineAccess) {
const {refreshToken} = user.getData();
response.refreshToken = refreshToken;
}
return response;
}
// TODO: may add signInWithPopup
// TODO: may add signInWithEmailLink
/**
* Logout or delete account
*/
delete(req: {
body: {
cancelAccount?: boolean;
refreshToken?: string;
};
data: {
user: UserObject;
};
}) {
const {cancelAccount, refreshToken} = req.body;
const {user} = req.data;
if (cancelAccount && refreshToken) {
const {refreshToken: userRefreshToken} = user.getData();
if (refreshToken === userRefreshToken) {
user.delete(); // delete
}
} else {
user.setRefreshToken().save(); // new refresh token
}
}
}