@marcwelti/mw-core 0.3.1 → 0.4.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.
@@ -1,27 +1,22 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import React, { ReactNode } from 'react';
3
3
  import { User, UserCredential } from 'firebase/auth';
4
- import { UserRole } from '../types/index.mjs';
4
+ import { ExtendedUser, UserRole, Subscription } from '../types/index.mjs';
5
5
  import 'firebase/firestore';
6
6
 
7
7
  /**
8
8
  * User data from Firestore (includes role and additional profile data)
9
9
  */
10
- interface UserData {
10
+ interface UserData extends ExtendedUser {
11
11
  id: string;
12
- uid: string;
13
- email: string | null;
14
- displayName: string | null;
15
- photoURL: string | null;
16
- role: UserRole | null;
17
- phoneNumber?: string | null;
18
- isActive?: boolean;
19
12
  }
20
13
  interface AuthContextValue {
21
14
  /** The current authenticated Firebase user, or null if not authenticated */
22
15
  user: User | null;
23
- /** User data from Firestore (includes role) */
16
+ /** Extended user data from Firestore (includes role, KYC, phone, etc.) */
24
17
  userData: UserData | null;
18
+ /** Raw Firebase user object */
19
+ firebaseUser: User | null;
25
20
  /** The user's role from Firestore */
26
21
  role: UserRole | null;
27
22
  /** Whether the auth state is still loading */
@@ -36,7 +31,11 @@ interface AuthContextValue {
36
31
  isStaff: boolean;
37
32
  /** Whether the user is the owner */
38
33
  isOwner: boolean;
39
- /** Any auth error that occurred */
34
+ /** User subscription data */
35
+ subscription: Subscription | null;
36
+ /** Any auth error that occurred (e.g., "SIGNUP_DISABLED") */
37
+ authError: string | null;
38
+ /** Any general error that occurred */
40
39
  error: Error | null;
41
40
  /** Sign in with email and password */
42
41
  signIn: (email: string, password: string) => Promise<UserCredential>;
@@ -45,18 +44,40 @@ interface AuthContextValue {
45
44
  /** Sign out the current user */
46
45
  signOut: () => Promise<void>;
47
46
  /** Sign in with Google */
48
- signInWithGoogle: () => Promise<UserCredential>;
47
+ signInWithGoogle: () => Promise<UserCredential | void>;
49
48
  /** Send a password reset email */
50
49
  resetPassword: (email: string) => Promise<void>;
51
- /** Update the user's profile */
50
+ /** Update the user's profile (displayName, photoURL) */
52
51
  updateProfile: (profile: {
53
52
  displayName?: string;
54
53
  photoURL?: string;
55
54
  }) => Promise<void>;
56
55
  /** Send email verification */
57
56
  sendEmailVerification: () => Promise<void>;
57
+ /** Update user password (requires current password) */
58
+ updateUserPassword: (currentPassword: string, newPassword: string) => Promise<void>;
59
+ /** Update user email (requires current password) */
60
+ updateUserEmail: (newEmail: string, currentPassword: string) => Promise<void>;
61
+ /** Update user phone number */
62
+ updateUserPhone: (phoneNumber: string, phoneVerified: boolean) => Promise<void>;
63
+ /** Update user Amicus ID */
64
+ updateUserAmicusId: (amicusId: string) => Promise<void>;
65
+ /** Update user profile data (address, plz, city) */
66
+ updateUserProfile: (data: {
67
+ address?: string;
68
+ plz?: string;
69
+ city?: string;
70
+ }) => Promise<void>;
71
+ /** Update another user's role (admin only) */
72
+ updateUserRole: (targetUserId: string, newRole: UserRole) => Promise<void>;
73
+ /** Delete the user's account */
74
+ deleteAccount: (currentPassword?: string) => Promise<boolean>;
75
+ /** Refresh user data from Firebase and Firestore */
76
+ refreshUser: () => Promise<void>;
58
77
  /** Clear any auth errors */
59
78
  clearError: () => void;
79
+ /** Clear auth error */
80
+ clearAuthError: () => void;
60
81
  /** Refresh user data from Firestore */
61
82
  refreshUserData: () => void;
62
83
  }
@@ -70,56 +91,36 @@ interface AuthProviderProps {
70
91
  loadingComponent?: ReactNode;
71
92
  /** Optional: Collection path for user documents (default: 'users') */
72
93
  usersCollection?: string;
94
+ /** Enable cross-domain session cookies (default: false) */
95
+ enableSessionCookies?: boolean;
96
+ /** Session login endpoint (default: '/api/auth/login') */
97
+ sessionLoginEndpoint?: string;
98
+ /** Session logout endpoint (default: '/api/auth/logout') */
99
+ sessionLogoutEndpoint?: string;
100
+ /** Enable signup blocking when NEXT_PUBLIC_SIGNUP_DISABLED=true (default: false) */
101
+ enableSignupBlocking?: boolean;
102
+ /** Check email endpoint for email change (default: '/api/user/check-email') */
103
+ checkEmailEndpoint?: string;
104
+ /** Delete account endpoint (default: '/api/user/delete-account') */
105
+ deleteAccountEndpoint?: string;
106
+ /** Public pages that allow state updates on logout (default: ['/', '/login']) */
107
+ publicPagesForLogout?: string[];
108
+ /** Auto-create user document if it doesn't exist (default: true) */
109
+ autoCreateUserDocument?: boolean;
73
110
  }
74
111
  /**
75
112
  * Auth Provider component that wraps your app and provides auth context
76
113
  * Automatically fetches user data from Firestore including role
77
- *
78
- * @example
79
- * ```tsx
80
- * // app/layout.tsx
81
- * import { AuthProvider } from '@marcwelti/mw-core';
82
- *
83
- * export default function RootLayout({ children }) {
84
- * return (
85
- * <html>
86
- * <body>
87
- * <AuthProvider>
88
- * {children}
89
- * </AuthProvider>
90
- * </body>
91
- * </html>
92
- * );
93
- * }
94
- * ```
114
+ * Supports user document creation, session cookies, and signup blocking
95
115
  */
96
- declare function AuthProvider({ children, onAuthStateChange, onUserDataChange, loadingComponent, usersCollection, }: AuthProviderProps): react_jsx_runtime.JSX.Element;
116
+ declare function AuthProvider({ children, onAuthStateChange, onUserDataChange, loadingComponent, usersCollection, enableSessionCookies, sessionLoginEndpoint, sessionLogoutEndpoint, enableSignupBlocking, checkEmailEndpoint, deleteAccountEndpoint, publicPagesForLogout, autoCreateUserDocument, }: AuthProviderProps): react_jsx_runtime.JSX.Element;
97
117
  /**
98
118
  * Hook to access auth context
99
119
  * Must be used within an AuthProvider
100
- *
101
- * @example
102
- * ```tsx
103
- * function MyComponent() {
104
- * const { user, signOut, isAuthenticated } = useAuthContext();
105
- *
106
- * if (!isAuthenticated) {
107
- * return <LoginForm />;
108
- * }
109
- *
110
- * return (
111
- * <div>
112
- * <p>Welcome, {user?.displayName}</p>
113
- * <button onClick={signOut}>Sign Out</button>
114
- * </div>
115
- * );
116
- * }
117
- * ```
118
120
  */
119
121
  declare function useAuthContext(): AuthContextValue;
120
122
  /**
121
123
  * HOC for protecting routes that require authentication
122
- * Redirects to login or shows loading state
123
124
  */
124
125
  declare function withAuth<P extends object>(WrappedComponent: React.ComponentType<P>, options?: {
125
126
  LoadingComponent?: React.ComponentType;
@@ -1,27 +1,22 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import React, { ReactNode } from 'react';
3
3
  import { User, UserCredential } from 'firebase/auth';
4
- import { UserRole } from '../types/index.js';
4
+ import { ExtendedUser, UserRole, Subscription } from '../types/index.js';
5
5
  import 'firebase/firestore';
6
6
 
7
7
  /**
8
8
  * User data from Firestore (includes role and additional profile data)
9
9
  */
10
- interface UserData {
10
+ interface UserData extends ExtendedUser {
11
11
  id: string;
12
- uid: string;
13
- email: string | null;
14
- displayName: string | null;
15
- photoURL: string | null;
16
- role: UserRole | null;
17
- phoneNumber?: string | null;
18
- isActive?: boolean;
19
12
  }
20
13
  interface AuthContextValue {
21
14
  /** The current authenticated Firebase user, or null if not authenticated */
22
15
  user: User | null;
23
- /** User data from Firestore (includes role) */
16
+ /** Extended user data from Firestore (includes role, KYC, phone, etc.) */
24
17
  userData: UserData | null;
18
+ /** Raw Firebase user object */
19
+ firebaseUser: User | null;
25
20
  /** The user's role from Firestore */
26
21
  role: UserRole | null;
27
22
  /** Whether the auth state is still loading */
@@ -36,7 +31,11 @@ interface AuthContextValue {
36
31
  isStaff: boolean;
37
32
  /** Whether the user is the owner */
38
33
  isOwner: boolean;
39
- /** Any auth error that occurred */
34
+ /** User subscription data */
35
+ subscription: Subscription | null;
36
+ /** Any auth error that occurred (e.g., "SIGNUP_DISABLED") */
37
+ authError: string | null;
38
+ /** Any general error that occurred */
40
39
  error: Error | null;
41
40
  /** Sign in with email and password */
42
41
  signIn: (email: string, password: string) => Promise<UserCredential>;
@@ -45,18 +44,40 @@ interface AuthContextValue {
45
44
  /** Sign out the current user */
46
45
  signOut: () => Promise<void>;
47
46
  /** Sign in with Google */
48
- signInWithGoogle: () => Promise<UserCredential>;
47
+ signInWithGoogle: () => Promise<UserCredential | void>;
49
48
  /** Send a password reset email */
50
49
  resetPassword: (email: string) => Promise<void>;
51
- /** Update the user's profile */
50
+ /** Update the user's profile (displayName, photoURL) */
52
51
  updateProfile: (profile: {
53
52
  displayName?: string;
54
53
  photoURL?: string;
55
54
  }) => Promise<void>;
56
55
  /** Send email verification */
57
56
  sendEmailVerification: () => Promise<void>;
57
+ /** Update user password (requires current password) */
58
+ updateUserPassword: (currentPassword: string, newPassword: string) => Promise<void>;
59
+ /** Update user email (requires current password) */
60
+ updateUserEmail: (newEmail: string, currentPassword: string) => Promise<void>;
61
+ /** Update user phone number */
62
+ updateUserPhone: (phoneNumber: string, phoneVerified: boolean) => Promise<void>;
63
+ /** Update user Amicus ID */
64
+ updateUserAmicusId: (amicusId: string) => Promise<void>;
65
+ /** Update user profile data (address, plz, city) */
66
+ updateUserProfile: (data: {
67
+ address?: string;
68
+ plz?: string;
69
+ city?: string;
70
+ }) => Promise<void>;
71
+ /** Update another user's role (admin only) */
72
+ updateUserRole: (targetUserId: string, newRole: UserRole) => Promise<void>;
73
+ /** Delete the user's account */
74
+ deleteAccount: (currentPassword?: string) => Promise<boolean>;
75
+ /** Refresh user data from Firebase and Firestore */
76
+ refreshUser: () => Promise<void>;
58
77
  /** Clear any auth errors */
59
78
  clearError: () => void;
79
+ /** Clear auth error */
80
+ clearAuthError: () => void;
60
81
  /** Refresh user data from Firestore */
61
82
  refreshUserData: () => void;
62
83
  }
@@ -70,56 +91,36 @@ interface AuthProviderProps {
70
91
  loadingComponent?: ReactNode;
71
92
  /** Optional: Collection path for user documents (default: 'users') */
72
93
  usersCollection?: string;
94
+ /** Enable cross-domain session cookies (default: false) */
95
+ enableSessionCookies?: boolean;
96
+ /** Session login endpoint (default: '/api/auth/login') */
97
+ sessionLoginEndpoint?: string;
98
+ /** Session logout endpoint (default: '/api/auth/logout') */
99
+ sessionLogoutEndpoint?: string;
100
+ /** Enable signup blocking when NEXT_PUBLIC_SIGNUP_DISABLED=true (default: false) */
101
+ enableSignupBlocking?: boolean;
102
+ /** Check email endpoint for email change (default: '/api/user/check-email') */
103
+ checkEmailEndpoint?: string;
104
+ /** Delete account endpoint (default: '/api/user/delete-account') */
105
+ deleteAccountEndpoint?: string;
106
+ /** Public pages that allow state updates on logout (default: ['/', '/login']) */
107
+ publicPagesForLogout?: string[];
108
+ /** Auto-create user document if it doesn't exist (default: true) */
109
+ autoCreateUserDocument?: boolean;
73
110
  }
74
111
  /**
75
112
  * Auth Provider component that wraps your app and provides auth context
76
113
  * Automatically fetches user data from Firestore including role
77
- *
78
- * @example
79
- * ```tsx
80
- * // app/layout.tsx
81
- * import { AuthProvider } from '@marcwelti/mw-core';
82
- *
83
- * export default function RootLayout({ children }) {
84
- * return (
85
- * <html>
86
- * <body>
87
- * <AuthProvider>
88
- * {children}
89
- * </AuthProvider>
90
- * </body>
91
- * </html>
92
- * );
93
- * }
94
- * ```
114
+ * Supports user document creation, session cookies, and signup blocking
95
115
  */
96
- declare function AuthProvider({ children, onAuthStateChange, onUserDataChange, loadingComponent, usersCollection, }: AuthProviderProps): react_jsx_runtime.JSX.Element;
116
+ declare function AuthProvider({ children, onAuthStateChange, onUserDataChange, loadingComponent, usersCollection, enableSessionCookies, sessionLoginEndpoint, sessionLogoutEndpoint, enableSignupBlocking, checkEmailEndpoint, deleteAccountEndpoint, publicPagesForLogout, autoCreateUserDocument, }: AuthProviderProps): react_jsx_runtime.JSX.Element;
97
117
  /**
98
118
  * Hook to access auth context
99
119
  * Must be used within an AuthProvider
100
- *
101
- * @example
102
- * ```tsx
103
- * function MyComponent() {
104
- * const { user, signOut, isAuthenticated } = useAuthContext();
105
- *
106
- * if (!isAuthenticated) {
107
- * return <LoginForm />;
108
- * }
109
- *
110
- * return (
111
- * <div>
112
- * <p>Welcome, {user?.displayName}</p>
113
- * <button onClick={signOut}>Sign Out</button>
114
- * </div>
115
- * );
116
- * }
117
- * ```
118
120
  */
119
121
  declare function useAuthContext(): AuthContextValue;
120
122
  /**
121
123
  * HOC for protecting routes that require authentication
122
- * Redirects to login or shows loading state
123
124
  */
124
125
  declare function withAuth<P extends object>(WrappedComponent: React.ComponentType<P>, options?: {
125
126
  LoadingComponent?: React.ComponentType;