import cookie from 'cookie' ; import { NextContext , NextFC } from 'next' ; import { AppContext } from 'next-with-apollo' ; import Router from 'next/router' ; import { SignInDocument , SignInMutation } from '../__generated__/graphql' ; const Callback = ( ) => null ; Callback . getInitialProps = async ( context ) => { if ( ! context . query . code ) redirect ( context , '/' ) ; if ( context . res ) { const { data } = await context . apolloClient . mutate < SignInMutation > ( { mutation : SignInDocument , variables : { gitHubCode : context . query . code , } , } ) ; if ( data ) { context . res . setHeader ( 'Set-Cookie' , cookie . serialize ( 'token' , data . signIn . token , { httpOnly : true } ) ) ; } } redirect ( context , ( context . query . from as string ) || '/' ) ; return { } ; } ; function redirect ( context , path ) { if ( context . res ) { context . res . writeHead ( 302 , { Location : path } ) ; context . res . end ( ) ; } else { Router . replace ( path ) ; } } export default Callback ;