{"action":"created","issue":{"url":"https://api.github.com/repos/alidcastano/rogue/issues/29","repository_url":"https://api.github.com/repos/alidcastano/rogue","labels_url":"https://api.github.com/repos/alidcastano/rogue/issues/29/labels{/name}","comments_url":"https://api.github.com/repos/alidcastano/rogue/issues/29/comments","events_url":"https://api.github.com/repos/alidcastano/rogue/issues/29/events","html_url":"https://github.com/alidcastano/rogue/issues/29","id":338647480,"node_id":"MDU6SXNzdWUzMzg2NDc0ODA=","number":29,"title":"Question: Server-side authentication","user":{"login":"malonehedges","id":12092296,"node_id":"MDQ6VXNlcjEyMDkyMjk2","avatar_url":"https://avatars1.githubusercontent.com/u/12092296?v=4","gravatar_id":"","url":"https://api.github.com/users/malonehedges","html_url":"https://github.com/malonehedges","followers_url":"https://api.github.com/users/malonehedges/followers","following_url":"https://api.github.com/users/malonehedges/following{/other_user}","gists_url":"https://api.github.com/users/malonehedges/gists{/gist_id}","starred_url":"https://api.github.com/users/malonehedges/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/malonehedges/subscriptions","organizations_url":"https://api.github.com/users/malonehedges/orgs","repos_url":"https://api.github.com/users/malonehedges/repos","events_url":"https://api.github.com/users/malonehedges/events{/privacy}","received_events_url":"https://api.github.com/users/malonehedges/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-07-05T16:36:11Z","updated_at":"2018-07-05T16:46:59Z","closed_at":"2018-07-05T16:46:59Z","author_association":"NONE","body":"Hi there,\r\n\r\nI was wondering if it would be possible to have a route protected by some server-side authentication.\r\n\r\nSimplest example:\r\n* Auth page (public)\r\n* Secret page (loaded only if authenticated)\r\n\r\nMost importantly, the data (including layout / content of the page) cannot be transmitted unless the client has successfully authenticated. I was hoping to use `import` and have the server only return the component if the user is authenticated but don't know how to hook into this tool (or if it is possible).\r\n\r\nThanks in advance!"},"comment":{"url":"https://api.github.com/repos/alidcastano/rogue/issues/comments/402784663","html_url":"https://github.com/alidcastano/rogue/issues/29#issuecomment-402784663","issue_url":"https://api.github.com/repos/alidcastano/rogue/issues/29","id":402784663,"node_id":"MDEyOklzc3VlQ29tbWVudDQwMjc4NDY2Mw==","user":{"login":"alidcastano","id":11031952,"node_id":"MDQ6VXNlcjExMDMxOTUy","avatar_url":"https://avatars0.githubusercontent.com/u/11031952?v=4","gravatar_id":"","url":"https://api.github.com/users/alidcastano","html_url":"https://github.com/alidcastano","followers_url":"https://api.github.com/users/alidcastano/followers","following_url":"https://api.github.com/users/alidcastano/following{/other_user}","gists_url":"https://api.github.com/users/alidcastano/gists{/gist_id}","starred_url":"https://api.github.com/users/alidcastano/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alidcastano/subscriptions","organizations_url":"https://api.github.com/users/alidcastano/orgs","repos_url":"https://api.github.com/users/alidcastano/repos","events_url":"https://api.github.com/users/alidcastano/events{/privacy}","received_events_url":"https://api.github.com/users/alidcastano/received_events","type":"User","site_admin":false},"created_at":"2018-07-05T16:46:59Z","updated_at":"2018-07-05T16:46:59Z","author_association":"OWNER","body":"Yes, here's how I handle this (going to mainly copy and paste some code I'm using)\r\n\r\nI create a `Router` component with all my routes and a `getInitialProps` method that refreshes the session (the example below uses apollo)\r\n\r\n```\r\nconst routable = (Layout, Page) => props => <Layout {...props}><Page {...props} /></Layout>\r\n\r\nexport default class Router extends React.Component {\r\n  static async getInitialProps (ctx) {\r\n    await Router.refreshSession(ctx)\r\n  }\r\n\r\n  static async refreshSession (ctx) {\r\n    const token = getTokenFromReq(ctx.req)\r\n    if (!ctx.store.getState().session.user && token) {\r\n      try {\r\n        const res = await ctx.apollo.query({ query: authUserQuery })\r\n        ctx.store.dispatch(setSession({ user: res.data.authUser }))\r\n      } catch (err) { // invalid or expired token \r\n        ctx.apollo.resetStore()\r\n        ctx.store.dispatch(removeSession())\r\n      }\r\n    }\r\n  }\r\n\r\n  render () {\r\n    return (\r\n      <Switch>\r\n        <Route exact path=\"/welcome\" render={routable(Landing, Welcome)} />\r\n        <Route exact path=\"/register\" render={routable(Auth, Register)} />\r\n        <Route exact path=\"/login\" render={routable(Auth, Login)} />\r\n        <Route path=\"/\" render={routable(App, Dashboard)} />\r\n      </Switch>\r\n    )\r\n  }\r\n}\r\n```\r\n\r\nThen I create some middleware for handling authentication\r\n\r\n```\r\nconst isSessionUser = (ctx) => ctx.store.getState().session.user\r\n\r\nexport const loggedInOrRedirect = (ctx) => {\r\n  if (!isSessionUser(ctx)) {\r\n    ctx.redirect('/welcome')\r\n  }\r\n}\r\n\r\nexport const loggedOutOrRedirect = (ctx) => {\r\n  if (isSessionUser(ctx)) {\r\n    ctx.redirect('/')\r\n  }\r\n}\r\n\r\n```\r\n\r\nand I use this middleware inside my `AppLayout` and `AuthLayout` etc. `getInitialProps` method\r\n\r\n```\r\nexport default class AuthLayout extends React.Component {\r\n  static async getInitialProps (ctx) {\r\n    await loggedOutOrRedirect(ctx)\r\n  }\r\n}\r\n```\r\n\r\nYour `Router` will be the top most component and be called first, so session will be refreshed prior to running middleware inside Layout/Page to check whether user is authenticated\r\n\r\nI plan on adding an example of this, and maybe even including this by default in starter template since everyone needs authentication for an app\r\n\r\nBut I hope it helps for now! Closing this\r\n\r\n"}}
{"actor":{"display_login":"alidcastano"}}