From 45e435dbb4c43cb890eb360413784d0b2e331bc5 Mon Sep 17 00:00:00 2001 From: Shravya Ramesh <37447613+shravyaramesh@users.noreply.github.com> Date: Wed, 7 Oct 2020 23:06:32 -0700 Subject: [TMA 68] Frontend Token Security (#43) * frontend tma-68 token security * removed: try catch while storing token to async, unnecessary console.log * login/registration exception handling and relocation * Modified promises, applied fetch restriction --- src/components/search/SearchResult.tsx | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/components/search/SearchResult.tsx') diff --git a/src/components/search/SearchResult.tsx b/src/components/search/SearchResult.tsx index e65be1f4..952f08f7 100644 --- a/src/components/search/SearchResult.tsx +++ b/src/components/search/SearchResult.tsx @@ -11,6 +11,11 @@ import { import RNFetchBlob from 'rn-fetch-blob'; import AsyncStorage from '@react-native-community/async-storage'; import {AVATAR_PHOTO_ENDPOINT} from '../../constants'; +import {UserType} from '../../types'; +const NO_USER: UserType = { + userId: '', + username: '', +}; interface SearchResultProps extends ViewProps { profilePreview: ProfilePreviewType; @@ -20,15 +25,22 @@ const SearchResult: React.FC = ({ style, }) => { const [avatarURI, setAvatarURI] = useState(null); - + const [user, setUser] = useState(NO_USER); useEffect(() => { let mounted = true; const loadAvatar = async () => { try { + const token = await AsyncStorage.getItem('token'); + if (!token) { + setUser(NO_USER); + return; + } const response = await RNFetchBlob.config({ fileCache: true, appendExt: 'jpg', - }).fetch('GET', AVATAR_PHOTO_ENDPOINT + `${id}`); + }).fetch('GET', AVATAR_PHOTO_ENDPOINT + `${id}`, { + Authorization: 'Token ' + token, + }); const status = response.info().status; if (status === 200) { if (mounted) { -- cgit v1.2.3-70-g09d2