0

I'm coding an POS-system in laravel and vuejs. To login I'm using JWT-auth. Now if there is a already a "cash session" the system needs to redirect to a vue-route /Dashboard, if not you need to redirect to /Cash. Now I've tried various things but nothing works. It always redirect to /Cash.

Does anyone have a solution?

methods: {
        login() {
            let app = this;
            this.$auth.login({
                params: {
                    username: app.username,
                    password: app.password
                },
                success: function () {
                },
                error: function (resp) {
                    alert('Error: ' + resp.response.data.msg)
                },
                redirect: app.redirectURL(),
                fetchUser: true
            });
        },

        setText(gebruiker) {
            this.buttonText = gebruiker;
            this.username = gebruiker;
        },

        redirectURL () {
            let cashActive = false;
            axios.get('cash/active').then(response => {
                console.log(response.data.active);
                cashActive = response.data.active;
            });
            console.log("Cash Active: " + cashActive);
            if (!cashActive) {
                console.log("Return /Cash");
                return "Cash";
            } else {
                console.log("Return /dashboard");
                return "/Dashboard";
            }
        }
    }

EDIT: I've tried asynchronous function, but redirect still doesn't work. My page doesn't redirect. There must be an issue with what I return in the function redirectURL. Can anyone help?

  methods: {
        login() {
            let app = this;
            this.$auth.login({
                params: {
                    username: app.username,
                    password: app.password
                },
                success: function () {
                },
                error: function (resp) {
                    alert('Error: ' + resp.response.data.msg)
                },
                redirect: app.redirectURL(),
                fetchUser: true
            });
        },

        setText(gebruiker) {
            this.buttonText = gebruiker;
            this.username = gebruiker;
        },

        async redirectURL () {
            let cashActive = await axios.get('cash/active').then(response => {
                //console.log(response.data.active);
                return response.data.active;
            });
            console.log("Cash Active: " + cashActive);
            if (!cashActive) {
                console.log("Return /cash");
                return "/cash";
            } else {
                console.log("Return /dashboard");
                return "/dashboard";
            }
        }
    },

}
Thomas Delputte
  • 141
  • 1
  • 2
  • 6
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Phil May 03 '18 at 11:38
  • Your `if (!cashActive)` and following code execute **before** the `axios.get` completes – Phil May 03 '18 at 11:39

0 Answers0