Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

boyjarv's avatar

why do I have to refresh after logging in to see my user name?

here is my login method form my login form:

const login = () => {
  if (!email.value || !password.value) {
    alert("Please fill all the field");
  } else {
    const data = {
      email: email.value,
      password: password.value,
    };
    auth.userLogin(data);
    router.push("/");
  }
};

here is my pinia action to login:

async userLogin(data) {
      this.isloading = true;

      const res = await fetch("http://127.0.0.1:8000/api/login/", {
        method: "POST",
        body: JSON.stringify(data),
        headers: {
          "Content-Type": "application/json",
        },
      });
      const output = await res.json();
      console.log("Output: ", output.authorisation.token);
      if (output.status === "success") {
        localStorage.setItem("token", output.authorisation.token);
        this.token = output.authorisation.token;
        alert(output.status);
        this.isloading = false;
        this.$route.push("/");
      } else {
        this.isloading = false;

        alert(output.message);
      }
    },

Great, all good, setting the localStorage Token and getting success....

when I get redirected to / the getUser state action is triggered:

top of my index.vue page

import { useAuth } from "@/store";
const auth = useAuth();
auth.getUser();

and here is my getUser method:

async getUser() {
      this.isloading = true;
      console.log("Local storage: ", localStorage.getItem("token"));
      const res = await fetch("http://localhost:8000/api/user", {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + localStorage.getItem("token"),
        },
      });
      const output = await res.json();
      console.log("Name: ", output.name);
      this.isloading = false;
      this.user = output;
    },

It only says Welcome Mr Tester after I refresh the page again?!

0 likes
11 replies
vincent15000's avatar

@boyjarv That's a shame, it should have worked. I have tried myself for several hours before really understanding what didn't work. And now it's very easy to set up a new app with Sanctum and VueJS. You should persevere ;).

vincent15000's avatar

Is the user name ok in the console.log ?

Assuming it's not the same page (redirection after login), you should store the user name in the VueJS store, this will allow you to share datas between several pages.

erizo's avatar

Might be you should try to await the getUser call from the index page You could consider making the call in asyncData or fetch to do so hope this could help

(I don't think you can make the setup async and wrap the page in a Suspense component in nuxt 2)

1 like
boyjarv's avatar

nope, still stuck on this... also, when I add a todo, I need to refresh so I can see this has been added

1 like
boyjarv's avatar

@vincent15000 yes, when logged in and get redorected to Hompage:

<script setup>
import { useAuth } from "@/store";
const auth = useAuth();
auth.getUser();
</script>

 <h1>{{ auth.user.name }}</h1>

Please or to participate in this conversation.