Skip to content

Commit fb08053

Browse files
committed
update authentication logic and UI
1 parent 11d2944 commit fb08053

File tree

4 files changed

+276
-182
lines changed

4 files changed

+276
-182
lines changed

src/contexts/AccountContext.tsx

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, { createContext, useContext, useEffect, useMemo, useState, useRef } from 'react';
1+
import React, { createContext, useContext, useEffect, useMemo, useState, useRef, useCallback } from 'react';
2+
import { AppState } from 'react-native';
23
import accountService, { AuthUser } from '../services/AccountService';
34

45
type AccountContextValue = {
@@ -18,22 +19,38 @@ export const AccountProvider: React.FC<{ children: React.ReactNode }> = ({ child
1819
const [loading, setLoading] = useState(true);
1920
const loadingTimeoutRef = useRef<NodeJS.Timeout | null>(null);
2021

21-
useEffect(() => {
22-
// Initial user load
23-
const loadUser = async () => {
24-
try {
25-
const u = await accountService.getCurrentUser();
26-
setUser(u);
27-
} catch (error) {
28-
console.warn('[AccountContext] Failed to load user:', error);
29-
} finally {
22+
const syncCurrentUser = useCallback(async (showLoading = false) => {
23+
if (showLoading) {
24+
setLoading(true);
25+
}
26+
27+
try {
28+
const u = await accountService.getCurrentUser();
29+
setUser(u);
30+
} catch (error) {
31+
console.warn('[AccountContext] Failed to load user:', error);
32+
setUser(null);
33+
} finally {
34+
if (showLoading) {
3035
setLoading(false);
3136
}
32-
};
33-
34-
loadUser();
37+
}
3538
}, []);
3639

40+
useEffect(() => {
41+
syncCurrentUser(true);
42+
43+
const subscription = AppState.addEventListener('change', (nextState) => {
44+
if (nextState === 'active') {
45+
syncCurrentUser(false);
46+
}
47+
});
48+
49+
return () => {
50+
subscription.remove();
51+
};
52+
}, [syncCurrentUser]);
53+
3754
const value = useMemo<AccountContextValue>(() => ({
3855
user,
3956
loading,

0 commit comments

Comments
 (0)