@oxyhq/services 0.1.7 → 0.1.8

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.
Files changed (2) hide show
  1. package/README.md +132 -11
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -67,7 +67,7 @@ const getWallet = async (userId) => {
67
67
  #### Using OxyProvider (Recommended)
68
68
 
69
69
  ```typescript
70
- import React from 'react';
70
+ import React, { useState, useEffect } from 'react';
71
71
  import { OxyProvider, useAuth, useUser, useKarma, useWallet } from '@oxyhq/services';
72
72
 
73
73
  // Wrap your app with OxyProvider
@@ -87,26 +87,65 @@ function AuthenticatedApp() {
87
87
  const karma = useKarma();
88
88
  const wallet = useWallet();
89
89
 
90
+ // State for login form
91
+ const [username, setUsername] = useState('');
92
+ const [password, setPassword] = useState('');
93
+ const [authStatus, setAuthStatus] = useState(false);
94
+
95
+ // Check authentication status on component mount and when auth changes
96
+ useEffect(() => {
97
+ const checkAuth = async () => {
98
+ const isAuthenticated = await auth.isAuthenticated();
99
+ setAuthStatus(isAuthenticated);
100
+
101
+ // If authenticated but no user data, fetch the user profile
102
+ if (isAuthenticated && !auth.user) {
103
+ await auth.getCurrentUser();
104
+ }
105
+ };
106
+
107
+ checkAuth();
108
+ }, [auth]);
109
+
90
110
  const handleLogin = async () => {
91
111
  const result = await auth.login({
92
- username: 'user123',
93
- password: 'secure-password'
112
+ username,
113
+ password
94
114
  });
95
115
 
96
116
  if (result.success) {
97
117
  console.log('Logged in successfully');
118
+ // User data should be automatically set from the response
119
+ // which may include a user object directly
98
120
  }
99
121
  };
100
122
 
101
123
  return (
102
124
  <div>
103
- {auth.isAuthenticated ? (
125
+ {authStatus ? (
104
126
  <div>
105
127
  <h2>Welcome, {auth.user?.username}</h2>
106
128
  <button onClick={auth.logout}>Logout</button>
107
129
  </div>
108
130
  ) : (
109
- <button onClick={handleLogin}>Login</button>
131
+ <form onSubmit={(e) => {
132
+ e.preventDefault();
133
+ handleLogin();
134
+ }}>
135
+ <input
136
+ type="text"
137
+ value={username}
138
+ onChange={(e) => setUsername(e.target.value)}
139
+ placeholder="Username"
140
+ />
141
+ <input
142
+ type="password"
143
+ value={password}
144
+ onChange={(e) => setPassword(e.target.value)}
145
+ placeholder="Password"
146
+ />
147
+ <button type="submit">Login</button>
148
+ </form>
110
149
  )}
111
150
  </div>
112
151
  );
@@ -118,7 +157,7 @@ export default App;
118
157
  #### Direct Usage (Without Provider)
119
158
 
120
159
  ```typescript
121
- import React from 'react';
160
+ import React, { useState, useEffect } from 'react';
122
161
  import { useAuth, useUser, useKarma, useWallet } from '@oxyhq/services';
123
162
 
124
163
  function App() {
@@ -147,26 +186,64 @@ function App() {
147
186
  getToken: auth.getToken
148
187
  });
149
188
 
189
+ // State for login form and auth status
190
+ const [username, setUsername] = useState('');
191
+ const [password, setPassword] = useState('');
192
+ const [authStatus, setAuthStatus] = useState(false);
193
+
194
+ // Check authentication status on component mount and when auth changes
195
+ useEffect(() => {
196
+ const checkAuth = async () => {
197
+ const isAuthenticated = await auth.isAuthenticated();
198
+ setAuthStatus(isAuthenticated);
199
+
200
+ // If authenticated but no user data, fetch the user profile
201
+ if (isAuthenticated && !auth.user) {
202
+ await auth.getCurrentUser();
203
+ }
204
+ };
205
+
206
+ checkAuth();
207
+ }, [auth]);
208
+
150
209
  const handleLogin = async () => {
151
210
  const result = await auth.login({
152
- username: 'user123',
153
- password: 'secure-password'
211
+ username,
212
+ password
154
213
  });
155
214
 
156
215
  if (result.success) {
157
216
  console.log('Logged in successfully');
217
+ // User data should be automatically set from the response
158
218
  }
159
219
  };
160
220
 
161
221
  return (
162
222
  <div>
163
- {auth.isAuthenticated ? (
223
+ {authStatus ? (
164
224
  <div>
165
225
  <h2>Welcome, {auth.user?.username}</h2>
166
226
  <button onClick={auth.logout}>Logout</button>
167
227
  </div>
168
228
  ) : (
169
- <button onClick={handleLogin}>Login</button>
229
+ <form onSubmit={(e) => {
230
+ e.preventDefault();
231
+ handleLogin();
232
+ }}>
233
+ <input
234
+ type="text"
235
+ value={username}
236
+ onChange={(e) => setUsername(e.target.value)}
237
+ placeholder="Username"
238
+ />
239
+ <input
240
+ type="password"
241
+ value={password}
242
+ onChange={(e) => setPassword(e.target.value)}
243
+ placeholder="Password"
244
+ />
245
+ <button type="submit">Login</button>
246
+ </form>
170
247
  )}
171
248
  </div>
172
249
  );
@@ -195,4 +272,48 @@ export default App;
195
272
 
196
273
  ## License
197
274
 
198
- MIT
275
+ MIT
276
+
277
+ ## Authentication Changes (v0.1.7)
278
+
279
+ ### Important Updates
280
+
281
+ 1. **Enhanced Login/Register Response Handling**:
282
+ - Both `login()` and `register()` functions now support receiving the user object directly in the API response.
283
+ - If the API returns a user object with the tokens, it will be stored automatically without making an extra `/auth/me` request.
284
+
285
+ 2. **Async Authentication Methods**:
286
+ - The `isAuthenticated()` method is now asynchronous and returns a Promise.
287
+ - Use `await auth.isAuthenticated()` to get the current authentication status.
288
+
289
+ 3. **Consistent Token Storage**:
290
+ - Tokens are stored with consistent keys (`oxy_access_token` and `oxy_refresh_token`).
291
+ - Token handling supports various storage mechanisms including local storage, session storage, and cookies.
292
+
293
+ ### Migration Guide
294
+
295
+ When updating from previous versions, make the following changes to your code:
296
+
297
+ ```typescript
298
+ // Before
299
+ if (auth.isAuthenticated) {
300
+ // User is logged in
301
+ }
302
+
303
+ // After - with async/await
304
+ const checkAuth = async () => {
305
+ if (await auth.isAuthenticated()) {
306
+ // User is logged in
307
+ }
308
+ };
309
+
310
+ // After - with useEffect
311
+ useEffect(() => {
312
+ const checkAuth = async () => {
313
+ const isAuthenticated = await auth.isAuthenticated();
314
+ // Update your component state
315
+ };
316
+
317
+ checkAuth();
318
+ }, [auth]);
319
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxyhq/services",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Authentication, user management, and karma system for Oxy applications",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",