This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a private "Dashboard" route. Add links for easier access.
- Loading branch information
1 parent
b853647
commit 609842c
Showing
5 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from "react"; | ||
|
||
export function Links() { | ||
return <ul> | ||
<li><a href={"/"}>Home</a></li> | ||
<li><a href={"/login"}>Login</a></li> | ||
<li><a href={"/dashboard"}>Dashboard (protected)</a></li> | ||
</ul> | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import ServerCookie from "next-cookies"; | ||
import Router from "next/router"; | ||
import React, { Component } from "react"; | ||
import { COOKIES } from "../services/login_service"; | ||
import { AuthToken } from "../services/auth_token"; | ||
|
||
export type AuthProps = { | ||
auth: AuthToken | ||
} | ||
|
||
type AuthState = { | ||
auth: AuthToken | ||
} | ||
|
||
export function privateRoute(WrappedComponent: any) { | ||
return class extends Component<AuthProps, AuthState> { | ||
state = { | ||
auth: new AuthToken(this.props.auth.token), | ||
}; | ||
|
||
static async getInitialProps(ctx: any) { | ||
const token = ServerCookie(ctx)[COOKIES.authToken]; | ||
const auth = new AuthToken(token); | ||
const initialProps = { auth }; | ||
if (WrappedComponent.getInitialProps) return WrappedComponent.getInitialProps(initialProps); | ||
return initialProps; | ||
} | ||
|
||
componentDidMount(): void { | ||
if (this.props.auth.decodedToken.exp === 0 || this.state.auth.isExpired) { | ||
Router.push("/login"); | ||
} | ||
} | ||
|
||
componentDidUpdate(): void { | ||
if (this.props.auth.decodedToken.exp === 0 || this.state.auth.isExpired) { | ||
Router.push("/login"); | ||
} | ||
} | ||
|
||
render() { | ||
// the server pass to the client serializes the token | ||
// so we have to reinitialize the authToken class | ||
// | ||
// @see https://github.com/zeit/next.js/issues/3536 | ||
// const auth =; | ||
return <WrappedComponent auth={this.state.auth} {...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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from "react" | ||
import { AuthProps, privateRoute } from "../components/private_route"; | ||
import Cookie from "js-cookie"; | ||
import Router from "next/router"; | ||
import { COOKIES } from "../services/login_service"; | ||
|
||
function Page(props: AuthProps) { | ||
const logout = async () => { | ||
Cookie.remove(COOKIES.authToken); | ||
alert("logout"); | ||
await Router.push("/login"); | ||
}; | ||
|
||
return <> | ||
<p>Hello Dashboard</p> | ||
<p>{JSON.stringify(props.auth)}</p> | ||
<button onClick={logout}>Logout</button> | ||
</> | ||
} | ||
|
||
export default privateRoute(Page); |
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,12 +1,10 @@ | ||
import React from "react"; | ||
import { Links } from "../components/links"; | ||
|
||
function Page() { | ||
return <> | ||
<ul> | ||
<li><a href={"/"}>Home</a></li> | ||
<li><a href={"/login"}>Login</a></li> | ||
</ul> | ||
</>; | ||
return <> | ||
<Links/> | ||
</>; | ||
} | ||
|
||
export default Page; |
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