-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57268fc
commit 29c30c1
Showing
8 changed files
with
148 additions
and
210 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 @@ | ||
// mock login and logout | ||
export function login() { | ||
// add cookie | ||
document.cookie = "authorization=swr;"; | ||
} | ||
export function logout() { | ||
// delete cookie | ||
document.cookie = "authorization=; expires=Thu, 01 Jan 1970 00:00:01 GMT;"; | ||
} |
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,37 @@ | ||
import useSWR from "swr"; | ||
import { logout } from "./auth"; | ||
|
||
type User = { | ||
name: string; | ||
}; | ||
|
||
// mock the user api | ||
const userFetcher = async (): Promise<User | null> => { | ||
// sleep 500 | ||
await new Promise((res) => setTimeout(res, 500)); | ||
|
||
if (document.cookie.includes("authorization=swr")) { | ||
// authorized | ||
return { | ||
name: "Fernando", | ||
}; | ||
} | ||
|
||
// not authorized | ||
logout(); | ||
throw new Error("not authorized"); | ||
}; | ||
|
||
export default function useUser() { | ||
const { data, mutate, error } = useSWR("api_user", userFetcher); | ||
|
||
const loading = !data && !error; | ||
const loggedOut = !!error; | ||
|
||
return { | ||
loading, | ||
loggedOut, | ||
user: data, | ||
mutate, | ||
}; | ||
} |
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,7 +1,6 @@ | ||
import '../styles/globals.css' | ||
import type { AppProps } from 'next/app' | ||
import type { AppProps } from "next/app"; | ||
|
||
function MyApp({ Component, pageProps }: AppProps) { | ||
return <Component {...pageProps} /> | ||
return <Component {...pageProps} />; | ||
} | ||
export default MyApp | ||
export default MyApp; |
This file was deleted.
Oops, something went wrong.
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,47 @@ | ||
import { useEffect } from "react"; | ||
import Router from "next/router"; | ||
import Head from "next/head"; | ||
|
||
import useUser from "../lib/useUser"; | ||
import { login } from "../lib/auth"; | ||
|
||
export default function App() { | ||
const { user, mutate } = useUser(); | ||
|
||
// if logged in, redirect to the protected route | ||
useEffect(() => { | ||
if (user) { | ||
Router.replace("/protected"); | ||
} | ||
}, [user]); | ||
|
||
return ( | ||
<> | ||
<Head> | ||
<script | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
if (document.cookie && document.cookie.indexOf('authorization=') > -1) { | ||
location.replace('/protected') | ||
}`, | ||
}} | ||
/> | ||
</Head> | ||
<div className="homepage"> | ||
<main> | ||
<h1>ACME!</h1> | ||
<p>Build Something Brilliant</p> | ||
<br /> | ||
<button | ||
onClick={() => { | ||
login(); | ||
mutate(); // after logging in, we revalidate the SWR | ||
}} | ||
> | ||
Login To Continue | ||
</button> | ||
</main> | ||
</div> | ||
</> | ||
); | ||
} |
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,52 @@ | ||
import { useEffect } from "react"; | ||
import Head from "next/head"; | ||
import Router from "next/router"; | ||
|
||
import useUser from "../lib/useUser"; | ||
import { logout } from "../lib/auth"; | ||
|
||
export default function Dashboard() { | ||
const { user, loading, loggedOut, mutate } = useUser(); | ||
|
||
// if logged out, redirect to the homepage | ||
useEffect(() => { | ||
if (loggedOut) { | ||
Router.replace("/login"); | ||
} | ||
}, [loggedOut]); | ||
if (loggedOut) return "redirecting..."; | ||
|
||
return ( | ||
<div> | ||
<main> | ||
<Head> | ||
<script | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
if (!document.cookie || document.cookie.indexOf('authorization=') === -1) { | ||
location.replace('/login') | ||
}`, | ||
}} | ||
/> | ||
</Head> | ||
{loading || !user ? ( | ||
"loading..." | ||
) : ( | ||
<> | ||
<h1>Welcome, {user.name}</h1> | ||
<p>This is your dashboard.</p> | ||
<button | ||
onClick={() => { | ||
logout(); | ||
mutate(null); // optimistically update the data and revalidate | ||
Router.replace("/login"); | ||
}} | ||
> | ||
Logout | ||
</button> | ||
</> | ||
)} | ||
</main> | ||
</div> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
29c30c1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: