{"size": 1544, "ext": "js", "max_stars_repo_path": "ReactPlatzi/todo-app-client-main/src/pages/_document.js", "max_stars_repo_name": "jhonnyagudelo/todosLosCursos", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "ReactPlatzi/todo-app-client-main/src/pages/_document.js", "max_issues_repo_name": "jhonnyagudelo/todosLosCursos", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "ReactPlatzi/todo-app-client-main/src/pages/_document.js", "max_forks_repo_name": "jhonnyagudelo/todosLosCursos", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.5079365079, "max_line_length": 62, "alphanum_fraction": 0.5382124352}
import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components'
import { Router } from 'next/router'
import { parseCookies } from 'nookies'

export default class MyDocument extends Document {
  static async getInitialProps (ctx) {
    const sheet = new ServerStyleSheet()
    const originalRenderPage = ctx.renderPage

    try {
      ctx.renderPage = () =>
        originalRenderPage({
          enhanceApp: (App) => (props) =>
            sheet.collectStyles(<App {...props} />)
        })

      const initialProps = await Document.getInitialProps(ctx)
      const { 'auth-token': authToken } = parseCookies(ctx)

      if (!authToken) {
        if (ctx.pathname === '/dashboard') {
          redirectUser(ctx, '/login')
        }
      } else {
        if (ctx.pathname === '/') {
          redirectUser(ctx, '/dashboard')
        }
        if (ctx.pathname === '/login') {
          redirectUser(ctx, '/dashboard')
        }
        if (ctx.pathname === '/join') {
          redirectUser(ctx, '/dashboard')
        }
        if (ctx.pathname === '/confirm') {
          redirectUser(ctx, '/dashboard')
        }
      }

      return {
        ...initialProps,
        styles: (
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>
        )
      }
    } finally {
      sheet.seal()
    }
  }
}

function redirectUser (ctx, location) {
  if (ctx.req) {
    ctx.res.writeHead(302, { Location: location })
    ctx.res.end()
  } else {
    Router.push(location)
  }
}
