Skip to content

Commit

Permalink
add auth example
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandobandeira committed May 31, 2021
1 parent 57268fc commit 29c30c1
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 210 deletions.
9 changes: 9 additions & 0 deletions lib/auth.tsx
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;";
}
37 changes: 37 additions & 0 deletions lib/useUser.tsx
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,
};
}
7 changes: 3 additions & 4 deletions pages/_app.tsx
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;
69 changes: 0 additions & 69 deletions pages/index.tsx

This file was deleted.

47 changes: 47 additions & 0 deletions pages/login.tsx
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>
</>
);
}
52 changes: 52 additions & 0 deletions pages/protected.tsx
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>
);
}
121 changes: 0 additions & 121 deletions styles/Home.module.css

This file was deleted.

16 changes: 0 additions & 16 deletions styles/globals.css

This file was deleted.

1 comment on commit 29c30c1

@vercel
Copy link

@vercel vercel bot commented on 29c30c1 May 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.