me.get.mjs
1.52 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
import { e as eventHandler, g as getHeader, b as getMockUserByToken } from '../../../nitro/nitro.mjs';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'node:crypto';
import 'node:url';
import '@iconify/utils';
import 'consola';
function getTokenFromAuthorizationHeader(value) {
if (!value) {
return null;
}
const [type, token] = value.split(" ");
if ((type == null ? void 0 : type.toLowerCase()) !== "bearer" || !token) {
return null;
}
return token;
}
const me_get = eventHandler((event) => {
const authorization = getHeader(event, "authorization");
const token = getTokenFromAuthorizationHeader(authorization);
if (!token) {
return {
success: false,
state: "failed",
nextAction: "relogin",
errorCode: "TOKEN_MISSING",
message: "\u672A\u63D0\u4F9B\u6709\u6548 token\u3002"
};
}
const result = getMockUserByToken(token);
if (!result.success) {
return {
success: false,
state: "failed",
nextAction: "relogin",
errorCode: result.errorCode,
message: result.errorCode === "TOKEN_EXPIRED" ? "token \u5DF2\u8FC7\u671F\u3002" : "token \u65E0\u6548\u3002"
};
}
return {
success: true,
state: "authenticated",
nextAction: "continue",
errorCode: null,
message: "token \u6821\u9A8C\u901A\u8FC7\u3002",
user: result.user,
expiresAt: new Date(result.expiresAt).toISOString()
};
});
export { me_get as default };
//# sourceMappingURL=me.get.mjs.map