Skip to content

Commit

Permalink
fix: fix getUser data
Browse files Browse the repository at this point in the history
  • Loading branch information
geeekgod committed Oct 6, 2022
1 parent b9ad4a5 commit e8ba498
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
22 changes: 12 additions & 10 deletions app/context/AuthContext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ const AuthContextProvider = ({ children }) => {
const [user, setUser] = useState({});
const [accessToken, setAccessToken] = useState(null);

const getUser = () => {
const getUser = (token) => {
bloodLineApi.get('/user', {
headers: {
Authorization: accessToken
Authorization: token
}
}).then((res) => {
if (res.data.success) {
setUser(res.data.data)
storeData("@user", res.data.data)
}
}).catch((err) => {
if (err.response.status === 401) {
if (err.response.status === 401 && err.response.data.message === "Not Authorized") {
logout()
}
})
Expand Down Expand Up @@ -78,11 +78,15 @@ const AuthContextProvider = ({ children }) => {
useEffect(() => {
const firstLoad = async () => {
try {
const user = await AsyncStorage.getItem("@user");
const accessToken = await AsyncStorage.getItem("@accessToken");
getUser();
setUser(JSON.parse(user));
setAccessToken(JSON.parse(accessToken));
const userN = await AsyncStorage.getItem("@user");
const accessTokenN = await AsyncStorage.getItem("@accessToken");
setUser(JSON.parse(userN));
setAccessToken(JSON.parse(accessTokenN));
setTimeout(() => {
if (userN && accessTokenN) {
getUser(JSON.parse(accessTokenN));
}
}, 1000)
} catch (err) {
console.log(err);
} finally {
Expand All @@ -93,8 +97,6 @@ const AuthContextProvider = ({ children }) => {
firstLoad();
}, []);

console.log(user)

useEffect(() => {
if (accessToken) {
bloodLineApi.defaults.headers.common['Authorization'] = accessToken;
Expand Down
6 changes: 1 addition & 5 deletions app/screens/CreateProfile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,17 @@ const CreateProfile = () => {

const onSubmit = () => {
const headers = { headers: { "Authorization": accessToken } }
console.log(headers);
if (phoneErr === '' && cityErr === '' && pinErr === '' && bloodGroupErr === "") {
bloodLineApi.post("/profile", {
phone: phone, city: city, pin: pin !== "" ? pin : null, bloodGroup: bloodGroup,
headers, withCredentials: true
}).then((res) => {
storeProfile(res.data);
setSubmitted(false)
getUser();
getUser(accessToken);
navigation.navigate("Home")
}).catch((err) => {
console.log(err);
setShowError(true)

setTimeout(() => setShowError(false), 5000);
})
}
}
Expand Down
8 changes: 6 additions & 2 deletions app/screens/Login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ const Login = () => {
bloodLineApi.post("/auth/googleAuth", {
accessToken: authentication.accessToken
}).then((res) => {
console.log(res.data);
storeCredentials(res.data);
setTimeout(() => navigation.navigate("CreateProfile"), 200)
if(res.data.isProfile){
setTimeout(() => navigation.navigate("Home"), 200)
}
else{
setTimeout(() => navigation.navigate("CreateProfile"), 200)
}
}).catch((err) => {
console.log(err);
setShowError(true)
Expand Down

0 comments on commit e8ba498

Please sign in to comment.