-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Updates logic for authentication
- Loading branch information
1 parent
6965cf0
commit 99151de
Showing
6 changed files
with
80 additions
and
49 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { setCookie } from 'nookies' | ||
import { setCookie, destroyCookie } from "nookies"; | ||
|
||
const cookieSetter = (key: string, value: string, ctx = null) => { | ||
setCookie(ctx, key, value, { | ||
maxAge: 30 * 24 * 60 * 60, | ||
path: '/', | ||
}) | ||
} | ||
path: "/" | ||
}); | ||
}; | ||
|
||
export { cookieSetter } | ||
const cookieRemover = (key: string) => { | ||
destroyCookie(null, key); | ||
}; | ||
|
||
export { cookieSetter, cookieRemover }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,71 @@ | ||
import { Component } from 'react' | ||
import { parseCookies } from 'nookies' | ||
import { NextPage } from 'next' | ||
import Router from 'next/router' | ||
import { Component } from "react"; | ||
import { parseCookies } from "nookies"; | ||
import { NextPage, NextPageContext, NextComponentType } from "next"; | ||
import Router from "next/router"; | ||
|
||
// Gets the display name of a JSX component for dev tools | ||
const getComponentDisplayName = (Component: any) => { | ||
return Component.displayName || Component.name || 'Unknown' | ||
interface Context extends NextPageContext { | ||
// any modifications to the default context, e.g. query types | ||
asPath: string; | ||
} | ||
|
||
// Gets the display name of a JSX component for dev tools | ||
const getComponentDisplayName = (Component: NextComponentType) => { | ||
return Component.displayName || Component.name || "Unknown"; | ||
}; | ||
|
||
export default (ComposedComponent: NextPage) => { | ||
return class WithAuthentication extends Component { | ||
static displayName = `WithAuthentication(${getComponentDisplayName( | ||
ComposedComponent | ||
)})` | ||
)})`; | ||
|
||
static async getInitialProps(ctx: any) { | ||
const isAuthenticated = !!parseCookies(ctx).token | ||
static async getInitialProps(ctx: Context) { | ||
const isAuthenticated = !!parseCookies(ctx).token; | ||
|
||
// Evaluate the composed component's getInitialProps() | ||
let composedInitialProps = {} | ||
let composedInitialProps = {}; | ||
|
||
if (ComposedComponent.getInitialProps) { | ||
composedInitialProps = await ComposedComponent.getInitialProps(ctx) | ||
composedInitialProps = await ComposedComponent.getInitialProps(ctx); | ||
} | ||
|
||
// When the user is authenticated, don't let the user visit the | ||
// sign-in and sign-up routes | ||
if ( | ||
isAuthenticated && | ||
['/sign-up', '/sign-in'].indexOf(ctx.asPath) > -1 | ||
["/sign-up", "/sign-in"].indexOf(ctx.asPath) > -1 | ||
) { | ||
if (typeof window !== 'undefined') { | ||
Router.push('/') | ||
if (typeof window !== "undefined") { | ||
Router.push("/"); | ||
} else { | ||
ctx.res.redirect('/') | ||
if (ctx.res) { | ||
ctx.res.writeHead(301, { | ||
Location: "/" | ||
}); | ||
ctx.res.end(); | ||
} | ||
} | ||
} else if (!isAuthenticated && ["/"].indexOf(ctx.asPath) > -1) { | ||
if (typeof window !== "undefined") { | ||
Router.push("/sign-up"); | ||
} else { | ||
if (ctx.res) { | ||
ctx.res.writeHead(301, { | ||
Location: "/sign-up" | ||
}); | ||
ctx.res.end(); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
...composedInitialProps, | ||
} | ||
isAuthenticated | ||
}; | ||
} | ||
|
||
render() { | ||
return <ComposedComponent {...this.props} /> | ||
return <ComposedComponent {...this.props} />; | ||
} | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters