This is the code for my index.js file in react native where im trying to fetch the userID if the currently logged in user.
import { StyleSheet, Text, View } from "react-native";
import React, { useEffect, useState } from "react";
import AsyncStorage from "@react-native-async-storage/async-storage";
import jwt_decode from "jwt-decode"; // Ensure this is correctly imported
import axios from "axios";
const index = () => {
const [userId, setUserId] = useState("");
const [user, setUser] = useState();
useEffect(() => {
const fetchUser = async () => {
try {
const token = await AsyncStorage.getItem("authToken");
if (!token) {
console.log("No token found");
return;
}
console.log("Token retrieved:", token);
const decodedToken = jwt_decode(token);
console.log("Decoded token:", decodedToken);
const userId = decodedToken.userId;
if (!userId) {
console.log("No userId in token");
return;
}
setUserId(userId);
} catch (error) {
console.log("Error fetching user:", error);
}
};
fetchUser();
}, []);
useEffect(() => {
console.log("userId:", userId);
}, [userId]);
return (
<View>
<Text>index</Text>
</View>
);
};
export default index;
const styles = StyleSheet.create({});
it gives me these logs:
` LOG userId:
LOG Token retrieved: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiI2NjdhYWEyODhlOWZiNTAxMDExMmQwNTIiLCJpYXQiOjE3MTk0ODIzMTl9.RB6ZVFQRv8Q2s_zrKljEUQP1EKt42Od7qyHZjTGOZAU
LOG Error fetching user: [TypeError: 0, _jwtDecode.default is not a function (it is undefined)]
`
I tried reinstalling jet-decode using rpm but it didnt help.
rhea laloo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.