|
|
@ -1,11 +1,11 @@ |
|
|
|
import * as SecureStore from "expo-secure-store"; |
|
|
|
import jwtDecode from "jwt-decode"; |
|
|
|
|
|
|
|
const key = "authToke"; |
|
|
|
const tokenKey = "authToken"; |
|
|
|
const userKey = "userKey"; |
|
|
|
|
|
|
|
const setToken = async (authToken) => { |
|
|
|
try { |
|
|
|
await SecureStore.setItemAsync(key, authToken); |
|
|
|
await SecureStore.setItemAsync(tokenKey, authToken); |
|
|
|
} catch (error) { |
|
|
|
console.log("Error while storing authToken"); |
|
|
|
} |
|
|
@ -13,7 +13,7 @@ const setToken = async (authToken) => { |
|
|
|
|
|
|
|
const getToken = async () => { |
|
|
|
try { |
|
|
|
return await SecureStore.getItemAsync(key); |
|
|
|
return await SecureStore.getItemAsync(tokenKey); |
|
|
|
} catch (error) { |
|
|
|
console.log("Error while gettig authToken"); |
|
|
|
} |
|
|
@ -21,15 +21,28 @@ const getToken = async () => { |
|
|
|
|
|
|
|
const removeToken = async () => { |
|
|
|
try { |
|
|
|
await SecureStore.deleteItemAsync(key); |
|
|
|
await SecureStore.deleteItemAsync(tokenKey); |
|
|
|
} catch (e) { |
|
|
|
console.log("Error while removing authToken"); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
const setUser = async (userData) => { |
|
|
|
try { |
|
|
|
await SecureStore.setItemAsync(userKey, JSON.stringify(userData)); |
|
|
|
} catch (error) { |
|
|
|
console.log("Error while storing user data"); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
const getUser = async () => { |
|
|
|
const token = await getToken(); |
|
|
|
return token ? jwtDecode(token) : null; |
|
|
|
try { |
|
|
|
return await SecureStore.getItemAsync(userKey).then((data) => { |
|
|
|
return JSON.parse(data); |
|
|
|
}); |
|
|
|
} catch (error) { |
|
|
|
console.log("Error while getting user data"); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
export default { setToken, getUser, removeToken, getToken }; |
|
|
|
export default { setToken, getUser, removeToken, getToken, setUser }; |