sentri 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/README.md +1044 -0
- package/dist/client.d.ts +158 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +49 -0
- package/dist/client.js.map +1 -0
- package/dist/errors/AuthError.d.ts +40 -0
- package/dist/errors/AuthError.d.ts.map +1 -0
- package/dist/errors/AuthError.js +29 -0
- package/dist/errors/AuthError.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/libs/config.d.ts +18 -0
- package/dist/libs/config.d.ts.map +1 -0
- package/dist/libs/config.js +59 -0
- package/dist/libs/config.js.map +1 -0
- package/dist/libs/hash.d.ts +3 -0
- package/dist/libs/hash.d.ts.map +1 -0
- package/dist/libs/hash.js +8 -0
- package/dist/libs/hash.js.map +1 -0
- package/dist/libs/token.d.ts +8 -0
- package/dist/libs/token.d.ts.map +1 -0
- package/dist/libs/token.js +54 -0
- package/dist/libs/token.js.map +1 -0
- package/dist/middleware/authorize.d.ts +3 -0
- package/dist/middleware/authorize.d.ts.map +1 -0
- package/dist/middleware/authorize.js +15 -0
- package/dist/middleware/authorize.js.map +1 -0
- package/dist/middleware/permit.d.ts +62 -0
- package/dist/middleware/permit.d.ts.map +1 -0
- package/dist/middleware/permit.js +61 -0
- package/dist/middleware/permit.js.map +1 -0
- package/dist/middleware/protect.d.ts +4 -0
- package/dist/middleware/protect.d.ts.map +1 -0
- package/dist/middleware/protect.js +19 -0
- package/dist/middleware/protect.js.map +1 -0
- package/dist/middleware/router.d.ts +27 -0
- package/dist/middleware/router.d.ts.map +1 -0
- package/dist/middleware/router.js +244 -0
- package/dist/middleware/router.js.map +1 -0
- package/dist/services/auth.d.ts +7 -0
- package/dist/services/auth.d.ts.map +1 -0
- package/dist/services/auth.js +84 -0
- package/dist/services/auth.js.map +1 -0
- package/dist/types/auth.d.ts +234 -0
- package/dist/types/auth.d.ts.map +1 -0
- package/dist/types/auth.js +2 -0
- package/dist/types/auth.js.map +1 -0
- package/package.json +38 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import type { AuthError } from '../errors/AuthError.js';
|
|
2
|
+
export type { AuthError };
|
|
3
|
+
/** Shape of a user row returned by the adapter — used internally by the library. */
|
|
4
|
+
export interface UserRecord {
|
|
5
|
+
id: string;
|
|
6
|
+
/**
|
|
7
|
+
* The credential identifier for this user (email, username, phone number, etc.).
|
|
8
|
+
* The adapter decides which column(s) this maps to.
|
|
9
|
+
*/
|
|
10
|
+
identifier: string;
|
|
11
|
+
passwordHash: string;
|
|
12
|
+
/** Role names currently assigned to the user. */
|
|
13
|
+
roles: string[];
|
|
14
|
+
}
|
|
15
|
+
/** Shape of a session row returned by the adapter. */
|
|
16
|
+
export interface SessionRecord {
|
|
17
|
+
id: string;
|
|
18
|
+
userId: string;
|
|
19
|
+
expiresAt: Date;
|
|
20
|
+
createdAt: Date;
|
|
21
|
+
}
|
|
22
|
+
/** Data the library passes to the adapter when creating a new user. */
|
|
23
|
+
export interface CreateUserData {
|
|
24
|
+
/**
|
|
25
|
+
* The credential identifier supplied at signup (email, username, phone, etc.).
|
|
26
|
+
* Store this in whichever column(s) your schema uses for login lookup.
|
|
27
|
+
*/
|
|
28
|
+
identifier: string;
|
|
29
|
+
passwordHash: string;
|
|
30
|
+
/** Validated role names to assign at creation. */
|
|
31
|
+
roles: string[];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The database adapter interface the library depends on.
|
|
35
|
+
*
|
|
36
|
+
* Implement this to connect the library to any ORM or data layer.
|
|
37
|
+
*
|
|
38
|
+
* The library uses a single `identifier` string for credentials — your adapter
|
|
39
|
+
* decides what that means: email column, username column, phone column, or a
|
|
40
|
+
* query across multiple columns.
|
|
41
|
+
*/
|
|
42
|
+
export interface AuthAdapter {
|
|
43
|
+
user: {
|
|
44
|
+
/**
|
|
45
|
+
* Find a user by their login identifier.
|
|
46
|
+
*
|
|
47
|
+
* The adapter decides which column(s) to query — email, username, phone,
|
|
48
|
+
* or a combined lookup (`WHERE email = $1 OR username = $1`).
|
|
49
|
+
* Returns `null` if not found.
|
|
50
|
+
*/
|
|
51
|
+
findByIdentifier(identifier: string): Promise<UserRecord | null>;
|
|
52
|
+
/** Find a user by their primary key. Returns `null` if not found. */
|
|
53
|
+
findById(id: string): Promise<UserRecord | null>;
|
|
54
|
+
/**
|
|
55
|
+
* Persist a new user with the given identifier, hashed password, and roles.
|
|
56
|
+
* The adapter maps `identifier` to the appropriate column(s) in your schema.
|
|
57
|
+
*/
|
|
58
|
+
create(data: CreateUserData): Promise<{
|
|
59
|
+
id: string;
|
|
60
|
+
}>;
|
|
61
|
+
};
|
|
62
|
+
session: {
|
|
63
|
+
/**
|
|
64
|
+
* Persist a new session and return its generated ID.
|
|
65
|
+
* `expiresAt` is computed from `refreshExpiresIn` in config.
|
|
66
|
+
*/
|
|
67
|
+
create(data: {
|
|
68
|
+
userId: string;
|
|
69
|
+
expiresAt: Date;
|
|
70
|
+
}): Promise<{
|
|
71
|
+
id: string;
|
|
72
|
+
}>;
|
|
73
|
+
/**
|
|
74
|
+
* Find a session by its ID, including the associated user.
|
|
75
|
+
* Returns `null` if the session does not exist (i.e. has been revoked).
|
|
76
|
+
*/
|
|
77
|
+
findById(sessionId: string): Promise<(SessionRecord & {
|
|
78
|
+
user: UserRecord;
|
|
79
|
+
}) | null>;
|
|
80
|
+
/** Delete a single session. Used during logout and token rotation. */
|
|
81
|
+
delete(sessionId: string): Promise<void>;
|
|
82
|
+
/** Delete all sessions belonging to a user. Used for "logout from all devices". */
|
|
83
|
+
deleteAllForUser(userId: string): Promise<void>;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Configuration passed to {@link createAuth}.
|
|
88
|
+
*
|
|
89
|
+
* Only `secret`, `validRoles`, and `adapter` are required.
|
|
90
|
+
* All other fields have sensible defaults.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* createAuth({
|
|
94
|
+
* secret: process.env.JWT_SECRET!,
|
|
95
|
+
* validRoles: ['user', 'admin'] as const,
|
|
96
|
+
* adapter: myAdapter,
|
|
97
|
+
* });
|
|
98
|
+
*/
|
|
99
|
+
export interface AuthConfig<TRole extends string = string> {
|
|
100
|
+
/** Secret used to sign JWT tokens. Must not be empty. Keep this in an env variable. */
|
|
101
|
+
secret: string;
|
|
102
|
+
/**
|
|
103
|
+
* How long access tokens are valid.
|
|
104
|
+
* Accepts a duration string (`'15m'`, `'1h'`) or seconds as a number.
|
|
105
|
+
* @default '15m'
|
|
106
|
+
*/
|
|
107
|
+
accessExpiresIn?: string | number;
|
|
108
|
+
/**
|
|
109
|
+
* How long refresh tokens / sessions are valid.
|
|
110
|
+
* Accepts a duration string (`'7d'`, `'30d'`) or seconds as a number.
|
|
111
|
+
* @default '7d'
|
|
112
|
+
*/
|
|
113
|
+
refreshExpiresIn?: string | number;
|
|
114
|
+
/**
|
|
115
|
+
* HMAC signing algorithm used for JWTs.
|
|
116
|
+
* @default 'HS256'
|
|
117
|
+
*/
|
|
118
|
+
algorithm?: 'HS256' | 'HS384' | 'HS512';
|
|
119
|
+
/**
|
|
120
|
+
* bcrypt cost factor. Higher = slower hashing but more secure.
|
|
121
|
+
* @default 12
|
|
122
|
+
*/
|
|
123
|
+
saltRounds?: number;
|
|
124
|
+
/**
|
|
125
|
+
* Exhaustive list of role names your application uses.
|
|
126
|
+
* Signup will be rejected with `INVALID_ROLE` if a role outside this list is requested.
|
|
127
|
+
* Use `as const` to get TypeScript union-type safety on `authorize()`.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* validRoles: ['user', 'admin', 'moderator'] as const
|
|
131
|
+
*/
|
|
132
|
+
validRoles: readonly TRole[];
|
|
133
|
+
/** ORM adapter that connects the library to your database. */
|
|
134
|
+
adapter: AuthAdapter;
|
|
135
|
+
/**
|
|
136
|
+
* When set, the built-in router (`auth.router()`) stores the refresh token
|
|
137
|
+
* in an httpOnly cookie instead of returning it in the response body.
|
|
138
|
+
*
|
|
139
|
+
* The `refreshToken` field is omitted from `/login`, `/signup`, and `/refresh`
|
|
140
|
+
* responses. The `/logout` and `/logout-all` routes automatically clear the cookie.
|
|
141
|
+
*
|
|
142
|
+
* No extra middleware (e.g. `cookie-parser`) is required.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* createAuth({
|
|
146
|
+
* // ...
|
|
147
|
+
* cookie: { secure: process.env.NODE_ENV === 'production' },
|
|
148
|
+
* });
|
|
149
|
+
*/
|
|
150
|
+
cookie?: CookieConfig;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Cookie settings for storing the refresh token in an httpOnly cookie.
|
|
154
|
+
* All fields are optional — defaults are chosen for security.
|
|
155
|
+
*/
|
|
156
|
+
export interface CookieConfig {
|
|
157
|
+
/**
|
|
158
|
+
* Name of the cookie.
|
|
159
|
+
* @default 'refresh_token'
|
|
160
|
+
*/
|
|
161
|
+
name?: string;
|
|
162
|
+
/**
|
|
163
|
+
* Prevents JavaScript from reading the cookie (`document.cookie`).
|
|
164
|
+
* @default true
|
|
165
|
+
*/
|
|
166
|
+
httpOnly?: boolean;
|
|
167
|
+
/**
|
|
168
|
+
* Restricts the cookie to HTTPS connections.
|
|
169
|
+
* Set to `true` in production.
|
|
170
|
+
* @default false
|
|
171
|
+
*/
|
|
172
|
+
secure?: boolean;
|
|
173
|
+
/**
|
|
174
|
+
* Controls cross-site request behaviour.
|
|
175
|
+
* @default 'strict'
|
|
176
|
+
*/
|
|
177
|
+
sameSite?: 'strict' | 'lax' | 'none';
|
|
178
|
+
/**
|
|
179
|
+
* URL path the cookie is scoped to.
|
|
180
|
+
* @default '/'
|
|
181
|
+
*/
|
|
182
|
+
path?: string;
|
|
183
|
+
}
|
|
184
|
+
/** The user payload embedded in the access token and injected as `req.user`. */
|
|
185
|
+
export interface AuthUser<TRole extends string = string> {
|
|
186
|
+
id: string;
|
|
187
|
+
/**
|
|
188
|
+
* The credential identifier for this user (email, username, phone, etc.).
|
|
189
|
+
* Reflects whatever value was passed as `identifier` at signup or login.
|
|
190
|
+
*/
|
|
191
|
+
identifier: string;
|
|
192
|
+
roles: TRole[];
|
|
193
|
+
}
|
|
194
|
+
/** Return type of `signup` and `login`. */
|
|
195
|
+
export type AuthResult<TRole extends string = string> = {
|
|
196
|
+
success: true;
|
|
197
|
+
accessToken: string;
|
|
198
|
+
refreshToken: string;
|
|
199
|
+
user: AuthUser<TRole>;
|
|
200
|
+
} | {
|
|
201
|
+
success: false;
|
|
202
|
+
error: AuthError;
|
|
203
|
+
};
|
|
204
|
+
/** Return type of `refresh`. */
|
|
205
|
+
export type RefreshResult<TRole extends string = string> = {
|
|
206
|
+
success: true;
|
|
207
|
+
accessToken: string;
|
|
208
|
+
refreshToken: string;
|
|
209
|
+
user: AuthUser<TRole>;
|
|
210
|
+
} | {
|
|
211
|
+
success: false;
|
|
212
|
+
error: AuthError;
|
|
213
|
+
};
|
|
214
|
+
/** Input for `signup`. */
|
|
215
|
+
export interface SignupInput<TRole extends string = string> {
|
|
216
|
+
/**
|
|
217
|
+
* The user's login credential — email, username, phone number, or any unique string.
|
|
218
|
+
* The adapter maps this to the appropriate column in your database.
|
|
219
|
+
*/
|
|
220
|
+
identifier: string;
|
|
221
|
+
password: string;
|
|
222
|
+
/** Roles to assign at creation. Must be a subset of `validRoles`. */
|
|
223
|
+
roles?: TRole[];
|
|
224
|
+
}
|
|
225
|
+
/** Input for `login`. */
|
|
226
|
+
export interface LoginInput {
|
|
227
|
+
/**
|
|
228
|
+
* The user's login credential — email, username, phone number, or any unique string.
|
|
229
|
+
* The adapter's `findByIdentifier` handles the lookup.
|
|
230
|
+
*/
|
|
231
|
+
identifier: string;
|
|
232
|
+
password: string;
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
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ources":["../../src/types/auth.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sentri",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Personal auth/authorization library for Express + Postgres",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc"
|
|
19
|
+
},
|
|
20
|
+
"keywords": ["auth", "authentication", "authorization", "express", "jwt", "bcrypt"],
|
|
21
|
+
"author": "rizzzdev",
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/bcrypt": "^6.0.0",
|
|
25
|
+
"@types/express": "^5.0.6",
|
|
26
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
27
|
+
"@types/node": "^22.20.0",
|
|
28
|
+
"tsx": "^4.22.4",
|
|
29
|
+
"typescript": "^6.0.3"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"bcrypt": "^6.0.0",
|
|
33
|
+
"jsonwebtoken": "^9.0.3"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"express": ">=4.0.0"
|
|
37
|
+
}
|
|
38
|
+
}
|