aboutsummaryrefslogtreecommitdiff
path: root/src/routes/authentication/AuthProvider.tsx
diff options
context:
space:
mode:
authorIvan Chen <ivan@thetaggid.com>2020-11-02 13:23:04 -0500
committerGitHub <noreply@github.com>2020-11-02 13:23:04 -0500
commit6aa3bf3068d60f5ffac5af43ed98175e4735903f (patch)
tree1bab1f411cde526cb23c152748fc420e18fc1fe0 /src/routes/authentication/AuthProvider.tsx
parenteba538802b4cb82d3ed8e0065eb848e78d0ac458 (diff)
added async storage login for userid and username (#89)
Diffstat (limited to 'src/routes/authentication/AuthProvider.tsx')
-rw-r--r--src/routes/authentication/AuthProvider.tsx27
1 files changed, 23 insertions, 4 deletions
diff --git a/src/routes/authentication/AuthProvider.tsx b/src/routes/authentication/AuthProvider.tsx
index 55a6f3ad..383b4d5b 100644
--- a/src/routes/authentication/AuthProvider.tsx
+++ b/src/routes/authentication/AuthProvider.tsx
@@ -4,19 +4,19 @@ import {INTEGRATED_SOCIAL_LIST} from '../../constants';
import {
loadAvatar,
loadCover,
+ loadFollowers,
+ loadFollowing,
+ loadMoments,
loadProfileInfo,
loadRecentlySearchedUsers,
loadSocialPosts,
- loadMoments,
- loadFollowers,
- loadFollowing,
} from '../../services';
import {
+ MomentType,
ProfilePreviewType,
ProfileType,
SocialAccountType,
UserType,
- MomentType,
} from '../../types';
interface AuthContextProps {
@@ -98,6 +98,23 @@ const AuthProvider: React.FC = ({children}) => {
const [following, setFollowing] = useState<Array<ProfilePreviewType>>([]);
const [followersNeedUpdate, setFollowersNeedUpdate] = useState<boolean>(true);
const {userId} = user;
+
+ useEffect(() => {
+ const loadUserInfoFromStorage = async () => {
+ const [id, username, token] = await Promise.all([
+ AsyncStorage.getItem('userId'),
+ AsyncStorage.getItem('username'),
+ AsyncStorage.getItem('token'),
+ ]);
+ if (id && username && token) {
+ setUser({...user, userId: id, username});
+ }
+ };
+ if (user === NO_USER) {
+ loadUserInfoFromStorage();
+ }
+ }, [user]);
+
useEffect(() => {
if (!userId) {
return;
@@ -196,6 +213,8 @@ const AuthProvider: React.FC = ({children}) => {
try {
new Promise(() => {
AsyncStorage.removeItem('token');
+ AsyncStorage.removeItem('userId');
+ AsyncStorage.removeItem('username');
}).then(() => {
setUser(NO_USER);
});