@mateosuarezdev/brpc 1.0.68 → 1.0.70

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,12 @@
1
+ export declare const COOKIE_NAMES: {
2
+ readonly ACCESS_TOKEN: "access_token";
3
+ readonly REFRESH_TOKEN: "refresh_token";
4
+ readonly SESSION_ID: "session_id";
5
+ };
6
+ export declare function setAuthCookies(headers: Headers, tokens: {
7
+ accessToken: string;
8
+ refreshToken: string;
9
+ sessionId: string;
10
+ }, isProd: boolean): void;
11
+ export declare function clearAuthCookies(headers: Headers, isProd: boolean): void;
12
+ export declare function parseCookies(cookieHeader: string | null): Record<string, string>;
@@ -1 +1,3 @@
1
- export {};
1
+ export { AuthService, createAuth } from "./service";
2
+ export { COOKIE_NAMES, setAuthCookies, clearAuthCookies, parseCookies } from "./cookies";
3
+ export type { AppMetadata, AuthConfig, AuthResponse, ContactMethod, DecodedJWT, EmailSignIn, EmailSignUp, IdentityData, IdentityRecord, OTPProvider, OTPRecord, OTPResponse, PhoneSignIn, PhoneSignUp, Session, SessionRecord, SignInMethod, SignUpMethod, SignUpOptions, UserRecord, UserResponse, } from "./types";
@@ -0,0 +1,4 @@
1
+ // @bun
2
+ export{r as setAuthCookies,d as parseCookies,n as createAuth,p as clearAuthCookies,s as COOKIE_NAMES,t as AuthService};
3
+
4
+ //# debugId=6CBC2D2D58A69A8964756E2164756E21
@@ -0,0 +1,89 @@
1
+ import type { AppMetadata, AuthConfig, AuthResponse, ContactMethod, DecodedJWT, IdentityRecord, OTPResponse, Session, SignInMethod, SignUpMethod, SignUpOptions, UserMetadata, UserRecord, UserResponse } from "./types";
2
+ export declare class AuthService<TAppMetadata extends AppMetadata = AppMetadata, TUserMetadata extends UserMetadata = UserMetadata> {
3
+ private userManager;
4
+ private identityManager;
5
+ private sessionManager;
6
+ private otpManager;
7
+ private tokenManager;
8
+ private cache;
9
+ private config;
10
+ constructor(config: AuthConfig<TAppMetadata>);
11
+ initialize(): Promise<void>;
12
+ signUp(method: SignUpMethod<TAppMetadata, TUserMetadata>, req: Request): Promise<AuthResponse<TAppMetadata, TUserMetadata> | OTPResponse>;
13
+ signIn(method: SignInMethod, req: Request): Promise<AuthResponse<TAppMetadata, TUserMetadata> | OTPResponse>;
14
+ verifyOTP(otpId: string, code: string, req: Request): Promise<AuthResponse<TAppMetadata, TUserMetadata>>;
15
+ signOut(sessionId: string): Promise<void>;
16
+ signOutAll(userId: string): Promise<void>;
17
+ signOutAllExcept(userId: string, currentSessionId: string): Promise<void>;
18
+ getUserSessions(userId: string): Promise<Omit<Session, "userId">[]>;
19
+ refreshTokens(refreshToken: string): Promise<{
20
+ accessToken: string;
21
+ refreshToken: string;
22
+ sessionId: string;
23
+ user: UserResponse<TAppMetadata, TUserMetadata>;
24
+ }>;
25
+ verifyToken(authorizationHeader?: string | null): Promise<DecodedJWT<TAppMetadata, TUserMetadata> | null>;
26
+ fastVerifyToken(authorizationHeader?: string | null): Promise<DecodedJWT<TAppMetadata, TUserMetadata> | null>;
27
+ verifyTokenFromCookie(cookieHeader: string | null): Promise<DecodedJWT<TAppMetadata, TUserMetadata> | null>;
28
+ refreshTokenFromCookie(cookieHeader: string | null): Promise<{
29
+ accessToken: string;
30
+ refreshToken: string;
31
+ user: UserResponse<TAppMetadata, TUserMetadata>;
32
+ } | null>;
33
+ getUserById(userId: string): Promise<UserRecord<TAppMetadata, TUserMetadata> | undefined>;
34
+ getUserByContact(contact: ContactMethod): Promise<UserRecord<TAppMetadata, TUserMetadata> | undefined>;
35
+ getUserIdentities(userId: string): Promise<IdentityRecord[]>;
36
+ updateUserPassword(userId: string, password: string): Promise<void>;
37
+ updateAppMetadata(userId: string, appMetadata?: Partial<TAppMetadata>): Promise<void>;
38
+ updateUserMetadata(userId: string, userMetadata?: Partial<TUserMetadata>): Promise<void>;
39
+ changePassword(userId: string, currentPassword: string, newPassword: string): Promise<void>;
40
+ requestPasswordReset(contact: ContactMethod): Promise<OTPResponse>;
41
+ resetPassword(otpId: string, code: string, newPassword: string): Promise<{
42
+ success: true;
43
+ }>;
44
+ adminCreateUser({ email, phone, password, options, }: {
45
+ email?: string;
46
+ phone?: string;
47
+ password?: string;
48
+ options?: SignUpOptions<TAppMetadata, TUserMetadata>;
49
+ }): Promise<UserResponse<TAppMetadata, TUserMetadata>>;
50
+ cacheSession(token: string, session: DecodedJWT<TAppMetadata, TUserMetadata>): void;
51
+ getCachedSession(token: string): DecodedJWT<TAppMetadata, TUserMetadata> | null;
52
+ invalidateCachedSession(token: string): boolean;
53
+ getCacheStats(): {
54
+ size: number;
55
+ valid: number;
56
+ expired: number;
57
+ maxSize: number;
58
+ defaultTTL: number | null;
59
+ entries: {
60
+ key: string;
61
+ expiresIn: number | null;
62
+ age: number;
63
+ }[];
64
+ };
65
+ clearCache(): void;
66
+ isTokenNearExpiry(exp: number): boolean;
67
+ testCreateUserWithSession({ email, phone, password, options, req, }: {
68
+ email?: string;
69
+ phone?: string;
70
+ password?: string;
71
+ options?: SignUpOptions<TAppMetadata, TUserMetadata>;
72
+ req: Request;
73
+ }): Promise<AuthResponse<TAppMetadata, TUserMetadata>>;
74
+ private startPasswordlessSignUp;
75
+ private startPasswordlessSignIn;
76
+ private completePasswordSignUp;
77
+ private completePasswordSignIn;
78
+ private completePasswordlessSignUp;
79
+ private completePasswordlessSignIn;
80
+ private createAuthSession;
81
+ private validateSignUpMethod;
82
+ private validateSignInMethod;
83
+ private validateEmail;
84
+ private normalizePhoneNumber;
85
+ private extractContact;
86
+ private extractIdentityKey;
87
+ private initializeCleanup;
88
+ }
89
+ export declare function createAuth<TAppMetadata extends AppMetadata = AppMetadata, TUserMetadata extends UserMetadata = UserMetadata>(config: AuthConfig<TAppMetadata>): AuthService<TAppMetadata, TUserMetadata>;
@@ -1 +1,153 @@
1
- export {};
1
+ import type { RecordId, Surreal } from "surrealdb";
2
+ export type AppMetadata = {
3
+ roles: string[];
4
+ [key: string]: unknown;
5
+ };
6
+ export type UserMetadata = {
7
+ name: string | null;
8
+ app_theme: string | null;
9
+ preferred_language: string | null;
10
+ };
11
+ export type UserRecord<TAppMetadata extends AppMetadata = AppMetadata, TUserMetadata extends UserMetadata = UserMetadata> = {
12
+ id: RecordId;
13
+ created_at: string;
14
+ updated_at: string;
15
+ email: string | null;
16
+ email_verified: boolean;
17
+ phone_verified: boolean;
18
+ phone: string | null;
19
+ app_metadata: TAppMetadata;
20
+ user_metadata: TUserMetadata;
21
+ last_sign_in_at: string | null;
22
+ };
23
+ export type IdentityData = {
24
+ password_hash?: string;
25
+ avatar_url?: string;
26
+ [key: string]: unknown;
27
+ };
28
+ export type IdentityRecord = {
29
+ id: RecordId;
30
+ user_id: RecordId;
31
+ provider: string;
32
+ provider_id: string;
33
+ identity_data: IdentityData;
34
+ created_at: string;
35
+ updated_at: string;
36
+ };
37
+ export type SessionRecord = {
38
+ id: RecordId;
39
+ user_id: RecordId;
40
+ ip_address: string | null;
41
+ user_agent: string | null;
42
+ invalidated_at: string | null;
43
+ created_at: string;
44
+ };
45
+ export type OTPRecord = {
46
+ id: RecordId;
47
+ user_id: RecordId | null;
48
+ email: string | null;
49
+ phone: string | null;
50
+ code: string;
51
+ hashed_code: string;
52
+ type: "signup" | "signin" | "password_reset";
53
+ signup_options: SignUpOptions | null;
54
+ expires_at: string;
55
+ verified_at: string | null;
56
+ attempts: number;
57
+ created_at: string;
58
+ };
59
+ export type Session = {
60
+ id: string;
61
+ userId: string;
62
+ ipAddress: string | null;
63
+ userAgent: string | null;
64
+ invalidatedAt?: string | null;
65
+ };
66
+ export type UserResponse<TAppMetadata extends AppMetadata = AppMetadata, TUserMetadata extends UserMetadata = UserMetadata> = {
67
+ id: string;
68
+ email: string | null;
69
+ phone: string | null;
70
+ appMetadata: TAppMetadata;
71
+ userMetadata: TUserMetadata;
72
+ };
73
+ export type AuthResponse<TAppMetadata extends AppMetadata = AppMetadata, TUserMetadata extends UserMetadata = UserMetadata> = {
74
+ user: UserResponse<TAppMetadata, TUserMetadata>;
75
+ session: Session;
76
+ accessToken: string;
77
+ refreshToken: string;
78
+ };
79
+ export type OTPResponse = {
80
+ otpId: string;
81
+ expiresAt: string;
82
+ message: string;
83
+ };
84
+ export type DecodedJWT<TAppMetadata extends AppMetadata = AppMetadata, TUserMetadata extends UserMetadata = UserMetadata> = {
85
+ sub: string;
86
+ sessionId: string;
87
+ appMetadata: TAppMetadata;
88
+ userMetadata: TUserMetadata;
89
+ exp: number;
90
+ };
91
+ export type ContactMethod = {
92
+ email?: string;
93
+ phone?: string;
94
+ };
95
+ export type SignUpOptions<TAppMetadata extends AppMetadata = AppMetadata, TUserMetadata extends UserMetadata = UserMetadata> = {
96
+ appMetadata?: Partial<TAppMetadata>;
97
+ userMetadata?: Partial<TUserMetadata>;
98
+ };
99
+ export type EmailSignUp<TAppMetadata extends AppMetadata = AppMetadata, TUserMetadata extends UserMetadata = UserMetadata> = {
100
+ type: "email";
101
+ email: string;
102
+ password?: string;
103
+ passwordless?: boolean;
104
+ options?: SignUpOptions<TAppMetadata, TUserMetadata>;
105
+ };
106
+ export type PhoneSignUp<TAppMetadata extends AppMetadata = AppMetadata, TUserMetadata extends UserMetadata = UserMetadata> = {
107
+ type: "phone";
108
+ phone: string;
109
+ password?: string;
110
+ passwordless?: boolean;
111
+ options?: SignUpOptions<TAppMetadata, TUserMetadata>;
112
+ };
113
+ export type EmailSignIn = {
114
+ type: "email";
115
+ email: string;
116
+ password?: string;
117
+ passwordless?: boolean;
118
+ };
119
+ export type PhoneSignIn = {
120
+ type: "phone";
121
+ phone: string;
122
+ password?: string;
123
+ passwordless?: boolean;
124
+ };
125
+ export type SignUpMethod<TAppMetadata extends AppMetadata = AppMetadata, TUserMetadata extends UserMetadata = UserMetadata> = EmailSignUp<TAppMetadata, TUserMetadata> | PhoneSignUp<TAppMetadata, TUserMetadata>;
126
+ export type SignInMethod = EmailSignIn | PhoneSignIn;
127
+ export interface OTPProvider {
128
+ sendEmailOTP(params: {
129
+ email: string;
130
+ code: string;
131
+ }): Promise<any>;
132
+ sendSMSOTP(params: {
133
+ phone: string;
134
+ code: string;
135
+ }): Promise<any>;
136
+ sendWhatsAppOTP(params: {
137
+ phone: string;
138
+ code: string;
139
+ }): Promise<any>;
140
+ }
141
+ export type AuthConfig<TAppMetadata extends AppMetadata = AppMetadata> = {
142
+ db: Surreal;
143
+ secrets: {
144
+ jwt: string;
145
+ jwtRefresh: string;
146
+ };
147
+ defaultSignupRoles: TAppMetadata["roles"];
148
+ otpProvider?: OTPProvider;
149
+ accessTokenExpiry?: string;
150
+ isProd?: boolean;
151
+ debug?: boolean;
152
+ disableCleanupCron?: boolean;
153
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mateosuarezdev/brpc",
3
- "version": "1.0.68",
3
+ "version": "1.0.70",
4
4
  "description": "A Type-Safe, Flexible Web application framework for Bun",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -31,6 +31,11 @@
31
31
  "types": "./dist/cache/index.d.ts",
32
32
  "import": "./dist/cache/index.js",
33
33
  "require": "./dist/cache/index.cjs"
34
+ },
35
+ "./auth": {
36
+ "types": "./dist/auth/index.d.ts",
37
+ "import": "./dist/auth/index.js",
38
+ "require": "./dist/auth/index.cjs"
34
39
  }
35
40
  },
36
41
  "files": [
@@ -73,12 +78,25 @@
73
78
  "bun": ">=1.2.0"
74
79
  },
75
80
  "peerDependencies": {
81
+ "bcryptjs": ">=2.4.0",
82
+ "jsonwebtoken": ">=9.0.0",
83
+ "node-cron": ">=3.0.0",
76
84
  "react": "^19.0.0",
77
85
  "react-dom": "^19.0.0",
78
86
  "sharp": "^0.34.4",
87
+ "surrealdb": ">=2.0.0",
79
88
  "zod": "^3.23.8"
80
89
  },
81
90
  "peerDependenciesMeta": {
91
+ "bcryptjs": {
92
+ "optional": true
93
+ },
94
+ "jsonwebtoken": {
95
+ "optional": true
96
+ },
97
+ "node-cron": {
98
+ "optional": true
99
+ },
82
100
  "react": {
83
101
  "optional": true
84
102
  },
@@ -88,16 +106,26 @@
88
106
  "sharp": {
89
107
  "optional": true
90
108
  },
109
+ "surrealdb": {
110
+ "optional": true
111
+ },
91
112
  "zod": {
92
113
  "optional": false
93
114
  }
94
115
  },
95
116
  "devDependencies": {
117
+ "@types/bcryptjs": "^2.4.6",
96
118
  "@types/bun": "^1.2.23",
119
+ "@types/jsonwebtoken": "^9.0.0",
120
+ "@types/node-cron": "^3.0.0",
97
121
  "@types/react": "^19.2.2",
98
122
  "@types/react-dom": "^19.2.2",
123
+ "bcryptjs": "^2.4.2",
124
+ "jsonwebtoken": "^9.0.0",
125
+ "node-cron": "^3.0.0",
99
126
  "rimraf": "^6.0.1",
100
127
  "sharp": "^0.34.4",
128
+ "surrealdb": "^2.0.0",
101
129
  "typescript": "^5.9.3",
102
130
  "zod": "^3.23.8"
103
131
  },
@@ -1 +0,0 @@
1
- export {};