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/screens/search/SearchScreen.tsx | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

(limited to 'src/screens/search/SearchScreen.tsx')

diff --git a/src/screens/search/SearchScreen.tsx b/src/screens/search/SearchScreen.tsx
index 2a2a5a4a..da83ddef 100644
--- a/src/screens/search/SearchScreen.tsx
+++ b/src/screens/search/SearchScreen.tsx
@@ -16,6 +16,11 @@ import AsyncStorage from '@react-native-community/async-storage';
 import {ProfilePreviewType} from '../../types';
 import {SEARCH_ENDPOINT} from '../../constants';
 import {AuthContext} from '../../routes/authentication';
+import {UserType} from '../../types';
+const NO_USER: UserType = {
+  userId: '',
+  username: '',
+};
 
 /**
  * Search Screen for user recommendations and a search
@@ -31,6 +36,7 @@ const SearchScreen: React.FC = () => {
   );
   const [searching, setSearching] = useState(false);
   const top = Animated.useValue(-SCREEN_HEIGHT);
+  const [user, setUser] = useState<UserType>(NO_USER);
   useEffect(() => {
     if (query.length < 3) {
       setResults([]);
@@ -38,8 +44,16 @@ const SearchScreen: React.FC = () => {
     }
     const loadResults = async (q: string) => {
       try {
+        const token = await AsyncStorage.getItem('token');
+        if (!token) {
+          setUser(NO_USER);
+          return;
+        }
         const response = await fetch(`${SEARCH_ENDPOINT}?query=${q}`, {
           method: 'GET',
+          headers: {
+            Authorization: 'Token ' + token,
+          },
         });
         const status = response.status;
         if (status === 200) {
-- 
cgit v1.2.3-70-g09d2

