{"action":"created","issue":{"url":"https://api.github.com/repos/nuxt/nuxt.js/issues/454","repository_url":"https://api.github.com/repos/nuxt/nuxt.js","labels_url":"https://api.github.com/repos/nuxt/nuxt.js/issues/454/labels{/name}","comments_url":"https://api.github.com/repos/nuxt/nuxt.js/issues/454/comments","events_url":"https://api.github.com/repos/nuxt/nuxt.js/issues/454/events","html_url":"https://github.com/nuxt/nuxt.js/issues/454","id":217049291,"number":454,"title":"Middleware Ajax Request doesn't send up cookies?","user":{"login":"Tibfib","id":290380,"avatar_url":"https://avatars2.githubusercontent.com/u/290380?v=3","gravatar_id":"","url":"https://api.github.com/users/Tibfib","html_url":"https://github.com/Tibfib","followers_url":"https://api.github.com/users/Tibfib/followers","following_url":"https://api.github.com/users/Tibfib/following{/other_user}","gists_url":"https://api.github.com/users/Tibfib/gists{/gist_id}","starred_url":"https://api.github.com/users/Tibfib/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Tibfib/subscriptions","organizations_url":"https://api.github.com/users/Tibfib/orgs","repos_url":"https://api.github.com/users/Tibfib/repos","events_url":"https://api.github.com/users/Tibfib/events{/privacy}","received_events_url":"https://api.github.com/users/Tibfib/received_events","type":"User","site_admin":false},"labels":[{"id":469223667,"url":"https://api.github.com/repos/nuxt/nuxt.js/labels/help%20wanted","name":"help wanted","color":"128A0C","default":true}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":9,"created_at":"2017-03-26T12:42:34Z","updated_at":"2017-05-16T15:25:54Z","closed_at":"2017-05-16T14:56:57Z","body":"Hello everyone! I'm absolutely loving using nuxtjs for my vue app. It's been awesome so far. Thanks for the hard work!\r\n\r\nI seem to have run into a bug or maybe I'm misunderstanding how the middlewares should work. The documentation indicates that I can return a promise in a middleware. \r\n\r\n\r\nHere's what my middleware looks like. I'm using secure cookies in my app which are not accessible via JavaScript, so, the only way to check if a user is authenticated is by hitting the server. This midleware will check the authentication using my vuex method `getSignedInUser`. \r\n```javascript\r\n// Middleware\r\nexport default function({ store, redirect }) {\r\n    // If the user is not authenticated\r\n    console.log('is authenticated?');\r\n    if (!store.getters.isAuthenticated) {\r\n        if (!store.getters.hasCheckedForAuthentication) {\r\n            return new Promise((resolve, reject) => {\r\n                store\r\n                    .dispatch('getSignedInUser')\r\n                    .then(() => resolve())\r\n                    .catch(() => redirect('/admin/login'));\r\n            });\r\n        } else\r\n            return redirect('/admin/login');\r\n    }\r\n}\r\n```\r\n(yeah, I know I probably don't have to wrap my promise in another promise–was trying to debug)\r\n\r\nThe ajax request is super simple. I use axios with `withCredentials: true`.\r\n\r\n```javascript\r\n// Get Signed in User\r\nfunction getSignedInUser() {\r\n    console.log('Send up ajax request');\r\n    return axios.get('/api/v1/admin/user');\r\n}\r\n```\r\n\r\nIn the end, there's no ajax request in Chrome:\r\n![image](https://cloud.githubusercontent.com/assets/290380/24331275/991f0b84-11e5-11e7-85af-fe97c4b57a4e.png)\r\n\r\nHowever, I know it's hitting the server, because the logs are coming through. \r\n![image](https://cloud.githubusercontent.com/assets/290380/24331302/58b00a7a-11e6-11e7-8641-a2dc9e95abf7.png)\r\n\r\nWhen I run the exact same vuex action from a vue method, it works. \r\n\r\n```javascript\r\nisSignedIn() {\r\n    this.$store.dispatch('getSignedInUser');\r\n }\r\n```\r\n![image](https://cloud.githubusercontent.com/assets/290380/24331328/a4da525c-11e6-11e7-8b6a-75ed009e8685.png)\r\n\r\nSo am I missing something? How can I get nuxt.js to send up the actual ajax request with the cookie during the middleware? Or should I change my strategy for authentication? I'd like to continue to use secure cookies (to protect against possible XSS attacks).\r\n\r\nThanks!\r\n\r\nUPDATE: I just noticed that this issue ONLY occurs when I directly load the page with the middleware. For example, I run this middleware on the `/admin` page. If I directly go to `localhost:3000/admin`, the issue I spoke of above occurs. However, if I add a `nuxt-link` to my home page, linking to the `/admin` page and go to the page by clicking on the link, this issue doesn't occur. \r\n"},"comment":{"url":"https://api.github.com/repos/nuxt/nuxt.js/issues/comments/301818414","html_url":"https://github.com/nuxt/nuxt.js/issues/454#issuecomment-301818414","issue_url":"https://api.github.com/repos/nuxt/nuxt.js/issues/454","id":301818414,"user":{"login":"Tibfib","id":290380,"avatar_url":"https://avatars2.githubusercontent.com/u/290380?v=3","gravatar_id":"","url":"https://api.github.com/users/Tibfib","html_url":"https://github.com/Tibfib","followers_url":"https://api.github.com/users/Tibfib/followers","following_url":"https://api.github.com/users/Tibfib/following{/other_user}","gists_url":"https://api.github.com/users/Tibfib/gists{/gist_id}","starred_url":"https://api.github.com/users/Tibfib/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Tibfib/subscriptions","organizations_url":"https://api.github.com/users/Tibfib/orgs","repos_url":"https://api.github.com/users/Tibfib/repos","events_url":"https://api.github.com/users/Tibfib/events{/privacy}","received_events_url":"https://api.github.com/users/Tibfib/received_events","type":"User","site_admin":false},"created_at":"2017-05-16T15:25:54Z","updated_at":"2017-05-16T15:25:54Z","body":"@Atinux oh, that makes sense. But if I navigate from `localhost:3000` to `localhost:3000/admin` within the app, the middleware is run client-side, right? Is this how it has to be or was it designed that way? That seems confusing :/. Could both not be client-side? or both be server-side? Could it be specified in the middleware?\r\n\r\nThanks for the info!"}}
{"actor":{"display_login":"Tibfib"}}