@blinkdotnew/sdk 0.14.9 → 0.14.10
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 +70 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -187,9 +187,13 @@ await blink.auth.updateMe({ displayName: 'New Name' })
|
|
|
187
187
|
blink.auth.setToken(jwt, persist?)
|
|
188
188
|
const isAuth = blink.auth.isAuthenticated()
|
|
189
189
|
|
|
190
|
-
// Auth state listener
|
|
190
|
+
// Auth state listener (REQUIRED for React apps!)
|
|
191
191
|
const unsubscribe = blink.auth.onAuthStateChanged((state) => {
|
|
192
192
|
console.log('Auth state:', state)
|
|
193
|
+
// state.user - current user or null
|
|
194
|
+
// state.isLoading - true while auth is initializing
|
|
195
|
+
// state.isAuthenticated - true if user is logged in
|
|
196
|
+
// state.tokens - current auth tokens
|
|
193
197
|
})
|
|
194
198
|
```
|
|
195
199
|
|
|
@@ -1390,6 +1394,44 @@ function MyRealtimeComponent() {
|
|
|
1390
1394
|
|
|
1391
1395
|
### React
|
|
1392
1396
|
|
|
1397
|
+
**⚠️ Critical: Always Use Auth State Listener, Never One-Time Checks**
|
|
1398
|
+
|
|
1399
|
+
The most common authentication mistake is checking auth status once instead of listening to changes:
|
|
1400
|
+
|
|
1401
|
+
```typescript
|
|
1402
|
+
// ❌ WRONG - One-time check misses auth completion
|
|
1403
|
+
useEffect(() => {
|
|
1404
|
+
const checkAuth = async () => {
|
|
1405
|
+
try {
|
|
1406
|
+
const userData = await blink.auth.me()
|
|
1407
|
+
setUser(userData)
|
|
1408
|
+
} catch (error) {
|
|
1409
|
+
console.error('Auth check failed:', error)
|
|
1410
|
+
} finally {
|
|
1411
|
+
setLoading(false)
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
checkAuth() // Only runs once - misses when auth completes later!
|
|
1415
|
+
}, [])
|
|
1416
|
+
|
|
1417
|
+
// ✅ CORRECT - Listen to auth state changes
|
|
1418
|
+
useEffect(() => {
|
|
1419
|
+
const unsubscribe = blink.auth.onAuthStateChanged((state) => {
|
|
1420
|
+
setUser(state.user)
|
|
1421
|
+
setLoading(state.isLoading)
|
|
1422
|
+
})
|
|
1423
|
+
return unsubscribe
|
|
1424
|
+
}, [])
|
|
1425
|
+
```
|
|
1426
|
+
|
|
1427
|
+
**Why the one-time check fails:**
|
|
1428
|
+
1. App loads → `blink.auth.me()` called immediately
|
|
1429
|
+
2. Auth still initializing → Call fails, user set to `null`
|
|
1430
|
+
3. Auth completes later → App never knows because it only checked once
|
|
1431
|
+
4. User stuck on "Please sign in" screen forever
|
|
1432
|
+
|
|
1433
|
+
**Always use `onAuthStateChanged()` for React apps!**
|
|
1434
|
+
|
|
1393
1435
|
```typescript
|
|
1394
1436
|
import { createClient } from '@blinkdotnew/sdk'
|
|
1395
1437
|
import { useState, useEffect } from 'react'
|
|
@@ -1398,19 +1440,46 @@ const blink = createClient({ projectId: 'your-project', authRequired: true })
|
|
|
1398
1440
|
|
|
1399
1441
|
function App() {
|
|
1400
1442
|
const [user, setUser] = useState(null)
|
|
1443
|
+
const [loading, setLoading] = useState(true)
|
|
1401
1444
|
|
|
1402
1445
|
useEffect(() => {
|
|
1403
1446
|
const unsubscribe = blink.auth.onAuthStateChanged((state) => {
|
|
1404
1447
|
setUser(state.user)
|
|
1448
|
+
setLoading(state.isLoading)
|
|
1405
1449
|
})
|
|
1406
1450
|
return unsubscribe
|
|
1407
1451
|
}, [])
|
|
1408
1452
|
|
|
1453
|
+
if (loading) return <div>Loading...</div>
|
|
1409
1454
|
if (!user) return <div>Please log in</div>
|
|
1410
1455
|
|
|
1411
1456
|
return <div>Welcome, {user.email}!</div>
|
|
1412
1457
|
}
|
|
1413
1458
|
|
|
1459
|
+
// ❌ WRONG - One-time auth check (misses auth state changes)
|
|
1460
|
+
function BadApp() {
|
|
1461
|
+
const [user, setUser] = useState(null)
|
|
1462
|
+
const [loading, setLoading] = useState(true)
|
|
1463
|
+
|
|
1464
|
+
useEffect(() => {
|
|
1465
|
+
const checkAuth = async () => {
|
|
1466
|
+
try {
|
|
1467
|
+
const userData = await blink.auth.me()
|
|
1468
|
+
setUser(userData)
|
|
1469
|
+
} catch (error) {
|
|
1470
|
+
console.error('Auth check failed:', error)
|
|
1471
|
+
} finally {
|
|
1472
|
+
setLoading(false)
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
checkAuth() // ❌ Only runs once - misses when auth completes later!
|
|
1477
|
+
}, [])
|
|
1478
|
+
|
|
1479
|
+
// This pattern causes users to get stuck on "Please sign in"
|
|
1480
|
+
// even after authentication completes
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1414
1483
|
// React example with search functionality
|
|
1415
1484
|
function SearchResults() {
|
|
1416
1485
|
const [query, setQuery] = useState('')
|
package/package.json
CHANGED