sentri 1.1.0 → 1.1.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/dist/client.d.ts +45 -10
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +3 -1
- package/dist/client.js.map +1 -1
- package/dist/errors/AuthError.d.ts +78 -18
- package/dist/errors/AuthError.d.ts.map +1 -1
- package/dist/errors/AuthError.js +83 -14
- package/dist/errors/AuthError.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/middleware/errorHandler.d.ts +73 -0
- package/dist/middleware/errorHandler.d.ts.map +1 -0
- package/dist/middleware/errorHandler.js +76 -0
- package/dist/middleware/errorHandler.js.map +1 -0
- package/dist/middleware/router.d.ts.map +1 -1
- package/dist/middleware/router.js +1 -3
- package/dist/middleware/router.js.map +1 -1
- package/package.json +1 -1
- package/templates/drizzle/auth.ts +37 -2
- package/templates/prisma/auth.ts +37 -2
package/dist/client.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PermitCheck, PermitOptions } from './middleware/permit.js';
|
|
2
|
+
import type { ErrorHandlerOptions } from './middleware/errorHandler.js';
|
|
2
3
|
import type { AuthConfig, AuthUser } from './types/auth.js';
|
|
3
|
-
import type { RequestHandler, Router } from 'express';
|
|
4
|
+
import type { ErrorRequestHandler, RequestHandler, Router } from 'express';
|
|
4
5
|
/**
|
|
5
6
|
* The bound auth client returned by {@link createAuth}.
|
|
6
7
|
*
|
|
@@ -15,7 +16,8 @@ export interface AuthClient<TRole extends string = string> {
|
|
|
15
16
|
* Express middleware factory that enforces authentication.
|
|
16
17
|
*
|
|
17
18
|
* Reads the `Authorization: Bearer <token>` header, verifies the access token,
|
|
18
|
-
*
|
|
19
|
+
* confirms the session is still active in the database, and injects the decoded
|
|
20
|
+
* payload as `request.user`. Calls `next(AuthError)` on any failure.
|
|
19
21
|
*
|
|
20
22
|
* @example
|
|
21
23
|
* router.get('/me', auth.protect(), (request, response) => {
|
|
@@ -82,25 +84,58 @@ export interface AuthClient<TRole extends string = string> {
|
|
|
82
84
|
sessionId: string;
|
|
83
85
|
};
|
|
84
86
|
/**
|
|
85
|
-
* Returns a pre-built Express Router with all standard auth endpoints mounted
|
|
87
|
+
* Returns a pre-built Express Router with all standard auth endpoints mounted.
|
|
86
88
|
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
+
* Endpoints:
|
|
90
|
+
* - `POST /register` — register a new user. Requires `X-Api-Key` header when `config.apiKey` is set.
|
|
89
91
|
* - `POST /login` — authenticate, sets refresh token cookie, returns `{ accessToken, user }`
|
|
90
|
-
* - `POST /refresh` —
|
|
91
|
-
* - `POST /logout` —
|
|
92
|
-
*
|
|
93
|
-
* - `POST /logout-all` — invalidate all sessions for the user (requires valid access token)
|
|
92
|
+
* - `POST /refresh` — rotate refresh token, returns new `{ accessToken }`
|
|
93
|
+
* - `POST /logout` — delete the current session; the bound access token is immediately rejected by `protect()`
|
|
94
|
+
* - `POST /logout-all` — delete all sessions for the user (requires valid access token)
|
|
94
95
|
* - `GET /me` — return the authenticated user
|
|
95
96
|
* - `POST /users/:userId/roles` — assign roles (requires admin)
|
|
96
97
|
*
|
|
97
|
-
* Requires `express.json()`
|
|
98
|
+
* Requires `express.json()` before the router.
|
|
98
99
|
*
|
|
99
100
|
* @example
|
|
100
101
|
* app.use(express.json());
|
|
101
102
|
* app.use('/auth', auth.router());
|
|
102
103
|
*/
|
|
103
104
|
router(): Router;
|
|
105
|
+
/**
|
|
106
|
+
* Returns an Express error-handling middleware that formats every `AuthError`
|
|
107
|
+
* (and any subclass) into the standard sentri response envelope:
|
|
108
|
+
*
|
|
109
|
+
* ```json
|
|
110
|
+
* { "error": true, "statusCode": 401, "code": "UNAUTHORIZED", "message": "...", "data": null }
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* Mount it **after all your routes** so it acts as the global catch-all for
|
|
114
|
+
* both sentri errors and your own `AuthError` subclasses.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* import { AuthError } from 'sentri';
|
|
118
|
+
*
|
|
119
|
+
* // Define app-specific errors by extending AuthError
|
|
120
|
+
* class NotFoundError extends AuthError {
|
|
121
|
+
* constructor(resource: string) {
|
|
122
|
+
* super('NOT_FOUND', `${resource} not found`, 404);
|
|
123
|
+
* }
|
|
124
|
+
* }
|
|
125
|
+
*
|
|
126
|
+
* app.use('/auth', auth.router());
|
|
127
|
+
* app.use('/api', apiRouter);
|
|
128
|
+
*
|
|
129
|
+
* // Catches errors from sentri AND your own subclasses
|
|
130
|
+
* app.use(auth.errorHandler());
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* // With optional unhandled-error logger
|
|
134
|
+
* app.use(auth.errorHandler({
|
|
135
|
+
* onUnhandled: (err) => logger.error('Unexpected error', { err }),
|
|
136
|
+
* }));
|
|
137
|
+
*/
|
|
138
|
+
errorHandler(options?: ErrorHandlerOptions): ErrorRequestHandler;
|
|
104
139
|
}
|
|
105
140
|
/**
|
|
106
141
|
* Create a fully configured auth client for your application.
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACzE,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACxE,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC5D,OAAO,KAAK,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAE3E;;;;;;;;GAQG;AACH,MAAM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ACxB,UAAU,CAAC,KAAK,CAAC,CAiBnB"}
|
package/dist/client.js
CHANGED
|
@@ -5,6 +5,7 @@ import { protect } from './middleware/protect.js';
|
|
|
5
5
|
import { authorize } from './middleware/authorize.js';
|
|
6
6
|
import { permit } from './middleware/permit.js';
|
|
7
7
|
import { createAuthRouter } from './middleware/router.js';
|
|
8
|
+
import { createErrorHandler } from './middleware/errorHandler.js';
|
|
8
9
|
/**
|
|
9
10
|
* Create a fully configured auth client for your application.
|
|
10
11
|
*
|
|
@@ -30,6 +31,7 @@ export function createAuth(config) {
|
|
|
30
31
|
return {
|
|
31
32
|
protect: () => protect(config),
|
|
32
33
|
authorize: (...roles) => authorize(...roles),
|
|
34
|
+
permit: (optionsOrCheck) => permit(optionsOrCheck),
|
|
33
35
|
hashPassword: (plain) => hashPassword(plain, resolved.saltRounds),
|
|
34
36
|
verifyPassword: (plain, hash) => verifyPassword(plain, hash),
|
|
35
37
|
signAccessToken: (payload) => signAccessToken(payload, config),
|
|
@@ -37,7 +39,7 @@ export function createAuth(config) {
|
|
|
37
39
|
verifyAccessToken: (token) => verifyAccessToken(token, config),
|
|
38
40
|
verifyRefreshToken: (token) => verifyRefreshToken(token, config),
|
|
39
41
|
router: () => createAuthRouter(config),
|
|
40
|
-
|
|
42
|
+
errorHandler: (options) => createErrorHandler(options),
|
|
41
43
|
};
|
|
42
44
|
}
|
|
43
45
|
//# sourceMappingURL=client.js.map
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAC3G,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAAE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C;QACjE,cAAc,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,cAAc,CAAC,KAAK,EAAE,IAAI,CAAC;QAC5D,eAAe,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,eAAe,CAAC,OAAmB,EAAE,MAAoB,CAAC;QACxF,gBAAgB,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,gBAAgB,CAAC,SAAS,EAAE,MAAoB,CAAC;QAClF,iBAAiB,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,iBAAiB,CAAC,KAAK,EAAE,MAAoB,CAAoB;QAC/F,kBAAkB,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,kBAAkB,CAAC,KAAK,EAAE,MAAoB,CAAC;QAC9E,MAAM,EAAE,GAAG,EAAE,CAAC,gBAAgB,CAAC,MAAM,CAAC;QACtC,YAAY,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,kBAAkB,CAAC,OAAO,CAAC;KACvD,CAAC;AACJ,CAAC"}
|
|
@@ -1,41 +1,101 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Discriminant codes for {@link AuthError}.
|
|
2
|
+
* Discriminant codes for built-in {@link AuthError} instances.
|
|
3
3
|
*
|
|
4
4
|
* - `INVALID_CREDENTIALS` — identifier or password did not match (intentionally vague to prevent user enumeration)
|
|
5
5
|
* - `USER_NOT_FOUND` — an operation required a user that does not exist
|
|
6
|
-
* - `USER_ALREADY_EXISTS` —
|
|
6
|
+
* - `USER_ALREADY_EXISTS` — registration was attempted with an identifier already in the database
|
|
7
7
|
* - `TOKEN_EXPIRED` — the JWT was valid but its `exp` claim is in the past
|
|
8
8
|
* - `TOKEN_INVALID` — the JWT could not be verified (bad signature, malformed, wrong type)
|
|
9
9
|
* - `FORBIDDEN` — the user is authenticated but lacks the required role
|
|
10
|
-
* - `UNAUTHORIZED` — no valid access token was present on the request
|
|
10
|
+
* - `UNAUTHORIZED` — no valid access token was present on the request, or the session was revoked
|
|
11
11
|
* - `INVALID_ROLE` — a role name was used that is not in `validRoles`
|
|
12
12
|
* - `VALIDATION_ERROR` — a required field was missing or had an invalid value
|
|
13
13
|
* - `CONFIGURATION_ERROR` — `createAuth` was called with an invalid configuration
|
|
14
|
+
*
|
|
15
|
+
* When you extend {@link AuthError} for your own error types you can use any
|
|
16
|
+
* string as `code` — it does not need to be one of these built-in values.
|
|
14
17
|
*/
|
|
15
18
|
export type AuthErrorCode = 'INVALID_CREDENTIALS' | 'USER_NOT_FOUND' | 'USER_ALREADY_EXISTS' | 'TOKEN_EXPIRED' | 'TOKEN_INVALID' | 'FORBIDDEN' | 'UNAUTHORIZED' | 'INVALID_ROLE' | 'VALIDATION_ERROR' | 'CONFIGURATION_ERROR';
|
|
16
19
|
/**
|
|
17
|
-
*
|
|
20
|
+
* Default HTTP status codes for built-in error codes.
|
|
21
|
+
* Custom codes that are not in this map default to 500.
|
|
22
|
+
*
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
export declare const AUTH_ERROR_STATUS: Record<string, number>;
|
|
26
|
+
/**
|
|
27
|
+
* Base error class for all authentication and authorization failures.
|
|
28
|
+
*
|
|
29
|
+
* Every error thrown by sentri is an instance of `AuthError`. The `code`
|
|
30
|
+
* property is a machine-readable string that lets you distinguish error
|
|
31
|
+
* types without string-matching on the message. Built-in codes are listed
|
|
32
|
+
* in {@link AuthErrorCode}; custom subclasses may use any string.
|
|
33
|
+
*
|
|
34
|
+
* The `statusCode` property holds the HTTP status that the built-in router
|
|
35
|
+
* and `createErrorHandler()` will use in the response. For built-in codes
|
|
36
|
+
* it is derived automatically. Pass it explicitly when subclassing with a
|
|
37
|
+
* custom code.
|
|
38
|
+
*
|
|
39
|
+
* ---
|
|
18
40
|
*
|
|
19
|
-
*
|
|
20
|
-
* string-matching on the message.
|
|
41
|
+
* **Extending AuthError**
|
|
21
42
|
*
|
|
22
|
-
*
|
|
43
|
+
* You can create application-specific error classes by extending `AuthError`.
|
|
44
|
+
* Any subclass will be caught automatically by `createErrorHandler()` because
|
|
45
|
+
* `instanceof AuthError` is `true` for all subclasses.
|
|
46
|
+
*
|
|
47
|
+
* ```typescript
|
|
23
48
|
* import { AuthError } from 'sentri';
|
|
24
49
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* : error.code === 'FORBIDDEN' ? 403
|
|
30
|
-
* : 400;
|
|
31
|
-
* response.status(status).json({ error: error.code, message: error.message });
|
|
32
|
-
* } else {
|
|
33
|
-
* next(error);
|
|
50
|
+
* // Domain error with a custom code and explicit HTTP status
|
|
51
|
+
* export class PaymentError extends AuthError {
|
|
52
|
+
* constructor(message: string) {
|
|
53
|
+
* super('PAYMENT_FAILED', message, 402);
|
|
34
54
|
* }
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* // Throw it anywhere in your routes — createErrorHandler() catches it
|
|
58
|
+
* router.post('/checkout', auth.protect(), async (req, res) => {
|
|
59
|
+
* const ok = await chargeCard(req.body.cardToken);
|
|
60
|
+
* if (!ok) throw new PaymentError('Card declined');
|
|
61
|
+
* res.json({ success: true });
|
|
35
62
|
* });
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* ---
|
|
66
|
+
*
|
|
67
|
+
* **Error handling in custom routes**
|
|
68
|
+
*
|
|
69
|
+
* ```typescript
|
|
70
|
+
* import { AuthError, createErrorHandler } from 'sentri';
|
|
71
|
+
*
|
|
72
|
+
* app.use('/auth', auth.router());
|
|
73
|
+
* app.use('/api', apiRouter);
|
|
74
|
+
*
|
|
75
|
+
* // Mount after all routes — catches AuthError from sentri AND your subclasses
|
|
76
|
+
* app.use(createErrorHandler());
|
|
77
|
+
* ```
|
|
36
78
|
*/
|
|
37
79
|
export declare class AuthError extends Error {
|
|
38
|
-
|
|
39
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Machine-readable error code.
|
|
82
|
+
* Built-in codes are defined by {@link AuthErrorCode}.
|
|
83
|
+
* Custom subclasses may use any string.
|
|
84
|
+
*/
|
|
85
|
+
readonly code: string;
|
|
86
|
+
/**
|
|
87
|
+
* HTTP status code associated with this error.
|
|
88
|
+
* Derived automatically for built-in codes; pass it explicitly when
|
|
89
|
+
* subclassing with a custom `code`.
|
|
90
|
+
*/
|
|
91
|
+
readonly statusCode: number;
|
|
92
|
+
/**
|
|
93
|
+
* @param code - Machine-readable error code. Use a built-in {@link AuthErrorCode}
|
|
94
|
+
* or any string for custom subclasses.
|
|
95
|
+
* @param message - Human-readable description of the error.
|
|
96
|
+
* @param statusCode - HTTP status to use in the response. For built-in codes
|
|
97
|
+
* this is derived automatically; for custom codes it defaults to `500`.
|
|
98
|
+
*/
|
|
99
|
+
constructor(code: AuthErrorCode | (string & {}), message: string, statusCode?: number);
|
|
40
100
|
}
|
|
41
101
|
//# sourceMappingURL=AuthError.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthError.d.ts","sourceRoot":"","sources":["../../src/errors/AuthError.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"AuthError.d.ts","sourceRoot":"","sources":["../../src/errors/AuthError.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,aAAa,GACrB,qBAAqB,GACrB,gBAAgB,GAChB,qBAAqB,GACrB,eAAe,GACf,eAAe,GACf,WAAW,GACX,cAAc,GACd,cAAc,GACd,kBAAkB,GAClB,qBAAqB,CAAC;AAE1B;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAWpD,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,qBAAa,SAAU,SAAQ,KAAK;IAClC;;;;OAIG;IACH,SAAgB,IAAI,EAAE,MAAM,CAAC;IAE7B;;;;OAIG;IACH,SAAgB,UAAU,EAAE,MAAM,CAAC;IAEnC;;;;;;OAMG;gBAED,IAAI,EAAE,aAAa,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,EACnC,OAAO,EAAE,MAAM,EACf,UAAU,CAAC,EAAE,MAAM;CAOtB"}
|
package/dist/errors/AuthError.js
CHANGED
|
@@ -1,30 +1,99 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Default HTTP status codes for built-in error codes.
|
|
3
|
+
* Custom codes that are not in this map default to 500.
|
|
3
4
|
*
|
|
4
|
-
*
|
|
5
|
-
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export const AUTH_ERROR_STATUS = {
|
|
8
|
+
UNAUTHORIZED: 401,
|
|
9
|
+
TOKEN_EXPIRED: 401,
|
|
10
|
+
TOKEN_INVALID: 401,
|
|
11
|
+
INVALID_CREDENTIALS: 401,
|
|
12
|
+
FORBIDDEN: 403,
|
|
13
|
+
USER_NOT_FOUND: 404,
|
|
14
|
+
USER_ALREADY_EXISTS: 409,
|
|
15
|
+
INVALID_ROLE: 400,
|
|
16
|
+
VALIDATION_ERROR: 400,
|
|
17
|
+
CONFIGURATION_ERROR: 500,
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Base error class for all authentication and authorization failures.
|
|
21
|
+
*
|
|
22
|
+
* Every error thrown by sentri is an instance of `AuthError`. The `code`
|
|
23
|
+
* property is a machine-readable string that lets you distinguish error
|
|
24
|
+
* types without string-matching on the message. Built-in codes are listed
|
|
25
|
+
* in {@link AuthErrorCode}; custom subclasses may use any string.
|
|
26
|
+
*
|
|
27
|
+
* The `statusCode` property holds the HTTP status that the built-in router
|
|
28
|
+
* and `createErrorHandler()` will use in the response. For built-in codes
|
|
29
|
+
* it is derived automatically. Pass it explicitly when subclassing with a
|
|
30
|
+
* custom code.
|
|
31
|
+
*
|
|
32
|
+
* ---
|
|
33
|
+
*
|
|
34
|
+
* **Extending AuthError**
|
|
35
|
+
*
|
|
36
|
+
* You can create application-specific error classes by extending `AuthError`.
|
|
37
|
+
* Any subclass will be caught automatically by `createErrorHandler()` because
|
|
38
|
+
* `instanceof AuthError` is `true` for all subclasses.
|
|
6
39
|
*
|
|
7
|
-
*
|
|
40
|
+
* ```typescript
|
|
8
41
|
* import { AuthError } from 'sentri';
|
|
9
42
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* : error.code === 'FORBIDDEN' ? 403
|
|
15
|
-
* : 400;
|
|
16
|
-
* response.status(status).json({ error: error.code, message: error.message });
|
|
17
|
-
* } else {
|
|
18
|
-
* next(error);
|
|
43
|
+
* // Domain error with a custom code and explicit HTTP status
|
|
44
|
+
* export class PaymentError extends AuthError {
|
|
45
|
+
* constructor(message: string) {
|
|
46
|
+
* super('PAYMENT_FAILED', message, 402);
|
|
19
47
|
* }
|
|
48
|
+
* }
|
|
49
|
+
*
|
|
50
|
+
* // Throw it anywhere in your routes — createErrorHandler() catches it
|
|
51
|
+
* router.post('/checkout', auth.protect(), async (req, res) => {
|
|
52
|
+
* const ok = await chargeCard(req.body.cardToken);
|
|
53
|
+
* if (!ok) throw new PaymentError('Card declined');
|
|
54
|
+
* res.json({ success: true });
|
|
20
55
|
* });
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* ---
|
|
59
|
+
*
|
|
60
|
+
* **Error handling in custom routes**
|
|
61
|
+
*
|
|
62
|
+
* ```typescript
|
|
63
|
+
* import { AuthError, createErrorHandler } from 'sentri';
|
|
64
|
+
*
|
|
65
|
+
* app.use('/auth', auth.router());
|
|
66
|
+
* app.use('/api', apiRouter);
|
|
67
|
+
*
|
|
68
|
+
* // Mount after all routes — catches AuthError from sentri AND your subclasses
|
|
69
|
+
* app.use(createErrorHandler());
|
|
70
|
+
* ```
|
|
21
71
|
*/
|
|
22
72
|
export class AuthError extends Error {
|
|
73
|
+
/**
|
|
74
|
+
* Machine-readable error code.
|
|
75
|
+
* Built-in codes are defined by {@link AuthErrorCode}.
|
|
76
|
+
* Custom subclasses may use any string.
|
|
77
|
+
*/
|
|
23
78
|
code;
|
|
24
|
-
|
|
79
|
+
/**
|
|
80
|
+
* HTTP status code associated with this error.
|
|
81
|
+
* Derived automatically for built-in codes; pass it explicitly when
|
|
82
|
+
* subclassing with a custom `code`.
|
|
83
|
+
*/
|
|
84
|
+
statusCode;
|
|
85
|
+
/**
|
|
86
|
+
* @param code - Machine-readable error code. Use a built-in {@link AuthErrorCode}
|
|
87
|
+
* or any string for custom subclasses.
|
|
88
|
+
* @param message - Human-readable description of the error.
|
|
89
|
+
* @param statusCode - HTTP status to use in the response. For built-in codes
|
|
90
|
+
* this is derived automatically; for custom codes it defaults to `500`.
|
|
91
|
+
*/
|
|
92
|
+
constructor(code, message, statusCode) {
|
|
25
93
|
super(message);
|
|
26
94
|
this.name = 'AuthError';
|
|
27
95
|
this.code = code;
|
|
96
|
+
this.statusCode = statusCode ?? AUTH_ERROR_STATUS[code] ?? 500;
|
|
28
97
|
}
|
|
29
98
|
}
|
|
30
99
|
//# sourceMappingURL=AuthError.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AuthError.js","sourceRoot":"","sources":["../../src/errors/AuthError.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AuthError.js","sourceRoot":"","sources":["../../src/errors/AuthError.ts"],"names":[],"mappings":"AA6BA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAA2B;IACvD,YAAY,EAAE,GAAG;IACjB,aAAa,EAAE,GAAG;IAClB,aAAa,EAAE,GAAG;IAClB,mBAAmB,EAAE,GAAG;IACxB,SAAS,EAAE,GAAG;IACd,cAAc,EAAE,GAAG;IACnB,mBAAmB,EAAE,GAAG;IACxB,YAAY,EAAE,GAAG;IACjB,gBAAgB,EAAE,GAAG;IACrB,mBAAmB,EAAE,GAAG;CACzB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IAClC;;;;OAIG;IACa,IAAI,CAAS;IAE7B;;;;OAIG;IACa,UAAU,CAAS;IAEnC;;;;;;OAMG;IACH,YACE,IAAmC,EACnC,OAAe,EACf,UAAmB;QAEnB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,UAAU,GAAG,UAAU,IAAI,iBAAiB,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC;IACjE,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,7 +9,9 @@ declare global {
|
|
|
9
9
|
export type { AuthConfig, CookieConfig, AuthUser, ApiResponse, AuthAdapter, UserRecord, SessionRecord, CreateUserData, RouterHandlers, SignupInput, LoginInput, SignupResult, AuthResult, RefreshResult, AssignRolesResult, } from './types/auth.js';
|
|
10
10
|
export type { AuthErrorCode } from './errors/AuthError.js';
|
|
11
11
|
export type { AuthClient } from './client.js';
|
|
12
|
-
export {
|
|
12
|
+
export type { ErrorHandlerOptions } from './middleware/errorHandler.js';
|
|
13
|
+
export { AuthError, AUTH_ERROR_STATUS } from './errors/AuthError.js';
|
|
13
14
|
export { createAuth } from './client.js';
|
|
15
|
+
export { createErrorHandler } from './middleware/errorHandler.js';
|
|
14
16
|
export type { PermitCheck, PermitOptions } from './middleware/permit.js';
|
|
15
17
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAIhD,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,OAAO,CAAC;QAChB,UAAU,OAAO;YACf,IAAI,CAAC,EAAE,QAAQ,CAAC;SACjB;KACF;CACF;AAED,YAAY,EACV,UAAU,EACV,YAAY,EACZ,QAAQ,EACR,WAAW,EACX,WAAW,EACX,UAAU,EACV,aAAa,EACb,cAAc,EACd,cAAc,EACd,WAAW,EACX,UAAU,EACV,YAAY,EACZ,UAAU,EACV,aAAa,EACb,iBAAiB,GAClB,MAAM,iBAAiB,CAAC;AACzB,YAAY,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAIhD,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,OAAO,CAAC;QAChB,UAAU,OAAO;YACf,IAAI,CAAC,EAAE,QAAQ,CAAC;SACjB;KACF;CACF;AAED,YAAY,EACV,UAAU,EACV,YAAY,EACZ,QAAQ,EACR,WAAW,EACX,WAAW,EACX,UAAU,EACV,aAAa,EACb,cAAc,EACd,cAAc,EACd,WAAW,EACX,UAAU,EACV,YAAY,EACZ,UAAU,EACV,aAAa,EACb,iBAAiB,GAClB,MAAM,iBAAiB,CAAC;AACzB,YAAY,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,YAAY,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAExE,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAiCA,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { ErrorRequestHandler } from 'express';
|
|
2
|
+
/**
|
|
3
|
+
* Options for {@link createErrorHandler}.
|
|
4
|
+
*/
|
|
5
|
+
export interface ErrorHandlerOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Called for errors that are **not** an `AuthError` instance (or subclass).
|
|
8
|
+
*
|
|
9
|
+
* Use this to log unexpected server errors before the generic 500 response
|
|
10
|
+
* is sent. The error is passed as-is and may be any unknown value.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* app.use(createErrorHandler({
|
|
14
|
+
* onUnhandled: (err) => logger.error('Unhandled error', { err }),
|
|
15
|
+
* }));
|
|
16
|
+
*/
|
|
17
|
+
onUnhandled?: (error: unknown) => void;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates an Express error-handling middleware that formats every `AuthError`
|
|
21
|
+
* (including subclasses) into the standard sentri response envelope:
|
|
22
|
+
*
|
|
23
|
+
* ```json
|
|
24
|
+
* { "error": true, "statusCode": 401, "code": "UNAUTHORIZED", "message": "...", "data": null }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* Mount it **after all your routes** so it acts as the global catch-all:
|
|
28
|
+
*
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { createErrorHandler } from 'sentri';
|
|
31
|
+
*
|
|
32
|
+
* app.use('/auth', auth.router());
|
|
33
|
+
* app.use('/api', apiRouter);
|
|
34
|
+
*
|
|
35
|
+
* // Must come after all route/middleware registrations
|
|
36
|
+
* app.use(createErrorHandler());
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* ---
|
|
40
|
+
*
|
|
41
|
+
* **Works with built-in sentri errors and your own subclasses**
|
|
42
|
+
*
|
|
43
|
+
* Because `instanceof AuthError` matches any subclass, you can define
|
|
44
|
+
* application-specific error types and have them automatically formatted
|
|
45
|
+
* by this handler:
|
|
46
|
+
*
|
|
47
|
+
* ```typescript
|
|
48
|
+
* import { AuthError, createErrorHandler } from 'sentri';
|
|
49
|
+
*
|
|
50
|
+
* // Extend AuthError for domain-specific failures
|
|
51
|
+
* export class NotFoundError extends AuthError {
|
|
52
|
+
* constructor(resource: string) {
|
|
53
|
+
* super('NOT_FOUND', `${resource} not found`, 404);
|
|
54
|
+
* }
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* export class PaymentError extends AuthError {
|
|
58
|
+
* constructor(message: string) {
|
|
59
|
+
* super('PAYMENT_FAILED', message, 402);
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* // All of the above are caught and formatted by one handler
|
|
64
|
+
* app.use(createErrorHandler({
|
|
65
|
+
* onUnhandled: (err) => console.error('Unexpected error:', err),
|
|
66
|
+
* }));
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @param options - Optional configuration (see {@link ErrorHandlerOptions}).
|
|
70
|
+
* @returns An Express `ErrorRequestHandler` (4-argument middleware).
|
|
71
|
+
*/
|
|
72
|
+
export declare function createErrorHandler(options?: ErrorHandlerOptions): ErrorRequestHandler;
|
|
73
|
+
//# sourceMappingURL=errorHandler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorHandler.d.ts","sourceRoot":"","sources":["../../src/middleware/errorHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAGnD;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;;;;;;;;OAUG;IACH,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CACxC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,mBAAmB,CAsBrF"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { AuthError } from '../errors/AuthError.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates an Express error-handling middleware that formats every `AuthError`
|
|
4
|
+
* (including subclasses) into the standard sentri response envelope:
|
|
5
|
+
*
|
|
6
|
+
* ```json
|
|
7
|
+
* { "error": true, "statusCode": 401, "code": "UNAUTHORIZED", "message": "...", "data": null }
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* Mount it **after all your routes** so it acts as the global catch-all:
|
|
11
|
+
*
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import { createErrorHandler } from 'sentri';
|
|
14
|
+
*
|
|
15
|
+
* app.use('/auth', auth.router());
|
|
16
|
+
* app.use('/api', apiRouter);
|
|
17
|
+
*
|
|
18
|
+
* // Must come after all route/middleware registrations
|
|
19
|
+
* app.use(createErrorHandler());
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* ---
|
|
23
|
+
*
|
|
24
|
+
* **Works with built-in sentri errors and your own subclasses**
|
|
25
|
+
*
|
|
26
|
+
* Because `instanceof AuthError` matches any subclass, you can define
|
|
27
|
+
* application-specific error types and have them automatically formatted
|
|
28
|
+
* by this handler:
|
|
29
|
+
*
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { AuthError, createErrorHandler } from 'sentri';
|
|
32
|
+
*
|
|
33
|
+
* // Extend AuthError for domain-specific failures
|
|
34
|
+
* export class NotFoundError extends AuthError {
|
|
35
|
+
* constructor(resource: string) {
|
|
36
|
+
* super('NOT_FOUND', `${resource} not found`, 404);
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
* export class PaymentError extends AuthError {
|
|
41
|
+
* constructor(message: string) {
|
|
42
|
+
* super('PAYMENT_FAILED', message, 402);
|
|
43
|
+
* }
|
|
44
|
+
* }
|
|
45
|
+
*
|
|
46
|
+
* // All of the above are caught and formatted by one handler
|
|
47
|
+
* app.use(createErrorHandler({
|
|
48
|
+
* onUnhandled: (err) => console.error('Unexpected error:', err),
|
|
49
|
+
* }));
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @param options - Optional configuration (see {@link ErrorHandlerOptions}).
|
|
53
|
+
* @returns An Express `ErrorRequestHandler` (4-argument middleware).
|
|
54
|
+
*/
|
|
55
|
+
export function createErrorHandler(options) {
|
|
56
|
+
return (err, _req, res, _next) => {
|
|
57
|
+
if (err instanceof AuthError) {
|
|
58
|
+
res.status(err.statusCode).json({
|
|
59
|
+
error: true,
|
|
60
|
+
statusCode: err.statusCode,
|
|
61
|
+
code: err.code,
|
|
62
|
+
message: err.message,
|
|
63
|
+
data: null,
|
|
64
|
+
});
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
options?.onUnhandled?.(err);
|
|
68
|
+
res.status(500).json({
|
|
69
|
+
error: true,
|
|
70
|
+
statusCode: 500,
|
|
71
|
+
message: 'Internal server error',
|
|
72
|
+
data: null,
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=errorHandler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorHandler.js","sourceRoot":"","sources":["../../src/middleware/errorHandler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAoBnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAA6B;IAC9D,OAAO,CAAC,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE;QAC/B,IAAI,GAAG,YAAY,SAAS,EAAE,CAAC;YAC7B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC;gBAC9B,KAAK,EAAE,IAAI;gBACX,UAAU,EAAE,GAAG,CAAC,UAAU;gBAC1B,IAAI,EAAE,GAAG,CAAC,IAAI;gBACd,OAAO,EAAE,GAAG,CAAC,OAAO;gBACpB,IAAI,EAAE,IAAI;aACX,CAAC,CAAC;YACH,OAAO;QACT,CAAC;QAED,OAAO,EAAE,WAAW,EAAE,CAAC,GAAG,CAAC,CAAC;QAE5B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;YACnB,KAAK,EAAE,IAAI;YACX,UAAU,EAAE,GAAG;YACf,OAAO,EAAE,uBAAuB;YAChC,IAAI,EAAE,IAAI;SACX,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/middleware/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAkD,MAAM,SAAS,CAAC;AAEjF,OAAO,KAAK,EAAE,UAAU,EAA2B,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../../src/middleware/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAkD,MAAM,SAAS,CAAC;AAEjF,OAAO,KAAK,EAAE,UAAU,EAA2B,MAAM,kBAAkB,CAAC;AA4E5E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,SAAS,MAAM,EAAE,MAAM,EAAE,UAAU,CAAC,KAAK,CAAC,GAAG,MAAM,CAwLxF"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Router } from 'express';
|
|
2
2
|
import { AuthError } from '../errors/AuthError.js';
|
|
3
|
-
import { authErrorStatus } from '../types/auth.js';
|
|
4
3
|
import { signup, login, refresh, logout, logoutAll, assignRoles } from '../services/auth.js';
|
|
5
4
|
import { resolveConfig, parseExpiry } from '../libs/config.js';
|
|
6
5
|
import { protect } from './protect.js';
|
|
@@ -18,8 +17,7 @@ function ok(response, statusCode, message, data) {
|
|
|
18
17
|
response.status(statusCode).json({ error: false, statusCode, message, data });
|
|
19
18
|
}
|
|
20
19
|
function fail(response, error) {
|
|
21
|
-
|
|
22
|
-
response.status(statusCode).json({ error: true, statusCode, message: error.message, data: null });
|
|
20
|
+
response.status(error.statusCode).json({ error: true, statusCode: error.statusCode, message: error.message, data: null });
|
|
23
21
|
}
|
|
24
22
|
function parseBody(body) {
|
|
25
23
|
if (body === null || body === undefined || typeof body !== 'object' || Array.isArray(body)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/middleware/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAkD,MAAM,SAAS,CAAC;AACjF,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/middleware/router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAkD,MAAM,SAAS,CAAC;AACjF,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAC7F,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,MAAM,mBAAmB,GAAG,CAAC,C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O,EAAE,uBAAuB,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QAC5G,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
package/package.json
CHANGED
|
@@ -17,6 +17,7 @@ export const auth = createAuth({
|
|
|
17
17
|
// refreshExpiresIn: '7d',
|
|
18
18
|
// algorithm: 'HS256',
|
|
19
19
|
// saltRounds: 12,
|
|
20
|
+
// apiKey: process.env.REGISTER_API_KEY, // when set, POST /register requires X-Api-Key header
|
|
20
21
|
cookie: {
|
|
21
22
|
secure: process.env.NODE_ENV === 'production',
|
|
22
23
|
// name: 'refresh_token',
|
|
@@ -25,8 +26,8 @@ export const auth = createAuth({
|
|
|
25
26
|
// path: '/',
|
|
26
27
|
},
|
|
27
28
|
// router: {
|
|
28
|
-
//
|
|
29
|
-
// // custom
|
|
29
|
+
// register: async (input) => {
|
|
30
|
+
// // custom register logic — must return SignupResult
|
|
30
31
|
// },
|
|
31
32
|
// login: async (input) => {
|
|
32
33
|
// // custom login logic — must return AuthResult
|
|
@@ -45,3 +46,37 @@ export const auth = createAuth({
|
|
|
45
46
|
// },
|
|
46
47
|
// },
|
|
47
48
|
});
|
|
49
|
+
|
|
50
|
+
// --- Express app setup ---
|
|
51
|
+
//
|
|
52
|
+
// import express from 'express';
|
|
53
|
+
// import { AuthError } from 'sentri';
|
|
54
|
+
//
|
|
55
|
+
// const app = express();
|
|
56
|
+
// app.use(express.json());
|
|
57
|
+
//
|
|
58
|
+
// // Mount the auth router (POST /auth/register, /auth/login, etc.)
|
|
59
|
+
// app.use('/auth', auth.router());
|
|
60
|
+
//
|
|
61
|
+
// // Your own routes — throw AuthError (or any subclass) and errorHandler catches them
|
|
62
|
+
// app.get('/protected', auth.protect(), (req, res) => {
|
|
63
|
+
// res.json(req.user);
|
|
64
|
+
// });
|
|
65
|
+
//
|
|
66
|
+
// // Domain-specific error by extending AuthError
|
|
67
|
+
// class NotFoundError extends AuthError {
|
|
68
|
+
// constructor(resource: string) {
|
|
69
|
+
// super('NOT_FOUND', `${resource} not found`, 404);
|
|
70
|
+
// }
|
|
71
|
+
// }
|
|
72
|
+
//
|
|
73
|
+
// app.get('/items/:id', auth.protect(), async (req, res) => {
|
|
74
|
+
// const item = await db.query.items.findFirst({ where: (t, { eq }) => eq(t.id, req.params['id']) });
|
|
75
|
+
// if (!item) throw new NotFoundError('Item');
|
|
76
|
+
// res.json(item);
|
|
77
|
+
// });
|
|
78
|
+
//
|
|
79
|
+
// // Mount AFTER all routes — catches AuthError from sentri AND your subclasses
|
|
80
|
+
// app.use(auth.errorHandler());
|
|
81
|
+
//
|
|
82
|
+
// app.listen(3000);
|
package/templates/prisma/auth.ts
CHANGED
|
@@ -20,6 +20,7 @@ export const auth = createAuth({
|
|
|
20
20
|
// refreshExpiresIn: '7d',
|
|
21
21
|
// algorithm: 'HS256',
|
|
22
22
|
// saltRounds: 12,
|
|
23
|
+
// apiKey: process.env.REGISTER_API_KEY, // when set, POST /register requires X-Api-Key header
|
|
23
24
|
cookie: {
|
|
24
25
|
secure: process.env.NODE_ENV === 'production',
|
|
25
26
|
// name: 'refresh_token',
|
|
@@ -28,8 +29,8 @@ export const auth = createAuth({
|
|
|
28
29
|
// path: '/',
|
|
29
30
|
},
|
|
30
31
|
// router: {
|
|
31
|
-
//
|
|
32
|
-
// // custom
|
|
32
|
+
// register: async (input) => {
|
|
33
|
+
// // custom register logic — must return SignupResult
|
|
33
34
|
// },
|
|
34
35
|
// login: async (input) => {
|
|
35
36
|
// // custom login logic — must return AuthResult
|
|
@@ -48,3 +49,37 @@ export const auth = createAuth({
|
|
|
48
49
|
// },
|
|
49
50
|
// },
|
|
50
51
|
});
|
|
52
|
+
|
|
53
|
+
// --- Express app setup ---
|
|
54
|
+
//
|
|
55
|
+
// import express from 'express';
|
|
56
|
+
// import { AuthError } from 'sentri';
|
|
57
|
+
//
|
|
58
|
+
// const app = express();
|
|
59
|
+
// app.use(express.json());
|
|
60
|
+
//
|
|
61
|
+
// // Mount the auth router (POST /auth/register, /auth/login, etc.)
|
|
62
|
+
// app.use('/auth', auth.router());
|
|
63
|
+
//
|
|
64
|
+
// // Your own routes — throw AuthError (or any subclass) and errorHandler catches them
|
|
65
|
+
// app.get('/protected', auth.protect(), (req, res) => {
|
|
66
|
+
// res.json(req.user);
|
|
67
|
+
// });
|
|
68
|
+
//
|
|
69
|
+
// // Domain-specific error by extending AuthError
|
|
70
|
+
// class NotFoundError extends AuthError {
|
|
71
|
+
// constructor(resource: string) {
|
|
72
|
+
// super('NOT_FOUND', `${resource} not found`, 404);
|
|
73
|
+
// }
|
|
74
|
+
// }
|
|
75
|
+
//
|
|
76
|
+
// app.get('/items/:id', auth.protect(), async (req, res) => {
|
|
77
|
+
// const item = await prisma.item.findUnique({ where: { id: req.params['id'] } });
|
|
78
|
+
// if (!item) throw new NotFoundError('Item');
|
|
79
|
+
// res.json(item);
|
|
80
|
+
// });
|
|
81
|
+
//
|
|
82
|
+
// // Mount AFTER all routes — catches AuthError from sentri AND your subclasses
|
|
83
|
+
// app.use(auth.errorHandler());
|
|
84
|
+
//
|
|
85
|
+
// app.listen(3000);
|