@thetechfossil/auth2 1.2.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/README.md ADDED
@@ -0,0 +1,218 @@
1
+ # Auth SDK
2
+
3
+ A lightweight, easy-to-use authentication SDK for integrating with the Auth backend service. Works with both Node.js and React/Next.js applications.
4
+
5
+ ## Features
6
+
7
+ - Dual authentication methods (email/password or email/OTP)
8
+ - Automatic token management
9
+ - React hooks for easy integration
10
+ - Pre-built UI components for login, registration, OTP verification, and email verification
11
+ - TypeScript support
12
+ - Works with both Node.js and browser environments
13
+ - Supports custom frontend base URLs for email verification links
14
+ - **Embedded styling** - No separate CSS imports required!
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install @auth/sdk
20
+ # or
21
+ yarn add @auth/sdk
22
+ # or
23
+ pnpm add @auth/sdk
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ ### React/Next.js Integration
29
+
30
+ ```tsx
31
+ import { react } from '@auth/sdk';
32
+ const { useAuth } = react;
33
+
34
+ function App() {
35
+ const { user, isAuthenticated, register, login, verify, logout } = useAuth({
36
+ baseUrl: 'http://localhost:7000', // Your auth service URL
37
+ localStorageKey: 'auth_token' // Optional, defaults to 'auth_token'
38
+ });
39
+
40
+ const handleRegister = async () => {
41
+ try {
42
+ // The SDK automatically detects the frontend base URL
43
+ // You can optionally override it by passing frontendBaseUrl
44
+ const response = await register({
45
+ name: 'John Doe',
46
+ email: 'user@example.com',
47
+ password: 'securepassword'
48
+ // frontendBaseUrl: 'https://myapp.com' // Optional: custom base URL for email verification links
49
+ });
50
+ console.log('Registration successful:', response.message);
51
+ } catch (error) {
52
+ console.error('Registration failed:', error);
53
+ }
54
+ };
55
+
56
+ // Email/Password Login
57
+ const handlePasswordLogin = async () => {
58
+ try {
59
+ const response = await login({
60
+ email: 'user@example.com',
61
+ password: 'securepassword'
62
+ });
63
+ console.log('Login successful:', response.message);
64
+ } catch (error) {
65
+ console.error('Login failed:', error);
66
+ }
67
+ };
68
+
69
+ // Email/OTP Login - Step 1: Request OTP
70
+ const handleRequestOtp = async () => {
71
+ try {
72
+ const response = await login({ email: 'user@example.com' });
73
+ console.log('OTP sent:', response.message);
74
+ } catch (error) {
75
+ console.error('Login failed:', error);
76
+ }
77
+ };
78
+
79
+ // Email/OTP Login - Step 2: Verify OTP
80
+ const handleVerifyOtp = async () => {
81
+ try {
82
+ const response = await login({
83
+ email: 'user@example.com',
84
+ otp: '123456'
85
+ });
86
+ console.log('Verification successful:', response.message);
87
+ } catch (error) {
88
+ console.error('Verification failed:', error);
89
+ }
90
+ };
91
+
92
+ if (isAuthenticated) {
93
+ return (
94
+ <div>
95
+ <h1>Welcome, {user?.name}!</h1>
96
+ <button onClick={logout}>Logout</button>
97
+ </div>
98
+ );
99
+ }
100
+
101
+ return (
102
+ <div>
103
+ <button onClick={handleRegister}>Register</button>
104
+ <button onClick={handlePasswordLogin}>Login with Password</button>
105
+ <button onClick={handleRequestOtp}>Request OTP</button>
106
+ <button onClick={handleVerifyOtp}>Verify OTP</button>
107
+ </div>
108
+ );
109
+ }
110
+ ```
111
+
112
+ ### Node.js Integration
113
+
114
+ ```ts
115
+ import { node } from '@auth/sdk';
116
+ const { AuthClient } = node;
117
+
118
+ const authClient = new AuthClient({
119
+ baseUrl: 'http://localhost:7000'
120
+ });
121
+
122
+ async function register() {
123
+ try {
124
+ const response = await authClient.register({
125
+ name: 'John Doe',
126
+ email: 'user@example.com',
127
+ password: 'securepassword',
128
+ frontendBaseUrl: 'https://myapp.com' // Optional: custom base URL for email verification links
129
+ });
130
+ console.log('Registration successful:', response.message);
131
+ } catch (error) {
132
+ console.error('Registration failed:', error);
133
+ }
134
+ }
135
+
136
+ // Email/Password Login
137
+ async function passwordLogin() {
138
+ try {
139
+ const response = await authClient.login({
140
+ email: 'user@example.com',
141
+ password: 'securepassword'
142
+ });
143
+ console.log('Login successful:', response.message);
144
+ // Save token manually in Node.js environment
145
+ const token = response.token;
146
+ } catch (error) {
147
+ console.error('Login failed:', error);
148
+ }
149
+ }
150
+
151
+ // Email/OTP Login - Step 1: Request OTP
152
+ async function requestOtp() {
153
+ try {
154
+ const response = await authClient.login({ email: 'user@example.com' });
155
+ console.log('OTP sent:', response.message);
156
+ } catch (error) {
157
+ console.error('Login failed:', error);
158
+ }
159
+ }
160
+
161
+ // Email/OTP Login - Step 2: Verify OTP
162
+ async function verifyOtp() {
163
+ try {
164
+ const response = await authClient.login({
165
+ email: 'user@example.com',
166
+ otp: '123456'
167
+ });
168
+
169
+ if (response.success) {
170
+ console.log('Verification successful:', response.message);
171
+ // Save token manually in Node.js environment
172
+ const token = response.token;
173
+ }
174
+ } catch (error) {
175
+ console.error('Verification failed:', error);
176
+ }
177
+ }
178
+ ```
179
+
180
+ ### Pre-built UI Components
181
+
182
+ The SDK includes pre-built UI components for common authentication flows with embedded styling:
183
+
184
+ ```tsx
185
+ import { react } from '@auth/sdk';
186
+ const { AuthFlow } = react;
187
+
188
+ function LoginPage() {
189
+ const handleAuthComplete = () => {
190
+ // Redirect to dashboard
191
+ console.log('Authentication complete!');
192
+ };
193
+
194
+ return (
195
+ <AuthFlow onAuthComplete={handleAuthComplete} />
196
+ );
197
+ }
198
+ ```
199
+
200
+ You can also use individual components:
201
+
202
+ ```tsx
203
+ import { react } from '@auth/sdk';
204
+ const { LoginForm, RegisterForm, OtpForm } = react;
205
+
206
+ function LoginPage() {
207
+ return (
208
+ <div>
209
+ <LoginForm
210
+ onLoginSuccess={(email) => console.log('OTP sent to:', email)}
211
+ onRegisterClick={() => console.log('Navigate to registration')}
212
+ />
213
+ </div>
214
+ );
215
+ }
216
+ ```
217
+
218
+ **Note:** As of version 1.2.0, all styling is embedded directly in the components. No separate CSS imports are required!
@@ -0,0 +1,62 @@
1
+ import React from 'react';
2
+
3
+ interface LoginFormProps {
4
+ onSuccess?: (response: {
5
+ message: string;
6
+ email?: string;
7
+ token?: string;
8
+ success: boolean;
9
+ user?: any;
10
+ }) => void;
11
+ onLoginSuccess?: (email: string, needsOtpVerification: boolean) => void;
12
+ onRegisterClick?: () => void;
13
+ showRegisterLink?: boolean;
14
+ config?: {
15
+ baseUrl?: string;
16
+ };
17
+ oauthProviders?: Array<'google' | 'github'>;
18
+ showOAuthButtons?: boolean;
19
+ }
20
+ declare const LoginForm: React.FC<LoginFormProps>;
21
+
22
+ interface AuthConfig {
23
+ baseUrl: string;
24
+ localStorageKey?: string;
25
+ token?: string;
26
+ csrfEnabled?: boolean;
27
+ }
28
+
29
+ interface RegisterFormProps {
30
+ onRegisterSuccess?: () => void;
31
+ onLoginClick?: () => void;
32
+ showLoginLink?: boolean;
33
+ authConfig?: AuthConfig;
34
+ oauthProviders?: Array<'google' | 'github'>;
35
+ showOAuthButtons?: boolean;
36
+ }
37
+ declare const RegisterForm: React.FC<RegisterFormProps>;
38
+
39
+ interface OtpFormProps {
40
+ email: string;
41
+ onVerifySuccess?: () => void;
42
+ onBackToLogin?: () => void;
43
+ }
44
+ declare const OtpForm: React.FC<OtpFormProps>;
45
+
46
+ type AuthStep = 'login' | 'register' | 'otp';
47
+ interface AuthFlowProps {
48
+ onAuthComplete?: () => void;
49
+ initialStep?: AuthStep;
50
+ showTitle?: boolean;
51
+ }
52
+ declare const AuthFlow: React.FC<AuthFlowProps>;
53
+
54
+ interface EmailVerificationPageProps {
55
+ token: string;
56
+ onVerificationSuccess?: () => void;
57
+ onVerificationError?: (error: string) => void;
58
+ baseUrl?: string;
59
+ }
60
+ declare const EmailVerificationPage: React.FC<EmailVerificationPageProps>;
61
+
62
+ export { AuthFlow, EmailVerificationPage, LoginForm, OtpForm, RegisterForm };