@solvapay/auth 1.0.8-preview.7 → 1.0.8-preview.9
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/package.json +1 -1
- package/dist/adapter-4HTpALyc.d.cts +0 -32
- package/dist/adapter-4HTpALyc.d.ts +0 -32
- package/dist/adapter-Bl9fq5jF.d.cts +0 -64
- package/dist/adapter-Bl9fq5jF.d.ts +0 -64
- package/dist/chunk-2P77RLPB.js +0 -77
- package/dist/chunk-AIXM4F5E.js +0 -26
- package/dist/chunk-B4VVTQWS.js +0 -77
- package/dist/chunk-T3UACNXQ.js +0 -74
- package/dist/src/adapter.d.ts +0 -63
- package/dist/src/adapter.d.ts.map +0 -1
- package/dist/src/adapter.js +0 -8
- package/dist/src/adapter.js.map +0 -1
- package/dist/src/index.d.ts +0 -11
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -14
- package/dist/src/index.js.map +0 -1
- package/dist/src/mock.d.ts +0 -30
- package/dist/src/mock.d.ts.map +0 -1
- package/dist/src/mock.js +0 -41
- package/dist/src/mock.js.map +0 -1
- package/dist/src/next-utils.d.ts +0 -159
- package/dist/src/next-utils.d.ts.map +0 -1
- package/dist/src/next-utils.js +0 -222
- package/dist/src/next-utils.js.map +0 -1
- package/dist/src/solvapay.d.ts +0 -65
- package/dist/src/solvapay.d.ts.map +0 -1
- package/dist/src/solvapay.js +0 -143
- package/dist/src/solvapay.js.map +0 -1
- package/dist/src/supabase.d.ts +0 -39
- package/dist/src/supabase.d.ts.map +0 -1
- package/dist/src/supabase.js +0 -62
- package/dist/src/supabase.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
package/package.json
CHANGED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth Adapter Interface
|
|
3
|
-
*
|
|
4
|
-
* Defines the contract for authentication adapters that extract user IDs from requests.
|
|
5
|
-
* Implementations should never throw - return null if authentication fails or is missing.
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* Request-like object that has headers
|
|
9
|
-
*/
|
|
10
|
-
interface RequestLike {
|
|
11
|
-
headers: Headers | {
|
|
12
|
-
get(name: string): string | null;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Auth adapter interface for extracting user IDs from requests
|
|
17
|
-
*/
|
|
18
|
-
interface AuthAdapter {
|
|
19
|
-
/**
|
|
20
|
-
* Extract the authenticated user ID from a request
|
|
21
|
-
*
|
|
22
|
-
* @param req - Request object or object with headers
|
|
23
|
-
* @returns The user ID string if authenticated, null otherwise
|
|
24
|
-
*
|
|
25
|
-
* @remarks
|
|
26
|
-
* This method should never throw. If authentication fails or is missing,
|
|
27
|
-
* return null and let the caller decide how to handle unauthenticated requests.
|
|
28
|
-
*/
|
|
29
|
-
getUserIdFromRequest(req: Request | RequestLike): Promise<string | null>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type { AuthAdapter as A, RequestLike as R };
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth Adapter Interface
|
|
3
|
-
*
|
|
4
|
-
* Defines the contract for authentication adapters that extract user IDs from requests.
|
|
5
|
-
* Implementations should never throw - return null if authentication fails or is missing.
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* Request-like object that has headers
|
|
9
|
-
*/
|
|
10
|
-
interface RequestLike {
|
|
11
|
-
headers: Headers | {
|
|
12
|
-
get(name: string): string | null;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Auth adapter interface for extracting user IDs from requests
|
|
17
|
-
*/
|
|
18
|
-
interface AuthAdapter {
|
|
19
|
-
/**
|
|
20
|
-
* Extract the authenticated user ID from a request
|
|
21
|
-
*
|
|
22
|
-
* @param req - Request object or object with headers
|
|
23
|
-
* @returns The user ID string if authenticated, null otherwise
|
|
24
|
-
*
|
|
25
|
-
* @remarks
|
|
26
|
-
* This method should never throw. If authentication fails or is missing,
|
|
27
|
-
* return null and let the caller decide how to handle unauthenticated requests.
|
|
28
|
-
*/
|
|
29
|
-
getUserIdFromRequest(req: Request | RequestLike): Promise<string | null>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export type { AuthAdapter as A, RequestLike as R };
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth Adapter Interface
|
|
3
|
-
*
|
|
4
|
-
* Defines the contract for authentication adapters that extract user IDs from requests.
|
|
5
|
-
* Implementations should never throw - return null if authentication fails or is missing.
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* Request-like object that has headers
|
|
9
|
-
*/
|
|
10
|
-
interface RequestLike {
|
|
11
|
-
headers: Headers | {
|
|
12
|
-
get(name: string): string | null;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Authentication adapter interface for extracting user IDs from requests.
|
|
17
|
-
*
|
|
18
|
-
* This interface defines the contract for authentication adapters that can
|
|
19
|
-
* extract user IDs from various authentication systems (Supabase, custom JWT,
|
|
20
|
-
* session-based auth, etc.). Adapters should never throw - return null if
|
|
21
|
-
* authentication fails or is missing.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```typescript
|
|
25
|
-
* import type { AuthAdapter } from '@solvapay/auth';
|
|
26
|
-
*
|
|
27
|
-
* // Custom adapter implementation
|
|
28
|
-
* const myAdapter: AuthAdapter = {
|
|
29
|
-
* async getUserIdFromRequest(req) {
|
|
30
|
-
* // Extract user ID from request (JWT, session, etc.)
|
|
31
|
-
* const token = req.headers.get('authorization')?.replace('Bearer ', '');
|
|
32
|
-
* if (!token) return null;
|
|
33
|
-
*
|
|
34
|
-
* // Validate and extract user ID
|
|
35
|
-
* const payload = await verifyToken(token);
|
|
36
|
-
* return payload.userId || null;
|
|
37
|
-
* }
|
|
38
|
-
* };
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* @see {@link SupabaseAuthAdapter} for Supabase implementation
|
|
42
|
-
* @see {@link MockAuthAdapter} for testing implementation
|
|
43
|
-
* @since 1.0.0
|
|
44
|
-
*/
|
|
45
|
-
interface AuthAdapter {
|
|
46
|
-
/**
|
|
47
|
-
* Extract the authenticated user ID from a request.
|
|
48
|
-
*
|
|
49
|
-
* This method should:
|
|
50
|
-
* - Never throw exceptions (return null on failure)
|
|
51
|
-
* - Handle missing/invalid authentication gracefully
|
|
52
|
-
* - Work with both Request objects and objects with headers
|
|
53
|
-
*
|
|
54
|
-
* @param req - Request object or object with headers
|
|
55
|
-
* @returns The user ID string if authenticated, null otherwise
|
|
56
|
-
*
|
|
57
|
-
* @remarks
|
|
58
|
-
* This method should never throw. If authentication fails or is missing,
|
|
59
|
-
* return null and let the caller decide how to handle unauthenticated requests.
|
|
60
|
-
*/
|
|
61
|
-
getUserIdFromRequest(req: Request | RequestLike): Promise<string | null>;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export type { AuthAdapter as A, RequestLike as R };
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth Adapter Interface
|
|
3
|
-
*
|
|
4
|
-
* Defines the contract for authentication adapters that extract user IDs from requests.
|
|
5
|
-
* Implementations should never throw - return null if authentication fails or is missing.
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* Request-like object that has headers
|
|
9
|
-
*/
|
|
10
|
-
interface RequestLike {
|
|
11
|
-
headers: Headers | {
|
|
12
|
-
get(name: string): string | null;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Authentication adapter interface for extracting user IDs from requests.
|
|
17
|
-
*
|
|
18
|
-
* This interface defines the contract for authentication adapters that can
|
|
19
|
-
* extract user IDs from various authentication systems (Supabase, custom JWT,
|
|
20
|
-
* session-based auth, etc.). Adapters should never throw - return null if
|
|
21
|
-
* authentication fails or is missing.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```typescript
|
|
25
|
-
* import type { AuthAdapter } from '@solvapay/auth';
|
|
26
|
-
*
|
|
27
|
-
* // Custom adapter implementation
|
|
28
|
-
* const myAdapter: AuthAdapter = {
|
|
29
|
-
* async getUserIdFromRequest(req) {
|
|
30
|
-
* // Extract user ID from request (JWT, session, etc.)
|
|
31
|
-
* const token = req.headers.get('authorization')?.replace('Bearer ', '');
|
|
32
|
-
* if (!token) return null;
|
|
33
|
-
*
|
|
34
|
-
* // Validate and extract user ID
|
|
35
|
-
* const payload = await verifyToken(token);
|
|
36
|
-
* return payload.userId || null;
|
|
37
|
-
* }
|
|
38
|
-
* };
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* @see {@link SupabaseAuthAdapter} for Supabase implementation
|
|
42
|
-
* @see {@link MockAuthAdapter} for testing implementation
|
|
43
|
-
* @since 1.0.0
|
|
44
|
-
*/
|
|
45
|
-
interface AuthAdapter {
|
|
46
|
-
/**
|
|
47
|
-
* Extract the authenticated user ID from a request.
|
|
48
|
-
*
|
|
49
|
-
* This method should:
|
|
50
|
-
* - Never throw exceptions (return null on failure)
|
|
51
|
-
* - Handle missing/invalid authentication gracefully
|
|
52
|
-
* - Work with both Request objects and objects with headers
|
|
53
|
-
*
|
|
54
|
-
* @param req - Request object or object with headers
|
|
55
|
-
* @returns The user ID string if authenticated, null otherwise
|
|
56
|
-
*
|
|
57
|
-
* @remarks
|
|
58
|
-
* This method should never throw. If authentication fails or is missing,
|
|
59
|
-
* return null and let the caller decide how to handle unauthenticated requests.
|
|
60
|
-
*/
|
|
61
|
-
getUserIdFromRequest(req: Request | RequestLike): Promise<string | null>;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export type { AuthAdapter as A, RequestLike as R };
|
package/dist/chunk-2P77RLPB.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
// src/next-utils.ts
|
|
2
|
-
function getUserIdFromRequest(request, options) {
|
|
3
|
-
const headerName = options?.headerName || "x-user-id";
|
|
4
|
-
return request.headers.get(headerName);
|
|
5
|
-
}
|
|
6
|
-
async function getUserEmailFromRequest(request, options) {
|
|
7
|
-
const authHeader = request.headers.get("authorization");
|
|
8
|
-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
const token = authHeader.slice(7);
|
|
12
|
-
if (!token) {
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
try {
|
|
16
|
-
const { jwtVerify } = await import("jose");
|
|
17
|
-
const jwtSecret = options?.jwtSecret || process.env.SUPABASE_JWT_SECRET;
|
|
18
|
-
if (!jwtSecret) {
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
const secret = new TextEncoder().encode(jwtSecret);
|
|
22
|
-
const { payload } = await jwtVerify(token, secret, {
|
|
23
|
-
algorithms: ["HS256"]
|
|
24
|
-
});
|
|
25
|
-
return payload.email ? String(payload.email) : null;
|
|
26
|
-
} catch {
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
async function getUserNameFromRequest(request, options) {
|
|
31
|
-
const authHeader = request.headers.get("authorization");
|
|
32
|
-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
const token = authHeader.slice(7);
|
|
36
|
-
if (!token) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
try {
|
|
40
|
-
const { jwtVerify } = await import("jose");
|
|
41
|
-
const jwtSecret = options?.jwtSecret || process.env.SUPABASE_JWT_SECRET;
|
|
42
|
-
if (!jwtSecret) {
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
const secret = new TextEncoder().encode(jwtSecret);
|
|
46
|
-
const { payload } = await jwtVerify(token, secret, {
|
|
47
|
-
algorithms: ["HS256"]
|
|
48
|
-
});
|
|
49
|
-
const name = payload.user_metadata?.full_name || payload.user_metadata?.name || payload.name || null;
|
|
50
|
-
return name ? String(name) : null;
|
|
51
|
-
} catch {
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
function requireUserId(request, options) {
|
|
56
|
-
const headerName = options?.headerName || "x-user-id";
|
|
57
|
-
const errorMessage = options?.errorMessage || "Unauthorized";
|
|
58
|
-
const errorDetails = options?.errorDetails || "User ID not found. Ensure middleware is configured.";
|
|
59
|
-
const userId = request.headers.get(headerName);
|
|
60
|
-
if (!userId) {
|
|
61
|
-
return new Response(
|
|
62
|
-
JSON.stringify({ error: errorMessage, details: errorDetails }),
|
|
63
|
-
{
|
|
64
|
-
status: 401,
|
|
65
|
-
headers: { "Content-Type": "application/json" }
|
|
66
|
-
}
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
return userId;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export {
|
|
73
|
-
getUserIdFromRequest,
|
|
74
|
-
getUserEmailFromRequest,
|
|
75
|
-
getUserNameFromRequest,
|
|
76
|
-
requireUserId
|
|
77
|
-
};
|
package/dist/chunk-AIXM4F5E.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
// src/next-utils.ts
|
|
2
|
-
function getUserIdFromRequest(request, options) {
|
|
3
|
-
const headerName = options?.headerName || "x-user-id";
|
|
4
|
-
return request.headers.get(headerName);
|
|
5
|
-
}
|
|
6
|
-
function requireUserId(request, options) {
|
|
7
|
-
const headerName = options?.headerName || "x-user-id";
|
|
8
|
-
const errorMessage = options?.errorMessage || "Unauthorized";
|
|
9
|
-
const errorDetails = options?.errorDetails || "User ID not found. Ensure middleware is configured.";
|
|
10
|
-
const userId = request.headers.get(headerName);
|
|
11
|
-
if (!userId) {
|
|
12
|
-
return new Response(
|
|
13
|
-
JSON.stringify({ error: errorMessage, details: errorDetails }),
|
|
14
|
-
{
|
|
15
|
-
status: 401,
|
|
16
|
-
headers: { "Content-Type": "application/json" }
|
|
17
|
-
}
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
return userId;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export {
|
|
24
|
-
getUserIdFromRequest,
|
|
25
|
-
requireUserId
|
|
26
|
-
};
|
package/dist/chunk-B4VVTQWS.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
// src/next-utils.ts
|
|
2
|
-
function getUserIdFromRequest(request, options) {
|
|
3
|
-
const headerName = options?.headerName || "x-user-id";
|
|
4
|
-
return request.headers.get(headerName);
|
|
5
|
-
}
|
|
6
|
-
async function getUserEmailFromRequest(request, options) {
|
|
7
|
-
const authHeader = request.headers.get("authorization");
|
|
8
|
-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
const token = authHeader.slice(7);
|
|
12
|
-
if (!token) {
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
try {
|
|
16
|
-
const { jwtVerify } = await import("jose");
|
|
17
|
-
const jwtSecret = options?.jwtSecret || process.env.SUPABASE_JWT_SECRET;
|
|
18
|
-
if (!jwtSecret) {
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
const secret = new TextEncoder().encode(jwtSecret);
|
|
22
|
-
const { payload } = await jwtVerify(token, secret, {
|
|
23
|
-
algorithms: ["HS256"]
|
|
24
|
-
});
|
|
25
|
-
return payload.email ? String(payload.email) : null;
|
|
26
|
-
} catch {
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
async function getUserNameFromRequest(request, options) {
|
|
31
|
-
const authHeader = request.headers.get("authorization");
|
|
32
|
-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
const token = authHeader.slice(7);
|
|
36
|
-
if (!token) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
try {
|
|
40
|
-
const { jwtVerify } = await import("jose");
|
|
41
|
-
const jwtSecret = options?.jwtSecret || process.env.SUPABASE_JWT_SECRET;
|
|
42
|
-
if (!jwtSecret) {
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
const secret = new TextEncoder().encode(jwtSecret);
|
|
46
|
-
const { payload } = await jwtVerify(token, secret, {
|
|
47
|
-
algorithms: ["HS256"]
|
|
48
|
-
});
|
|
49
|
-
const name = payload.user_metadata?.name || payload.name || null;
|
|
50
|
-
return name ? String(name) : null;
|
|
51
|
-
} catch {
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
function requireUserId(request, options) {
|
|
56
|
-
const headerName = options?.headerName || "x-user-id";
|
|
57
|
-
const errorMessage = options?.errorMessage || "Unauthorized";
|
|
58
|
-
const errorDetails = options?.errorDetails || "User ID not found. Ensure middleware is configured.";
|
|
59
|
-
const userId = request.headers.get(headerName);
|
|
60
|
-
if (!userId) {
|
|
61
|
-
return new Response(
|
|
62
|
-
JSON.stringify({ error: errorMessage, details: errorDetails }),
|
|
63
|
-
{
|
|
64
|
-
status: 401,
|
|
65
|
-
headers: { "Content-Type": "application/json" }
|
|
66
|
-
}
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
return userId;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export {
|
|
73
|
-
getUserIdFromRequest,
|
|
74
|
-
getUserEmailFromRequest,
|
|
75
|
-
getUserNameFromRequest,
|
|
76
|
-
requireUserId
|
|
77
|
-
};
|
package/dist/chunk-T3UACNXQ.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
// src/next-utils.ts
|
|
2
|
-
function getUserIdFromRequest(request, options) {
|
|
3
|
-
const headerName = options?.headerName || "x-user-id";
|
|
4
|
-
return request.headers.get(headerName);
|
|
5
|
-
}
|
|
6
|
-
async function getUserEmailFromRequest(request, options) {
|
|
7
|
-
const authHeader = request.headers.get("authorization");
|
|
8
|
-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
9
|
-
return null;
|
|
10
|
-
}
|
|
11
|
-
const token = authHeader.slice(7);
|
|
12
|
-
if (!token) {
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
try {
|
|
16
|
-
const { jwtVerify } = await import("jose");
|
|
17
|
-
const jwtSecret = options?.jwtSecret || process.env.SUPABASE_JWT_SECRET;
|
|
18
|
-
if (!jwtSecret) {
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
|
-
const secret = new TextEncoder().encode(jwtSecret);
|
|
22
|
-
const { payload } = await jwtVerify(token, secret, {
|
|
23
|
-
algorithms: ["HS256"]
|
|
24
|
-
});
|
|
25
|
-
return payload.email ? String(payload.email) : null;
|
|
26
|
-
} catch {
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
async function getUserNameFromRequest(request, options) {
|
|
31
|
-
const authHeader = request.headers.get("authorization");
|
|
32
|
-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
const token = authHeader.slice(7);
|
|
36
|
-
if (!token) {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
try {
|
|
40
|
-
const { jwtVerify } = await import("jose");
|
|
41
|
-
const jwtSecret = options?.jwtSecret || process.env.SUPABASE_JWT_SECRET;
|
|
42
|
-
if (!jwtSecret) {
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
const secret = new TextEncoder().encode(jwtSecret);
|
|
46
|
-
const { payload } = await jwtVerify(token, secret, {
|
|
47
|
-
algorithms: ["HS256"]
|
|
48
|
-
});
|
|
49
|
-
const name = payload.user_metadata?.full_name || payload.user_metadata?.name || payload.name || null;
|
|
50
|
-
return name ? String(name) : null;
|
|
51
|
-
} catch {
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
function requireUserId(request, options) {
|
|
56
|
-
const headerName = options?.headerName || "x-user-id";
|
|
57
|
-
const errorMessage = options?.errorMessage || "Unauthorized";
|
|
58
|
-
const errorDetails = options?.errorDetails || "User ID not found. Ensure middleware is configured.";
|
|
59
|
-
const userId = request.headers.get(headerName);
|
|
60
|
-
if (!userId) {
|
|
61
|
-
return new Response(JSON.stringify({ error: errorMessage, details: errorDetails }), {
|
|
62
|
-
status: 401,
|
|
63
|
-
headers: { "Content-Type": "application/json" }
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
return userId;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export {
|
|
70
|
-
getUserIdFromRequest,
|
|
71
|
-
getUserEmailFromRequest,
|
|
72
|
-
getUserNameFromRequest,
|
|
73
|
-
requireUserId
|
|
74
|
-
};
|
package/dist/src/adapter.d.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth Adapter Interface
|
|
3
|
-
*
|
|
4
|
-
* Defines the contract for authentication adapters that extract user IDs from requests.
|
|
5
|
-
* Implementations should never throw - return null if authentication fails or is missing.
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* Request-like object that has headers
|
|
9
|
-
*/
|
|
10
|
-
export interface RequestLike {
|
|
11
|
-
headers: Headers | {
|
|
12
|
-
get(name: string): string | null;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Authentication adapter interface for extracting user IDs from requests.
|
|
17
|
-
*
|
|
18
|
-
* This interface defines the contract for authentication adapters that can
|
|
19
|
-
* extract user IDs from various authentication systems (Supabase, custom JWT,
|
|
20
|
-
* session-based auth, etc.). Adapters should never throw - return null if
|
|
21
|
-
* authentication fails or is missing.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```typescript
|
|
25
|
-
* import type { AuthAdapter } from '@solvapay/auth';
|
|
26
|
-
*
|
|
27
|
-
* // Custom adapter implementation
|
|
28
|
-
* const myAdapter: AuthAdapter = {
|
|
29
|
-
* async getUserIdFromRequest(req) {
|
|
30
|
-
* // Extract user ID from request (JWT, session, etc.)
|
|
31
|
-
* const token = req.headers.get('authorization')?.replace('Bearer ', '');
|
|
32
|
-
* if (!token) return null;
|
|
33
|
-
*
|
|
34
|
-
* // Validate and extract user ID
|
|
35
|
-
* const payload = await verifyToken(token);
|
|
36
|
-
* return payload.userId || null;
|
|
37
|
-
* }
|
|
38
|
-
* };
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* @see SupabaseAuthAdapter (from `@solvapay/auth/supabase`) for Supabase implementation
|
|
42
|
-
* @see {@link MockAuthAdapter} for testing implementation
|
|
43
|
-
* @since 1.0.0
|
|
44
|
-
*/
|
|
45
|
-
export interface AuthAdapter {
|
|
46
|
-
/**
|
|
47
|
-
* Extract the authenticated user ID from a request.
|
|
48
|
-
*
|
|
49
|
-
* This method should:
|
|
50
|
-
* - Never throw exceptions (return null on failure)
|
|
51
|
-
* - Handle missing/invalid authentication gracefully
|
|
52
|
-
* - Work with both Request objects and objects with headers
|
|
53
|
-
*
|
|
54
|
-
* @param req - Request object or object with headers
|
|
55
|
-
* @returns The user ID string if authenticated, null otherwise
|
|
56
|
-
*
|
|
57
|
-
* @remarks
|
|
58
|
-
* This method should never throw. If authentication fails or is missing,
|
|
59
|
-
* return null and let the caller decide how to handle unauthenticated requests.
|
|
60
|
-
*/
|
|
61
|
-
getUserIdFromRequest(req: Request | RequestLike): Promise<string | null>;
|
|
62
|
-
}
|
|
63
|
-
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/adapter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,OAAO,EACH,OAAO,GACP;QACE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;KACjC,CAAA;CACN;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,WAAW,WAAW;IAC1B;;;;;;;;;;;;;;OAcG;IACH,oBAAoB,CAAC,GAAG,EAAE,OAAO,GAAG,WAAW,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;CACzE"}
|
package/dist/src/adapter.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Auth Adapter Interface
|
|
3
|
-
*
|
|
4
|
-
* Defines the contract for authentication adapters that extract user IDs from requests.
|
|
5
|
-
* Implementations should never throw - return null if authentication fails or is missing.
|
|
6
|
-
*/
|
|
7
|
-
export {};
|
|
8
|
-
//# sourceMappingURL=adapter.js.map
|
package/dist/src/adapter.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../../src/adapter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
package/dist/src/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @solvapay/auth
|
|
3
|
-
*
|
|
4
|
-
* Authentication adapters for extracting user IDs from requests.
|
|
5
|
-
* Provides adapters for Supabase and mock/testing scenarios.
|
|
6
|
-
*/
|
|
7
|
-
export type { AuthAdapter, RequestLike } from './adapter';
|
|
8
|
-
export { MockAuthAdapter } from './mock';
|
|
9
|
-
export type { MockAuthAdapter as MockAuthAdapterType } from './mock';
|
|
10
|
-
export { getUserIdFromRequest, requireUserId, getUserEmailFromRequest, getUserNameFromRequest, } from './next-utils';
|
|
11
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAGzD,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAA;AACxC,YAAY,EAAE,eAAe,IAAI,mBAAmB,EAAE,MAAM,QAAQ,CAAA;AAGpE,OAAO,EACL,oBAAoB,EACpB,aAAa,EACb,uBAAuB,EACvB,sBAAsB,GACvB,MAAM,cAAc,CAAA"}
|
package/dist/src/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @solvapay/auth
|
|
3
|
-
*
|
|
4
|
-
* Authentication adapters for extracting user IDs from requests.
|
|
5
|
-
* Provides adapters for Supabase and mock/testing scenarios.
|
|
6
|
-
*/
|
|
7
|
-
// Export mock adapter (no dependencies)
|
|
8
|
-
export { MockAuthAdapter } from './mock';
|
|
9
|
-
// Export Next.js route utilities
|
|
10
|
-
export { getUserIdFromRequest, requireUserId, getUserEmailFromRequest, getUserNameFromRequest, } from './next-utils';
|
|
11
|
-
// Note: SupabaseAuthAdapter is exported from ./supabase.ts directly
|
|
12
|
-
// Users import it via: import { SupabaseAuthAdapter } from '@solvapay/auth/supabase'
|
|
13
|
-
// This keeps the main export lean if users don't need Supabase
|
|
14
|
-
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,wCAAwC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAA;AAGxC,iCAAiC;AACjC,OAAO,EACL,oBAAoB,EACpB,aAAa,EACb,uBAAuB,EACvB,sBAAsB,GACvB,MAAM,cAAc,CAAA;AAErB,oEAAoE;AACpE,qFAAqF;AACrF,+DAA+D"}
|
package/dist/src/mock.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mock Auth Adapter
|
|
3
|
-
*
|
|
4
|
-
* Simple adapter for testing and examples that extracts user ID from headers
|
|
5
|
-
* or environment variables. No dependencies required.
|
|
6
|
-
*/
|
|
7
|
-
import type { AuthAdapter, RequestLike } from './adapter';
|
|
8
|
-
/**
|
|
9
|
-
* Mock authentication adapter
|
|
10
|
-
*
|
|
11
|
-
* Extracts user ID from `x-mock-user-id` header or `MOCK_USER_ID` environment variable.
|
|
12
|
-
* Useful for testing, examples, and development where real authentication isn't needed.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```ts
|
|
16
|
-
* import { MockAuthAdapter } from '@solvapay/auth/mock';
|
|
17
|
-
*
|
|
18
|
-
* const auth = new MockAuthAdapter();
|
|
19
|
-
*
|
|
20
|
-
* // In tests or examples:
|
|
21
|
-
* // Set header: x-mock-user-id: user_123
|
|
22
|
-
* // Or set env: MOCK_USER_ID=user_123
|
|
23
|
-
*
|
|
24
|
-
* const userId = await auth.getUserIdFromRequest(request);
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
export declare class MockAuthAdapter implements AuthAdapter {
|
|
28
|
-
getUserIdFromRequest(req: Request | RequestLike): Promise<string | null>;
|
|
29
|
-
}
|
|
30
|
-
//# sourceMappingURL=mock.d.ts.map
|
package/dist/src/mock.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mock.d.ts","sourceRoot":"","sources":["../../src/mock.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEzD;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,eAAgB,YAAW,WAAW;IAC3C,oBAAoB,CAAC,GAAG,EAAE,OAAO,GAAG,WAAW,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CAgB/E"}
|
package/dist/src/mock.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mock Auth Adapter
|
|
3
|
-
*
|
|
4
|
-
* Simple adapter for testing and examples that extracts user ID from headers
|
|
5
|
-
* or environment variables. No dependencies required.
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* Mock authentication adapter
|
|
9
|
-
*
|
|
10
|
-
* Extracts user ID from `x-mock-user-id` header or `MOCK_USER_ID` environment variable.
|
|
11
|
-
* Useful for testing, examples, and development where real authentication isn't needed.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* import { MockAuthAdapter } from '@solvapay/auth/mock';
|
|
16
|
-
*
|
|
17
|
-
* const auth = new MockAuthAdapter();
|
|
18
|
-
*
|
|
19
|
-
* // In tests or examples:
|
|
20
|
-
* // Set header: x-mock-user-id: user_123
|
|
21
|
-
* // Or set env: MOCK_USER_ID=user_123
|
|
22
|
-
*
|
|
23
|
-
* const userId = await auth.getUserIdFromRequest(request);
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
export class MockAuthAdapter {
|
|
27
|
-
async getUserIdFromRequest(req) {
|
|
28
|
-
const headers = 'headers' in req ? req.headers : req;
|
|
29
|
-
// First try to get from header
|
|
30
|
-
const headerUserId = headers.get('x-mock-user-id');
|
|
31
|
-
if (headerUserId) {
|
|
32
|
-
return headerUserId;
|
|
33
|
-
}
|
|
34
|
-
// Fallback to environment variable (only in Node.js runtime)
|
|
35
|
-
if (typeof process !== 'undefined' && process.env.MOCK_USER_ID) {
|
|
36
|
-
return process.env.MOCK_USER_ID;
|
|
37
|
-
}
|
|
38
|
-
return null;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
//# sourceMappingURL=mock.js.map
|
package/dist/src/mock.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mock.js","sourceRoot":"","sources":["../../src/mock.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,eAAe;IAC1B,KAAK,CAAC,oBAAoB,CAAC,GAA0B;QACnD,MAAM,OAAO,GAAG,SAAS,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAA;QAEpD,+BAA+B;QAC/B,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAA;QAClD,IAAI,YAAY,EAAE,CAAC;YACjB,OAAO,YAAY,CAAA;QACrB,CAAC;QAED,6DAA6D;QAC7D,IAAI,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,CAAC;YAC/D,OAAO,OAAO,CAAC,GAAG,CAAC,YAAY,CAAA;QACjC,CAAC;QAED,OAAO,IAAI,CAAA;IACb,CAAC;CACF"}
|