Home > Software engineering >  How to get properly userId from strapi when login?
How to get properly userId from strapi when login?

Time:06-30

I am migrating from strapi 3.6.8 (mongodb-mongo cluster) to 4.2 (mysql -azuremysql) with React native Expo CLI, I am testing this new version locally but I have this error when i login and try to get auth data from user i dont receive idUser only token. The interesting part is when I refresh the app (not clossing session) and go to Account Screen I can finally get idUser if I close session and login with another user, have same problem.

This is where i try to get idUser (Account Screen)

import useAuth from "../../Hooks/useAuth";
export default function Account() {
  const [user, setUser] = useState(null);
  const { auth } = useAuth();
  console.log(auth);
  useFocusEffect(
    useCallback(() => {
      (async () => {
        const response = await getMeApi(auth.token);
        setUser(response);
      })();
    }, [])
  );

This is useAuth (just a bridge between AuthContext and other components)

import { useContext } from "react";
import AuthContext from "../Context/AuthContext";

export default () => useContext(AuthContext);

This is AuthContext

import { createContext } from "react";

const AuthContext = createContext({
  auth: undefined,
  login: () => null,
  logout: () => null,
});

export default AuthContext;

This is what i have printing auth enter image description here

CodePudding user response:

Your setUser(response); call takes in a response from the Strapi User Permissions provider which looks something like this:

data : {
  user: {
    ...
  },
  jwt: ...
}

https://docs.strapi.io/developer-docs/latest/plugins/users-permissions.html#login.

Trying to access idUser on this object is not defined.

  • Related