0

I'm going straight to the point here. I'm doing an authentication on the login page, after clicking the login page it redirects me to a product's component and on that product's component, I do an HTTP request to get all the products. However, after logging in and redirecting on the products page the product's component seems it can't run my HTTP request on the created() lifecycle hooks. Is this a normal behaviour?

Here's my code:

LOGIN:

export default{
        data(){
            return {
                email: '',
                password: ''
            }
        },
        methods:{
            login(){
                var data = {
                    client_id: 2,
                    client_secret: 'TOKEN_HERE,
                    grant_type: 'password',
                    username: this.email,
                    password: this.password
                }

                this.$http.post("oauth/token", data).then(response => {
                    this.$auth.setToken(response.body.access_token, response.body.expires_in + Date.now())
                    this.$router.push('/products')
                })

            }
        }

PRODUCTS.VUE

   import Products from './product/Products.vue'
    export default {
        components: {
            'my-products' : Products
        },
        created(){
          this.$http.get('api/products')
            .then(response => {
                alert("products from feed");
                this.products = response.body
            })
        }
    }

after redirecting to products.vue created lifecycle hook, it can't run my http request.

Thanks in advance.

15
  • What do you mean can't run? What is happening? The created hook is not triggering? Commented Mar 26, 2018 at 1:30
  • @acdcjunior when I login successfully, I will be redirected to products component using this.$router.push('/products').. after the redirection the created() lifecyclye hook can't run my http request.. it gives me error on my console uncaught (in promise) Commented Mar 26, 2018 at 1:37
  • Then catch it and see: this.$http.get('api/products') .then(response => { alert("products from feed"); this.products = response.body }, err => console.log('error is:', err)); can you paste the error here? Commented Mar 26, 2018 at 1:39
  • @acdcjunior will do.. give me a second. Commented Mar 26, 2018 at 1:39
  • @acdcjunior Hi sir error message says I am Unauthenticated.. however I am 100% sure I am authenticated... when I refresh the page it shows me all the products... however, the initial load of the page after this.$router.push gives me unauthorized error Commented Mar 26, 2018 at 1:44

1 Answer 1

1

I seems like you need mounted hook instead.

https://v2.vuejs.org/v2/guide/instance.html#Instance-Lifecycle-Hooks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.