@thestarweb/star-web-user-center-sdk 0.0.0-stage → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api.d.ts +56 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/login.interceptor.js +51 -0
- package/dist/login.interceptor.js.map +1 -0
- package/dist/web.js +15 -0
- package/dist/web.js.map +1 -0
- package/package.json +22 -5
- package/src/index.ts +7 -0
- package/src/login.interceptor.ts +76 -0
- package/src/web.ts +19 -0
- package/tsconfig.json +20 -0
- package/README.md +0 -3
package/api.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type * as t from "@thestarweb/star-framework-app-web";
|
|
2
|
+
declare module "@thestarweb/star-framework-app-web"{
|
|
3
|
+
interface AllApi{
|
|
4
|
+
"@thestarweb/star-web-user-center-server":{
|
|
5
|
+
GET:{
|
|
6
|
+
"/":{
|
|
7
|
+
prop:{
|
|
8
|
+
},
|
|
9
|
+
return:any
|
|
10
|
+
},
|
|
11
|
+
"/login":{
|
|
12
|
+
prop:{
|
|
13
|
+
},
|
|
14
|
+
return:any
|
|
15
|
+
},
|
|
16
|
+
"/token-message":{
|
|
17
|
+
prop:{
|
|
18
|
+
header?:{"referer": string,},
|
|
19
|
+
},
|
|
20
|
+
return:any
|
|
21
|
+
},
|
|
22
|
+
"/home/":{
|
|
23
|
+
prop:{
|
|
24
|
+
},
|
|
25
|
+
return:any
|
|
26
|
+
},
|
|
27
|
+
"/home/safe":{
|
|
28
|
+
prop:{
|
|
29
|
+
},
|
|
30
|
+
return:any
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
POST:{
|
|
34
|
+
"/login":{
|
|
35
|
+
prop:{
|
|
36
|
+
body:{"mode": 1,"username": string,"password": string,},
|
|
37
|
+
header?:{"user-agent": string,},
|
|
38
|
+
},
|
|
39
|
+
return:any
|
|
40
|
+
},
|
|
41
|
+
"/refreshToken":{
|
|
42
|
+
prop:{
|
|
43
|
+
body:string,
|
|
44
|
+
header?:{"user-agent": string,},
|
|
45
|
+
},
|
|
46
|
+
return:any
|
|
47
|
+
},
|
|
48
|
+
"/check":{
|
|
49
|
+
prop:{
|
|
50
|
+
},
|
|
51
|
+
return:{"username": string,"uid": string,}
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EACN,sBAAsB,EACtB,YAAY,EACZ,GAAG,EACH,IAAI,GACJ,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Context, getSfRequest, LocationTo, } from "@thestarweb/star-framework-app-web";
|
|
2
|
+
import { getMetadata, initMetadata } from "@thestarweb/star-framework-utils";
|
|
3
|
+
import { parseCookie, stringifyCookie, } from "cookie";
|
|
4
|
+
const metaKey = Symbol();
|
|
5
|
+
export function RequireLogin() {
|
|
6
|
+
return function (target, key) {
|
|
7
|
+
initMetadata(target, metaKey, key, []);
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
// @side-only server
|
|
11
|
+
export function createLoginInterceptor(baseUrl) {
|
|
12
|
+
return async (prop, next) => {
|
|
13
|
+
const cookie = prop.header.cookie
|
|
14
|
+
? parseCookie(prop.header.cookie)
|
|
15
|
+
: Object.create(null);
|
|
16
|
+
if (cookie.token) {
|
|
17
|
+
const user = await getSfRequest().post("@thestarweb/star-web-user-center-server:/check", {
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
header: {
|
|
20
|
+
cookie: stringifyCookie(cookie.token),
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
if (user) {
|
|
24
|
+
prop.context.user = user;
|
|
25
|
+
prop.context.uid = user.uid;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
let shouldLogin = false;
|
|
29
|
+
prop.route.allRoute.forEach((r) => {
|
|
30
|
+
const classMeta = getMetadata(r.obj, metaKey, undefined, undefined);
|
|
31
|
+
if (classMeta) {
|
|
32
|
+
shouldLogin = true;
|
|
33
|
+
}
|
|
34
|
+
const keyMeta = getMetadata(r.obj, metaKey, r.propertyKey, undefined);
|
|
35
|
+
if (keyMeta) {
|
|
36
|
+
shouldLogin = true;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
if (shouldLogin && !prop.context.user) {
|
|
40
|
+
return new LocationTo(new URL(`login?from=${encodeURIComponent(prop.url.pathname + prop.url.search + prop.url.hash)}`, baseUrl).toString());
|
|
41
|
+
}
|
|
42
|
+
return next(prop);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export function Uid() {
|
|
46
|
+
return Context("uid");
|
|
47
|
+
}
|
|
48
|
+
export function User() {
|
|
49
|
+
return Context("user");
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=login.interceptor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"login.interceptor.js","sourceRoot":"","sources":["../src/login.interceptor.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,OAAO,EACP,YAAY,EACZ,UAAU,GAEV,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAC7E,OAAO,EAEN,WAAW,EAEX,eAAe,GAEf,MAAM,QAAQ,CAAC;AAEhB,MAAM,OAAO,GAAG,MAAM,EAAE,CAAC;AACzB,MAAM,UAAU,YAAY;IAG3B,OAAO,UAAU,MAAc,EAAE,GAAiB;QACjD,YAAY,CAAC,MAAM,EAAE,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC;IACxC,CAAC,CAAC;AACH,CAAC;AAKD,oBAAoB;AACpB,MAAM,UAAU,sBAAsB,CAAC,OAAe;IACrD,OAAO,KAAK,EAAE,IAAI,E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CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,EACvF,OAAO,CACP,CAAC,QAAQ,EAAE,CACZ,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC;IACnB,CAAC,CAAC;AACH,CAAC;AACD,MAAM,UAAU,GAAG;IAClB,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC;AACD,MAAM,UAAU,IAAI;IACnB,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;AACxB,CAAC"}
|
package/dist/web.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import Cookies from "js-cookie";
|
|
2
|
+
export function init(baseUrl, onChange) {
|
|
3
|
+
const iframe = document.createElement("iframe");
|
|
4
|
+
iframe.style.display = "none";
|
|
5
|
+
const url = new URL("./token-message", baseUrl);
|
|
6
|
+
iframe.src = url.toString();
|
|
7
|
+
window.addEventListener("message", (ev) => {
|
|
8
|
+
if (ev.origin === url.origin) {
|
|
9
|
+
Cookies.set("token", ev.data.authToken);
|
|
10
|
+
onChange(ev.data);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
document.body.append(iframe);
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=web.js.map
|
package/dist/web.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web.js","sourceRoot":"","sources":["../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,WAAW,CAAC;AAGhC,MAAM,UAAU,IAAI,CACnB,OAAe,EACf,QAAiD;IAEjD,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAChD,MAAM,CAAC,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC;IAC9B,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,iBAAiB,EAAE,OAAO,CAAC,CAAC;IAChD,MAAM,CAAC,GAAG,GAAG,GAAG,CAAC,QAAQ,EAAE,CAAC;IAC5B,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,CAAC,EAAE,EAAE,EAAE;QACzC,IAAI,EAAE,CAAC,MAAM,KAAK,GAAG,CAAC,MAAM,EAAE,CAAC;YAC9B,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,CAAC,SAAU,CAAC,CAAC;YACzC,QAAQ,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;QACnB,CAAC;IACF,CAAC,CAAC,CAAC;IACH,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AAC9B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@thestarweb/star-web-user-center-sdk",
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
|
+
"description": "StarWeb user center sdk",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "src/index.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"dev": "tsc -w",
|
|
10
|
+
"build": "tsc"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@thestarweb/star-framework-app-web": "^1.2.0",
|
|
14
|
+
"cookie": "^2.0.2",
|
|
15
|
+
"js-cookie": "^3.0.8"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^26.0.1",
|
|
20
|
+
"typescript": "^7.0.2"
|
|
21
|
+
},
|
|
22
|
+
"license": "ISC"
|
|
23
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Context,
|
|
3
|
+
getSfRequest,
|
|
4
|
+
LocationTo,
|
|
5
|
+
type Interceptor,
|
|
6
|
+
} from "@thestarweb/star-framework-app-web";
|
|
7
|
+
import { getMetadata, initMetadata } from "@thestarweb/star-framework-utils";
|
|
8
|
+
import {
|
|
9
|
+
Cookies,
|
|
10
|
+
parseCookie,
|
|
11
|
+
SetCookie,
|
|
12
|
+
stringifyCookie,
|
|
13
|
+
stringifySetCookie,
|
|
14
|
+
} from "cookie";
|
|
15
|
+
|
|
16
|
+
const metaKey = Symbol();
|
|
17
|
+
export function RequireLogin(): ClassDecorator &
|
|
18
|
+
MethodDecorator &
|
|
19
|
+
PropertyDecorator {
|
|
20
|
+
return function (target: object, key?: PropertyKey) {
|
|
21
|
+
initMetadata(target, metaKey, key, []);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export interface LoginInfo {
|
|
25
|
+
username: string;
|
|
26
|
+
uid: string;
|
|
27
|
+
}
|
|
28
|
+
// @side-only server
|
|
29
|
+
export function createLoginInterceptor(baseUrl: string): Interceptor {
|
|
30
|
+
return async (prop, next) => {
|
|
31
|
+
const cookie = prop.header.cookie
|
|
32
|
+
? parseCookie(prop.header.cookie)
|
|
33
|
+
: Object.create(null);
|
|
34
|
+
if (cookie.token) {
|
|
35
|
+
const user = await getSfRequest().post(
|
|
36
|
+
"@thestarweb/star-web-user-center-server:/check",
|
|
37
|
+
{
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
header: {
|
|
40
|
+
cookie: stringifyCookie(cookie.token),
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
if (user) {
|
|
45
|
+
prop.context.user = user;
|
|
46
|
+
prop.context.uid = user.uid;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
let shouldLogin = false;
|
|
50
|
+
prop.route.allRoute.forEach((r) => {
|
|
51
|
+
const classMeta = getMetadata(r.obj, metaKey, undefined, undefined);
|
|
52
|
+
if (classMeta) {
|
|
53
|
+
shouldLogin = true;
|
|
54
|
+
}
|
|
55
|
+
const keyMeta = getMetadata(r.obj, metaKey, r.propertyKey, undefined);
|
|
56
|
+
if (keyMeta) {
|
|
57
|
+
shouldLogin = true;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
if (shouldLogin && !prop.context.user) {
|
|
61
|
+
return new LocationTo(
|
|
62
|
+
new URL(
|
|
63
|
+
`login?from=${encodeURIComponent(prop.url.pathname + prop.url.search + prop.url.hash)}`,
|
|
64
|
+
baseUrl,
|
|
65
|
+
).toString(),
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return next(prop);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export function Uid() {
|
|
72
|
+
return Context("uid");
|
|
73
|
+
}
|
|
74
|
+
export function User() {
|
|
75
|
+
return Context("user");
|
|
76
|
+
}
|
package/src/web.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import Cookies from "js-cookie";
|
|
2
|
+
import { LoginInfo } from "./login.interceptor";
|
|
3
|
+
|
|
4
|
+
export function init(
|
|
5
|
+
baseUrl: string,
|
|
6
|
+
onChange: (user?: LoginInfo, ak?: string) => void,
|
|
7
|
+
) {
|
|
8
|
+
const iframe = document.createElement("iframe");
|
|
9
|
+
iframe.style.display = "none";
|
|
10
|
+
const url = new URL("./token-message", baseUrl);
|
|
11
|
+
iframe.src = url.toString();
|
|
12
|
+
window.addEventListener("message", (ev) => {
|
|
13
|
+
if (ev.origin === url.origin) {
|
|
14
|
+
Cookies.set("token", ev.data.authToken!);
|
|
15
|
+
onChange(ev.data);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
document.body.append(iframe);
|
|
19
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2022",
|
|
4
|
+
"module": "es2022",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"declaration": false,
|
|
7
|
+
"sourceMap": true,
|
|
8
|
+
"outDir": "./dist",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"experimentalDecorators": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"forceConsistentCasingInFileNames": true,
|
|
14
|
+
"resolveJsonModule": true,
|
|
15
|
+
"importHelpers": true,
|
|
16
|
+
"types": ["node"],
|
|
17
|
+
"rootDir": "./src"
|
|
18
|
+
},
|
|
19
|
+
"reflection": true
|
|
20
|
+
}
|
package/README.md
DELETED