@owujib/sabi-auth 0.0.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.
@@ -0,0 +1,25 @@
1
+ import type { Request, Response, NextFunction } from 'express';
2
+ import type { AuthService } from './AuthService.js';
3
+ /**
4
+ * Express middleware that protects a route by requiring a valid Bearer token.
5
+ *
6
+ * On success it attaches the decoded user to `req.user` so controllers can
7
+ * read it via `ctx.request.user()`.
8
+ *
9
+ * On failure it returns a 401 JSON response — the request never reaches the controller.
10
+ *
11
+ * @example
12
+ * // Protect an entire group of routes
13
+ * import { authenticate } from '@owujib/sabi-auth';
14
+ *
15
+ * const guard = authenticate(authService);
16
+ *
17
+ * Route.group('/users', [guard], () => {
18
+ * Route.get('/', [UserController, 'index']);
19
+ * });
20
+ *
21
+ * // Or a single route
22
+ * Route.get('/me', [AuthController, 'me'], [guard]);
23
+ */
24
+ export declare function authenticate(authService: AuthService): (req: Request, res: Response, next: NextFunction) => void;
25
+ //# sourceMappingURL=AuthGuard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthGuard.d.ts","sourceRoot":"","sources":["../../src/auth/AuthGuard.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC/D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAIpD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,YAAY,CAAC,WAAW,EAAE,WAAW,IAC3C,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,YAAY,KAAG,IAAI,CAiB/D"}
@@ -0,0 +1,41 @@
1
+ // ─── authenticate ─────────────────────────────────────────────────────────────
2
+ /**
3
+ * Express middleware that protects a route by requiring a valid Bearer token.
4
+ *
5
+ * On success it attaches the decoded user to `req.user` so controllers can
6
+ * read it via `ctx.request.user()`.
7
+ *
8
+ * On failure it returns a 401 JSON response — the request never reaches the controller.
9
+ *
10
+ * @example
11
+ * // Protect an entire group of routes
12
+ * import { authenticate } from '@owujib/sabi-auth';
13
+ *
14
+ * const guard = authenticate(authService);
15
+ *
16
+ * Route.group('/users', [guard], () => {
17
+ * Route.get('/', [UserController, 'index']);
18
+ * });
19
+ *
20
+ * // Or a single route
21
+ * Route.get('/me', [AuthController, 'me'], [guard]);
22
+ */
23
+ export function authenticate(authService) {
24
+ return (req, res, next) => {
25
+ const header = req.headers.authorization;
26
+ if (!header?.startsWith('Bearer ')) {
27
+ res.status(401).json({ message: 'Unauthorized — missing token' });
28
+ return;
29
+ }
30
+ try {
31
+ const token = header.slice(7);
32
+ const user = authService.verifyAccessToken(token);
33
+ req.user = user;
34
+ next();
35
+ }
36
+ catch {
37
+ res.status(401).json({ message: 'Unauthorized — invalid or expired token' });
38
+ }
39
+ };
40
+ }
41
+ //# sourceMappingURL=AuthGuard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthGuard.js","sourceRoot":"","sources":["../../src/auth/AuthGuard.ts"],"names":[],"mappings":"AAGA,iFAAiF;AAEjF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,YAAY,CAAC,WAAwB;IACnD,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAQ,EAAE;QAC/D,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC;QAEzC,IAAI,CAAC,MAAM,EAAE,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;YACnC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,8BAA8B,EAAE,CAAC,CAAC;YAClE,OAAO;QACT,CAAC;QAED,IAAI,CAAC;YACH,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAC9B,MAAM,IAAI,GAAI,WAAW,CAAC,iBAAiB,CAAC,KAAK,CAAC,CAAC;YAClD,GAAW,CAAC,IAAI,GAAG,IAAI,CAAC;YACzB,IAAI,EAAE,CAAC;QACT,CAAC;QAAC,MAAM,CAAC;YACP,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,yCAAyC,EAAE,CAAC,CAAC;QAC/E,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,43 @@
1
+ import type { SabiAuthConfig, AuthUser, TokenPair } from '../types.js';
2
+ /**
3
+ * Core authentication service.
4
+ *
5
+ * Handles password hashing, token generation, and token verification.
6
+ * Obtain an instance from the DI container via AUTH_SERVICE.
7
+ *
8
+ * @example
9
+ * const authService = app.make<AuthService>(AUTH_SERVICE);
10
+ *
11
+ * const hash = await authService.hashPassword('secret');
12
+ * const ok = await authService.verifyPassword('secret', hash); // true
13
+ *
14
+ * const tokens = authService.generateTokens({ id: 1, email: 'a@b.com' });
15
+ * const user = authService.verifyAccessToken(tokens.accessToken);
16
+ */
17
+ export declare class AuthService {
18
+ private readonly secret;
19
+ private readonly accessTokenExpiry;
20
+ private readonly refreshTokenExpiry;
21
+ private readonly saltRounds;
22
+ constructor(config: SabiAuthConfig);
23
+ /** Hash a plain-text password. Store the result in your database — never the plain text. */
24
+ hashPassword(plain: string): Promise<string>;
25
+ /** Compare a plain-text password against a stored hash. Returns true if they match. */
26
+ verifyPassword(plain: string, hashed: string): Promise<boolean>;
27
+ /**
28
+ * Generate an access + refresh token pair for the given user.
29
+ * Call this on successful login or registration.
30
+ */
31
+ generateTokens(user: AuthUser): TokenPair;
32
+ /**
33
+ * Verify an access token and return the user payload.
34
+ * Throws if the token is missing, expired, or tampered with.
35
+ */
36
+ verifyAccessToken(token: string): AuthUser;
37
+ /**
38
+ * Verify a refresh token and return a new token pair.
39
+ * Call this when the access token expires.
40
+ */
41
+ refreshTokens(refreshToken: string): TokenPair;
42
+ }
43
+ //# sourceMappingURL=AuthService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthService.d.ts","sourceRoot":"","sources":["../../src/auth/AuthService.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAIvE;;;;;;;;;;;;;;GAcG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAS;IAC3C,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAS;IAC5C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;gBAExB,MAAM,EAAE,cAAc;IASlC,4FAA4F;IACtF,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAIlD,uFAAuF;IACjF,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAMrE;;;OAGG;IACH,cAAc,CAAC,IAAI,EAAE,QAAQ,GAAG,SAAS;IAgBzC;;;OAGG;IACH,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,QAAQ;IAK1C;;;OAGG;IACH,aAAa,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS;CAI/C"}
@@ -0,0 +1,73 @@
1
+ import bcrypt from 'bcryptjs';
2
+ import jwt from 'jsonwebtoken';
3
+ // ─── AuthService ──────────────────────────────────────────────────────────────
4
+ /**
5
+ * Core authentication service.
6
+ *
7
+ * Handles password hashing, token generation, and token verification.
8
+ * Obtain an instance from the DI container via AUTH_SERVICE.
9
+ *
10
+ * @example
11
+ * const authService = app.make<AuthService>(AUTH_SERVICE);
12
+ *
13
+ * const hash = await authService.hashPassword('secret');
14
+ * const ok = await authService.verifyPassword('secret', hash); // true
15
+ *
16
+ * const tokens = authService.generateTokens({ id: 1, email: 'a@b.com' });
17
+ * const user = authService.verifyAccessToken(tokens.accessToken);
18
+ */
19
+ export class AuthService {
20
+ secret;
21
+ accessTokenExpiry;
22
+ refreshTokenExpiry;
23
+ saltRounds;
24
+ constructor(config) {
25
+ this.secret = config.secret;
26
+ this.accessTokenExpiry = config.accessTokenExpiry ?? '15m';
27
+ this.refreshTokenExpiry = config.refreshTokenExpiry ?? '7d';
28
+ this.saltRounds = config.saltRounds ?? 12;
29
+ }
30
+ // ─── Passwords ─────────────────────────────────────────────────────────────
31
+ /** Hash a plain-text password. Store the result in your database — never the plain text. */
32
+ async hashPassword(plain) {
33
+ return bcrypt.hash(plain, this.saltRounds);
34
+ }
35
+ /** Compare a plain-text password against a stored hash. Returns true if they match. */
36
+ async verifyPassword(plain, hashed) {
37
+ return bcrypt.compare(plain, hashed);
38
+ }
39
+ // ─── Tokens ────────────────────────────────────────────────────────────────
40
+ /**
41
+ * Generate an access + refresh token pair for the given user.
42
+ * Call this on successful login or registration.
43
+ */
44
+ generateTokens(user) {
45
+ const payload = { id: user.id, email: user.email };
46
+ const accessToken = jwt.sign(payload, this.secret, {
47
+ expiresIn: this.accessTokenExpiry,
48
+ subject: String(user.id),
49
+ });
50
+ const refreshToken = jwt.sign(payload, this.secret + ':refresh', {
51
+ expiresIn: this.refreshTokenExpiry,
52
+ subject: String(user.id),
53
+ });
54
+ return { accessToken, refreshToken };
55
+ }
56
+ /**
57
+ * Verify an access token and return the user payload.
58
+ * Throws if the token is missing, expired, or tampered with.
59
+ */
60
+ verifyAccessToken(token) {
61
+ const payload = jwt.verify(token, this.secret);
62
+ return { id: payload.id, email: payload.email, ...payload };
63
+ }
64
+ /**
65
+ * Verify a refresh token and return a new token pair.
66
+ * Call this when the access token expires.
67
+ */
68
+ refreshTokens(refreshToken) {
69
+ const payload = jwt.verify(refreshToken, this.secret + ':refresh');
70
+ return this.generateTokens({ id: payload.id, email: payload.email });
71
+ }
72
+ }
73
+ //# sourceMappingURL=AuthService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthService.js","sourceRoot":"","sources":["../../src/auth/AuthService.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,UAAU,CAAC;AAC9B,OAAO,GAAG,MAAM,cAAc,CAAC;AAG/B,iFAAiF;AAEjF;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,WAAW;IACL,MAAM,CAAS;IACf,iBAAiB,CAAS;IAC1B,kBAAkB,CAAS;IAC3B,UAAU,CAAS;IAEpC,YAAY,MAAsB;QAChC,IAAI,CAAC,MAAM,GAAe,MAAM,CAAC,MAAM,CAAC;QACxC,IAAI,CAAC,iBAAiB,GAAI,MAAM,CAAC,iBAAiB,IAAK,KAAK,CAAC;QAC7D,IAAI,CAAC,kBAAkB,GAAG,MAAM,CAAC,kBAAkB,IAAI,IAAI,CAAC;QAC5D,IAAI,CAAC,UAAU,GAAW,MAAM,CAAC,UAAU,IAAY,EAAE,CAAC;IAC5D,CAAC;IAED,8EAA8E;IAE9E,4FAA4F;IAC5F,KAAK,CAAC,YAAY,CAAC,KAAa;QAC9B,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IAC7C,CAAC;IAED,uFAAuF;IACvF,KAAK,CAAC,cAAc,CAAC,KAAa,EAAE,MAAc;QAChD,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACvC,CAAC;IAED,8EAA8E;IAE9E;;;OAGG;IACH,cAAc,CAAC,IAAc;QAC3B,MAAM,OAAO,GAAG,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;QAEnD,MAAM,WAAW,GAAG,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,EAAE;YACjD,SAAS,EAAE,IAAI,CAAC,iBAAwB;YACxC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;SACzB,CAAC,CAAC;QAEH,MAAM,YAAY,GAAG,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,MAAM,GAAG,UAAU,EAAE;YAC/D,SAAS,EAAE,IAAI,CAAC,kBAAyB;YACzC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;SACzB,CAAC,CAAC;QAEH,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,CAAC;IACvC,CAAC;IAED;;;OAGG;IACH,iBAAiB,CAAC,KAAa;QAC7B,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAQ,CAAC;QACtD,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,GAAG,OAAO,EAAE,CAAC;IAC9D,CAAC;IAED;;;OAGG;IACH,aAAa,CAAC,YAAoB;QAChC,MAAM,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,YAAY,EAAE,IAAI,CAAC,MAAM,GAAG,UAAU,CAAQ,CAAC;QAC1E,OAAO,IAAI,CAAC,cAAc,CAAC,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;IACvE,CAAC;CACF"}
@@ -0,0 +1,41 @@
1
+ import type { SabiAuthConfig } from '../types.js';
2
+ export declare const AUTH_SERVICE: unique symbol;
3
+ /**
4
+ * Registers auth services with the DI container.
5
+ *
6
+ * Works with any DI container that exposes `bindFactory` and `make`.
7
+ *
8
+ * @example
9
+ * // bootstrap/app.ts
10
+ * export class AppAuthServiceProvider extends ServiceProvider {
11
+ * private authProvider!: AuthServiceProvider;
12
+ *
13
+ * register() {
14
+ * this.authProvider = new AuthServiceProvider(this.app, {
15
+ * secret: process.env.APP_SECRET!,
16
+ * accessTokenExpiry: '15m',
17
+ * refreshTokenExpiry: '7d',
18
+ * });
19
+ * this.authProvider.register();
20
+ * }
21
+ *
22
+ * boot() { this.authProvider.boot(); }
23
+ * }
24
+ */
25
+ export declare class AuthServiceProvider {
26
+ private readonly app;
27
+ private readonly config;
28
+ constructor(app: any, config: SabiAuthConfig);
29
+ register(): void;
30
+ /**
31
+ * Returns a ready-to-use `authenticate` middleware bound to the registered AuthService.
32
+ * Call after `boot()` when you need a guard reference.
33
+ *
34
+ * @example
35
+ * const guard = authProvider.guard();
36
+ * Route.group('/api', [guard], () => { ... });
37
+ */
38
+ guard(): (req: import("express").Request, res: import("express").Response, next: import("express").NextFunction) => void;
39
+ boot(): void;
40
+ }
41
+ //# sourceMappingURL=AuthServiceProvider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthServiceProvider.d.ts","sourceRoot":"","sources":["../../src/auth/AuthServiceProvider.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAIlD,eAAO,MAAM,YAAY,eAAwB,CAAC;AAIlD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,mBAAmB;IAE5B,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,GAAG,EAAE,GAAG,EACR,MAAM,EAAE,cAAc;IAGzC,QAAQ,IAAI,IAAI;IAQhB;;;;;;;OAOG;IACH,KAAK;IAKL,IAAI,IAAI,IAAI;CAGb"}
@@ -0,0 +1,54 @@
1
+ import { AuthService } from './AuthService.js';
2
+ import { authenticate } from './AuthGuard.js';
3
+ // ─── Symbols ──────────────────────────────────────────────────────────────────
4
+ export const AUTH_SERVICE = Symbol('AuthService');
5
+ // ─── AuthServiceProvider ──────────────────────────────────────────────────────
6
+ /**
7
+ * Registers auth services with the DI container.
8
+ *
9
+ * Works with any DI container that exposes `bindFactory` and `make`.
10
+ *
11
+ * @example
12
+ * // bootstrap/app.ts
13
+ * export class AppAuthServiceProvider extends ServiceProvider {
14
+ * private authProvider!: AuthServiceProvider;
15
+ *
16
+ * register() {
17
+ * this.authProvider = new AuthServiceProvider(this.app, {
18
+ * secret: process.env.APP_SECRET!,
19
+ * accessTokenExpiry: '15m',
20
+ * refreshTokenExpiry: '7d',
21
+ * });
22
+ * this.authProvider.register();
23
+ * }
24
+ *
25
+ * boot() { this.authProvider.boot(); }
26
+ * }
27
+ */
28
+ export class AuthServiceProvider {
29
+ app;
30
+ config;
31
+ constructor(app, config) {
32
+ this.app = app;
33
+ this.config = config;
34
+ }
35
+ register() {
36
+ this.app.bindFactory(AUTH_SERVICE, () => new AuthService(this.config), true);
37
+ }
38
+ /**
39
+ * Returns a ready-to-use `authenticate` middleware bound to the registered AuthService.
40
+ * Call after `boot()` when you need a guard reference.
41
+ *
42
+ * @example
43
+ * const guard = authProvider.guard();
44
+ * Route.group('/api', [guard], () => { ... });
45
+ */
46
+ guard() {
47
+ const authService = this.app.make(AUTH_SERVICE);
48
+ return authenticate(authService);
49
+ }
50
+ boot() {
51
+ // nothing to boot — AuthService is stateless
52
+ }
53
+ }
54
+ //# sourceMappingURL=AuthServiceProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AuthServiceProvider.js","sourceRoot":"","sources":["../../src/auth/AuthServiceProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C,iFAAiF;AAEjF,MAAM,CAAC,MAAM,YAAY,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;AAElD,iFAAiF;AAEjF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,OAAO,mBAAmB;IAEX;IACA;IAFnB,YACmB,GAAQ,EACR,MAAsB;QADtB,QAAG,GAAH,GAAG,CAAK;QACR,WAAM,GAAN,MAAM,CAAgB;IACtC,CAAC;IAEJ,QAAQ;QACN,IAAI,CAAC,GAAG,CAAC,WAAW,CAClB,YAAY,EACZ,GAAG,EAAE,CAAC,IAAI,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,EAClC,IAAI,CACL,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACH,KAAK;QACH,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,CAAgB,CAAC;QAC/D,OAAO,YAAY,CAAC,WAAW,CAAC,CAAC;IACnC,CAAC;IAED,IAAI;QACF,6CAA6C;IAC/C,CAAC;CACF"}
@@ -0,0 +1,5 @@
1
+ export { AuthService } from './auth/AuthService.js';
2
+ export { authenticate } from './auth/AuthGuard.js';
3
+ export { AuthServiceProvider, AUTH_SERVICE } from './auth/AuthServiceProvider.js';
4
+ export type { SabiAuthConfig, AuthUser, TokenPair } from './types.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAClF,YAAY,EAAE,cAAc,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { AuthService } from './auth/AuthService.js';
2
+ export { authenticate } from './auth/AuthGuard.js';
3
+ export { AuthServiceProvider, AUTH_SERVICE } from './auth/AuthServiceProvider.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Configuration for @owujib/sabi-auth.
3
+ *
4
+ * @example
5
+ * const config: SabiAuthConfig = {
6
+ * secret: process.env.APP_SECRET!,
7
+ * accessTokenExpiry: '15m',
8
+ * refreshTokenExpiry: '7d',
9
+ * };
10
+ */
11
+ export interface SabiAuthConfig {
12
+ /** Secret used to sign tokens. Use a long random string in production. */
13
+ secret: string;
14
+ /** How long access tokens are valid. Default: '15m' */
15
+ accessTokenExpiry?: string;
16
+ /** How long refresh tokens are valid. Default: '7d' */
17
+ refreshTokenExpiry?: string;
18
+ /** bcrypt cost factor. Higher = slower but more secure. Default: 12 */
19
+ saltRounds?: number;
20
+ }
21
+ /**
22
+ * The authenticated user payload stored inside a token and attached to the request.
23
+ * Extend this with your own fields via declaration merging if needed.
24
+ */
25
+ export interface AuthUser {
26
+ id: number | string;
27
+ email: string;
28
+ [key: string]: unknown;
29
+ }
30
+ /**
31
+ * A pair of access and refresh tokens returned on login or token refresh.
32
+ */
33
+ export interface TokenPair {
34
+ accessToken: string;
35
+ refreshToken: string;
36
+ }
37
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,MAAM,WAAW,cAAc;IAC7B,0EAA0E;IAC1E,MAAM,EAAE,MAAM,CAAC;IACf,uDAAuD;IACvD,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,uDAAuD;IACvD,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,uEAAuE;IACvE,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;CACtB"}
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ // ─── Developer-facing types ───────────────────────────────────────────────────
2
+ export {};
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,iFAAiF"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@owujib/sabi-auth",
3
+ "version": "0.0.1",
4
+ "description": "Authentication package for the Sabi framework — JWT tokens, password hashing, and route guards",
5
+ "keywords": [
6
+ "auth",
7
+ "jwt",
8
+ "sabi",
9
+ "typescript",
10
+ "nodejs"
11
+ ],
12
+ "homepage": "https://github.com/owujib/sabi#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/owujib/sabi/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/owujib/sabi.git"
19
+ },
20
+ "license": "ISC",
21
+ "author": "owujib",
22
+ "type": "module",
23
+ "main": "dist/index.js",
24
+ "types": "dist/index.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "import": "./dist/index.js",
28
+ "types": "./dist/index.d.ts"
29
+ }
30
+ },
31
+ "scripts": {
32
+ "build": "tsc -p tsconfig.json",
33
+ "prepublishOnly": "npm run build",
34
+ "publish:public": "npm publish --access public"
35
+ },
36
+ "dependencies": {
37
+ "bcryptjs": "^2.4.3",
38
+ "jsonwebtoken": "^9.0.2"
39
+ },
40
+ "devDependencies": {
41
+ "@types/bcryptjs": "^2.4.6",
42
+ "@types/express": "^5.0.6",
43
+ "@types/jsonwebtoken": "^9.0.7",
44
+ "@types/node": "^20.0.0",
45
+ "typescript": "^5.0.0"
46
+ },
47
+ "peerDependencies": {
48
+ "@owujib/sabi": ">=0.0.1"
49
+ }
50
+ }