@tspscale/npm-login 1.0.0
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/dist/index.d.ts +31 -0
- package/dist/index.js +56 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +54 -0
- package/dist/index.mjs.map +1 -0
- package/dist/src/index.d.ts +31 -0
- package/dist/src/index.js +56 -0
- package/package.json +21 -0
- package/src/index.ts +75 -0
- package/tsconfig.json +14 -0
- package/tsup.config.ts +10 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface TSPUser {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string | null;
|
|
4
|
+
email: string;
|
|
5
|
+
role: string;
|
|
6
|
+
logo: string | null;
|
|
7
|
+
}
|
|
8
|
+
export interface TSPAuthResult {
|
|
9
|
+
user?: TSPUser;
|
|
10
|
+
error?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare class TSPClient {
|
|
13
|
+
private apiUrl;
|
|
14
|
+
constructor(options?: {
|
|
15
|
+
apiUrl?: string;
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* Verifies an access token and returns the user profile.
|
|
19
|
+
* @param accessToken The OAuth 2.0 Access Token provided by the frontend.
|
|
20
|
+
*/
|
|
21
|
+
verifyToken(accessToken: string): Promise<TSPAuthResult>;
|
|
22
|
+
/**
|
|
23
|
+
* Express middleware to protect routes.
|
|
24
|
+
* Requires the frontend to send the token in the Authorization header: `Bearer <token>`.
|
|
25
|
+
*/
|
|
26
|
+
middleware(): (req: any, res: any, next: any) => Promise<any>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Convenience default client instance
|
|
30
|
+
*/
|
|
31
|
+
export declare const tspAuth: TSPClient;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tspAuth = exports.TSPClient = void 0;
|
|
4
|
+
class TSPClient {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.apiUrl = options?.apiUrl || 'https://tspscale.in/api/v1';
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Verifies an access token and returns the user profile.
|
|
10
|
+
* @param accessToken The OAuth 2.0 Access Token provided by the frontend.
|
|
11
|
+
*/
|
|
12
|
+
async verifyToken(accessToken) {
|
|
13
|
+
try {
|
|
14
|
+
const response = await fetch(`${this.apiUrl}/oauth/userinfo`, {
|
|
15
|
+
method: 'GET',
|
|
16
|
+
headers: {
|
|
17
|
+
'Authorization': `Bearer ${accessToken}`,
|
|
18
|
+
'Content-Type': 'application/json'
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
const data = await response.json();
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
return { error: data.error || 'Failed to verify token' };
|
|
24
|
+
}
|
|
25
|
+
return { user: data };
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
return { error: err.message || 'Network error verifying token' };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Express middleware to protect routes.
|
|
33
|
+
* Requires the frontend to send the token in the Authorization header: `Bearer <token>`.
|
|
34
|
+
*/
|
|
35
|
+
middleware() {
|
|
36
|
+
return async (req, res, next) => {
|
|
37
|
+
const authHeader = req.headers.authorization;
|
|
38
|
+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
39
|
+
return res.status(401).json({ error: 'Missing or invalid Bearer token' });
|
|
40
|
+
}
|
|
41
|
+
const token = authHeader.split(' ')[1];
|
|
42
|
+
const result = await this.verifyToken(token);
|
|
43
|
+
if (result.error || !result.user) {
|
|
44
|
+
return res.status(401).json({ error: result.error || 'Unauthorized' });
|
|
45
|
+
}
|
|
46
|
+
// Attach user to the request object
|
|
47
|
+
req.user = result.user;
|
|
48
|
+
next();
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.TSPClient = TSPClient;
|
|
53
|
+
/**
|
|
54
|
+
* Convenience default client instance
|
|
55
|
+
*/
|
|
56
|
+
exports.tspAuth = new TSPClient();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export interface TSPUser {\n id: string;\n name: string | null;\n email: string;\n role: string;\n logo: string | null;\n}\n\nexport interface TSPAuthResult {\n user?: TSPUser;\n error?: string;\n}\n\nexport class TSPClient {\n private apiUrl: string;\n\n constructor(options?: { apiUrl?: string }) {\n this.apiUrl = options?.apiUrl || 'https://tspscale.in/api/v1';\n }\n\n /**\n * Verifies an access token and returns the user profile.\n * @param accessToken The OAuth 2.0 Access Token provided by the frontend.\n */\n async verifyToken(accessToken: string): Promise<TSPAuthResult> {\n try {\n const response = await fetch(`${this.apiUrl}/oauth/userinfo`, {\n method: 'GET',\n headers: {\n 'Authorization': `Bearer ${accessToken}`,\n 'Content-Type': 'application/json'\n }\n });\n\n const data = await response.json();\n\n if (!response.ok) {\n return { error: data.error || 'Failed to verify token' };\n }\n\n return { user: data };\n } catch (err: any) {\n return { error: err.message || 'Network error verifying token' };\n }\n }\n\n /**\n * Express middleware to protect routes.\n * Requires the frontend to send the token in the Authorization header: `Bearer <token>`.\n */\n middleware() {\n return async (req: any, res: any, next: any) => {\n const authHeader = req.headers.authorization;\n if (!authHeader || !authHeader.startsWith('Bearer ')) {\n return res.status(401).json({ error: 'Missing or invalid Bearer token' });\n }\n\n const token = authHeader.split(' ')[1];\n const result = await this.verifyToken(token);\n\n if (result.error || !result.user) {\n return res.status(401).json({ error: result.error || 'Unauthorized' });\n }\n\n // Attach user to the request object\n req.user = result.user;\n next();\n };\n }\n}\n\n/**\n * Convenience default client instance\n */\nexport const tspAuth = new TSPClient();\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAaO,IAAM,YAAN,MAAgB;AAAA,EACb;AAAA,EAER,YAAY,SAA+B;AACzC,SAAK,UAAS,mCAAS,WAAU;AAAA,EACnC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,YAAY,aAA6C;AAC7D,QAAI;AACF,YAAM,WAAW,MAAM,MAAM,GAAG,KAAK,MAAM,mBAAmB;AAAA,QAC5D,QAAQ;AAAA,QACR,SAAS;AAAA,UACP,iBAAiB,UAAU,WAAW;AAAA,UACtC,gBAAgB;AAAA,QAClB;AAAA,MACF,CAAC;AAED,YAAM,OAAO,MAAM,SAAS,KAAK;AAEjC,UAAI,CAAC,SAAS,IAAI;AAChB,eAAO,EAAE,OAAO,KAAK,SAAS,yBAAyB;AAAA,MACzD;AAEA,aAAO,EAAE,MAAM,KAAK;AAAA,IACtB,SAAS,KAAU;AACjB,aAAO,EAAE,OAAO,IAAI,WAAW,gCAAgC;AAAA,IACjE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,aAAa;AACX,WAAO,OAAO,KAAU,KAAU,SAAc;AAC9C,YAAM,aAAa,IAAI,QAAQ;AAC/B,UAAI,CAAC,cAAc,CAAC,WAAW,WAAW,SAAS,GAAG;AACpD,eAAO,IAAI,OAAO,GAAG,EAAE,KAAK,EAAE,OAAO,kCAAkC,CAAC;AAAA,MAC1E;AAEA,YAAM,QAAQ,WAAW,MAAM,GAAG,EAAE,CAAC;AACrC,YAAM,SAAS,MAAM,KAAK,YAAY,KAAK;AAE3C,UAAI,OAAO,SAAS,CAAC,OAAO,MAAM;AAChC,eAAO,IAAI,OAAO,GAAG,EAAE,KAAK,EAAE,OAAO,OAAO,SAAS,eAAe,CAAC;AAAA,MACvE;AAGA,UAAI,OAAO,OAAO;AAClB,WAAK;AAAA,IACP;AAAA,EACF;AACF;AAKO,IAAM,UAAU,IAAI,UAAU;","names":[]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var TSPClient = class {
|
|
3
|
+
apiUrl;
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.apiUrl = (options == null ? void 0 : options.apiUrl) || "https://tspscale.in/api/v1";
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Verifies an access token and returns the user profile.
|
|
9
|
+
* @param accessToken The OAuth 2.0 Access Token provided by the frontend.
|
|
10
|
+
*/
|
|
11
|
+
async verifyToken(accessToken) {
|
|
12
|
+
try {
|
|
13
|
+
const response = await fetch(`${this.apiUrl}/oauth/userinfo`, {
|
|
14
|
+
method: "GET",
|
|
15
|
+
headers: {
|
|
16
|
+
"Authorization": `Bearer ${accessToken}`,
|
|
17
|
+
"Content-Type": "application/json"
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
const data = await response.json();
|
|
21
|
+
if (!response.ok) {
|
|
22
|
+
return { error: data.error || "Failed to verify token" };
|
|
23
|
+
}
|
|
24
|
+
return { user: data };
|
|
25
|
+
} catch (err) {
|
|
26
|
+
return { error: err.message || "Network error verifying token" };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Express middleware to protect routes.
|
|
31
|
+
* Requires the frontend to send the token in the Authorization header: `Bearer <token>`.
|
|
32
|
+
*/
|
|
33
|
+
middleware() {
|
|
34
|
+
return async (req, res, next) => {
|
|
35
|
+
const authHeader = req.headers.authorization;
|
|
36
|
+
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
37
|
+
return res.status(401).json({ error: "Missing or invalid Bearer token" });
|
|
38
|
+
}
|
|
39
|
+
const token = authHeader.split(" ")[1];
|
|
40
|
+
const result = await this.verifyToken(token);
|
|
41
|
+
if (result.error || !result.user) {
|
|
42
|
+
return res.status(401).json({ error: result.error || "Unauthorized" });
|
|
43
|
+
}
|
|
44
|
+
req.user = result.user;
|
|
45
|
+
next();
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var tspAuth = new TSPClient();
|
|
50
|
+
export {
|
|
51
|
+
TSPClient,
|
|
52
|
+
tspAuth
|
|
53
|
+
};
|
|
54
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export interface TSPUser {\n id: string;\n name: string | null;\n email: string;\n role: string;\n logo: string | null;\n}\n\nexport interface TSPAuthResult {\n user?: TSPUser;\n error?: string;\n}\n\nexport class TSPClient {\n private apiUrl: string;\n\n constructor(options?: { apiUrl?: string }) {\n this.apiUrl = options?.apiUrl || 'https://tspscale.in/api/v1';\n }\n\n /**\n * Verifies an access token and returns the user profile.\n * @param accessToken The OAuth 2.0 Access Token provided by the frontend.\n */\n async verifyToken(accessToken: string): Promise<TSPAuthResult> {\n try {\n const response = await fetch(`${this.apiUrl}/oauth/userinfo`, {\n method: 'GET',\n headers: {\n 'Authorization': `Bearer ${accessToken}`,\n 'Content-Type': 'application/json'\n }\n });\n\n const data = await response.json();\n\n if (!response.ok) {\n return { error: data.error || 'Failed to verify token' };\n }\n\n return { user: data };\n } catch (err: any) {\n return { error: err.message || 'Network error verifying token' };\n }\n }\n\n /**\n * Express middleware to protect routes.\n * Requires the frontend to send the token in the Authorization header: `Bearer <token>`.\n */\n middleware() {\n return async (req: any, res: any, next: any) => {\n const authHeader = req.headers.authorization;\n if (!authHeader || !authHeader.startsWith('Bearer ')) {\n return res.status(401).json({ error: 'Missing or invalid Bearer token' });\n }\n\n const token = authHeader.split(' ')[1];\n const result = await this.verifyToken(token);\n\n if (result.error || !result.user) {\n return res.status(401).json({ error: result.error || 'Unauthorized' });\n }\n\n // Attach user to the request object\n req.user = result.user;\n next();\n };\n }\n}\n\n/**\n * Convenience default client instance\n */\nexport const tspAuth = new TSPClient();\n"],"mappings":";AAaO,IAAM,YAAN,MAAgB;AAAA,EACb;AAAA,EAER,YAAY,SAA+B;AACzC,SAAK,UAAS,mCAAS,WAAU;AAAA,EACnC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,YAAY,aAA6C;AAC7D,QAAI;AACF,YAAM,WAAW,MAAM,MAAM,GAAG,KAAK,MAAM,mBAAmB;AAAA,QAC5D,QAAQ;AAAA,QACR,SAAS;AAAA,UACP,iBAAiB,UAAU,WAAW;AAAA,UACtC,gBAAgB;AAAA,QAClB;AAAA,MACF,CAAC;AAED,YAAM,OAAO,MAAM,SAAS,KAAK;AAEjC,UAAI,CAAC,SAAS,IAAI;AAChB,eAAO,EAAE,OAAO,KAAK,SAAS,yBAAyB;AAAA,MACzD;AAEA,aAAO,EAAE,MAAM,KAAK;AAAA,IACtB,SAAS,KAAU;AACjB,aAAO,EAAE,OAAO,IAAI,WAAW,gCAAgC;AAAA,IACjE;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,aAAa;AACX,WAAO,OAAO,KAAU,KAAU,SAAc;AAC9C,YAAM,aAAa,IAAI,QAAQ;AAC/B,UAAI,CAAC,cAAc,CAAC,WAAW,WAAW,SAAS,GAAG;AACpD,eAAO,IAAI,OAAO,GAAG,EAAE,KAAK,EAAE,OAAO,kCAAkC,CAAC;AAAA,MAC1E;AAEA,YAAM,QAAQ,WAAW,MAAM,GAAG,EAAE,CAAC;AACrC,YAAM,SAAS,MAAM,KAAK,YAAY,KAAK;AAE3C,UAAI,OAAO,SAAS,CAAC,OAAO,MAAM;AAChC,eAAO,IAAI,OAAO,GAAG,EAAE,KAAK,EAAE,OAAO,OAAO,SAAS,eAAe,CAAC;AAAA,MACvE;AAGA,UAAI,OAAO,OAAO;AAClB,WAAK;AAAA,IACP;AAAA,EACF;AACF;AAKO,IAAM,UAAU,IAAI,UAAU;","names":[]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface TSPUser {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string | null;
|
|
4
|
+
email: string;
|
|
5
|
+
role: string;
|
|
6
|
+
logo: string | null;
|
|
7
|
+
}
|
|
8
|
+
export interface TSPAuthResult {
|
|
9
|
+
user?: TSPUser;
|
|
10
|
+
error?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare class TSPClient {
|
|
13
|
+
private apiUrl;
|
|
14
|
+
constructor(options?: {
|
|
15
|
+
apiUrl?: string;
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* Verifies an access token and returns the user profile.
|
|
19
|
+
* @param accessToken The OAuth 2.0 Access Token provided by the frontend.
|
|
20
|
+
*/
|
|
21
|
+
verifyToken(accessToken: string): Promise<TSPAuthResult>;
|
|
22
|
+
/**
|
|
23
|
+
* Express middleware to protect routes.
|
|
24
|
+
* Requires the frontend to send the token in the Authorization header: `Bearer <token>`.
|
|
25
|
+
*/
|
|
26
|
+
middleware(): (req: any, res: any, next: any) => Promise<any>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Convenience default client instance
|
|
30
|
+
*/
|
|
31
|
+
export declare const tspAuth: TSPClient;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tspAuth = exports.TSPClient = void 0;
|
|
4
|
+
class TSPClient {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.apiUrl = options?.apiUrl || 'https://tspscale.in/api/v1';
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Verifies an access token and returns the user profile.
|
|
10
|
+
* @param accessToken The OAuth 2.0 Access Token provided by the frontend.
|
|
11
|
+
*/
|
|
12
|
+
async verifyToken(accessToken) {
|
|
13
|
+
try {
|
|
14
|
+
const response = await fetch(`${this.apiUrl}/oauth/userinfo`, {
|
|
15
|
+
method: 'GET',
|
|
16
|
+
headers: {
|
|
17
|
+
'Authorization': `Bearer ${accessToken}`,
|
|
18
|
+
'Content-Type': 'application/json'
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
const data = await response.json();
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
return { error: data.error || 'Failed to verify token' };
|
|
24
|
+
}
|
|
25
|
+
return { user: data };
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
return { error: err.message || 'Network error verifying token' };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Express middleware to protect routes.
|
|
33
|
+
* Requires the frontend to send the token in the Authorization header: `Bearer <token>`.
|
|
34
|
+
*/
|
|
35
|
+
middleware() {
|
|
36
|
+
return async (req, res, next) => {
|
|
37
|
+
const authHeader = req.headers.authorization;
|
|
38
|
+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
39
|
+
return res.status(401).json({ error: 'Missing or invalid Bearer token' });
|
|
40
|
+
}
|
|
41
|
+
const token = authHeader.split(' ')[1];
|
|
42
|
+
const result = await this.verifyToken(token);
|
|
43
|
+
if (result.error || !result.user) {
|
|
44
|
+
return res.status(401).json({ error: result.error || 'Unauthorized' });
|
|
45
|
+
}
|
|
46
|
+
// Attach user to the request object
|
|
47
|
+
req.user = result.user;
|
|
48
|
+
next();
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.TSPClient = TSPClient;
|
|
53
|
+
/**
|
|
54
|
+
* Convenience default client instance
|
|
55
|
+
*/
|
|
56
|
+
exports.tspAuth = new TSPClient();
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tspscale/npm-login",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"dev": "tsc --watch"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"author": "",
|
|
12
|
+
"license": "ISC",
|
|
13
|
+
"description": "",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/node": "^26.1.1",
|
|
16
|
+
"tsup": "^8.5.1",
|
|
17
|
+
"typescript": "^7.0.2"
|
|
18
|
+
},
|
|
19
|
+
"module": "dist/index.mjs",
|
|
20
|
+
"types": "dist/index.d.ts"
|
|
21
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export interface TSPUser {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string | null;
|
|
4
|
+
email: string;
|
|
5
|
+
role: string;
|
|
6
|
+
logo: string | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface TSPAuthResult {
|
|
10
|
+
user?: TSPUser;
|
|
11
|
+
error?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class TSPClient {
|
|
15
|
+
private apiUrl: string;
|
|
16
|
+
|
|
17
|
+
constructor(options?: { apiUrl?: string }) {
|
|
18
|
+
this.apiUrl = options?.apiUrl || 'https://tspscale.in/api/v1';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Verifies an access token and returns the user profile.
|
|
23
|
+
* @param accessToken The OAuth 2.0 Access Token provided by the frontend.
|
|
24
|
+
*/
|
|
25
|
+
async verifyToken(accessToken: string): Promise<TSPAuthResult> {
|
|
26
|
+
try {
|
|
27
|
+
const response = await fetch(`${this.apiUrl}/oauth/userinfo`, {
|
|
28
|
+
method: 'GET',
|
|
29
|
+
headers: {
|
|
30
|
+
'Authorization': `Bearer ${accessToken}`,
|
|
31
|
+
'Content-Type': 'application/json'
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const data = await response.json();
|
|
36
|
+
|
|
37
|
+
if (!response.ok) {
|
|
38
|
+
return { error: data.error || 'Failed to verify token' };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return { user: data };
|
|
42
|
+
} catch (err: any) {
|
|
43
|
+
return { error: err.message || 'Network error verifying token' };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Express middleware to protect routes.
|
|
49
|
+
* Requires the frontend to send the token in the Authorization header: `Bearer <token>`.
|
|
50
|
+
*/
|
|
51
|
+
middleware() {
|
|
52
|
+
return async (req: any, res: any, next: any) => {
|
|
53
|
+
const authHeader = req.headers.authorization;
|
|
54
|
+
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
|
55
|
+
return res.status(401).json({ error: 'Missing or invalid Bearer token' });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const token = authHeader.split(' ')[1];
|
|
59
|
+
const result = await this.verifyToken(token);
|
|
60
|
+
|
|
61
|
+
if (result.error || !result.user) {
|
|
62
|
+
return res.status(401).json({ error: result.error || 'Unauthorized' });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Attach user to the request object
|
|
66
|
+
req.user = result.user;
|
|
67
|
+
next();
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Convenience default client instance
|
|
74
|
+
*/
|
|
75
|
+
export const tspAuth = new TSPClient();
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2021",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"rootDir": "./src",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true
|
|
12
|
+
},
|
|
13
|
+
"include": ["src/**/*"]
|
|
14
|
+
}
|
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { defineConfig } from 'tsup';
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: ['src/index.ts'],
|
|
5
|
+
format: ['cjs', 'esm'], // Build for commonJS and ESmodules
|
|
6
|
+
dts: true, // Generate declaration file (.d.ts)
|
|
7
|
+
splitting: false,
|
|
8
|
+
sourcemap: true,
|
|
9
|
+
clean: true,
|
|
10
|
+
});
|