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
aff9ba6
commit 8af1b3f
Showing
5 changed files
with
74 additions
and
75 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
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,14 +1,14 @@ | ||
import { UserProvider } from '@auth0/nextjs-auth0' | ||
|
||
export default function App({ Component, pageProps }) { | ||
// optionally pass the 'user' prop from pages that require server-side | ||
// rendering to prepopulate the 'useUser' hook. | ||
|
||
const { user } = pageProps | ||
|
||
return ( | ||
<UserProvider user={user}> | ||
<Component {...pageProps} /> | ||
</UserProvider> | ||
) | ||
} | ||
import { UserProvider } from '@auth0/nextjs-auth0' | ||
|
||
export default function App({ Component, pageProps }) { | ||
// optionally pass the 'user' prop from pages that require server-side | ||
// rendering to prepopulate the 'useUser' hook. | ||
|
||
const { user } = pageProps | ||
|
||
return ( | ||
<UserProvider user={user}> | ||
<Component {...pageProps} /> | ||
</UserProvider> | ||
) | ||
} |
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,37 +1,37 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useUser } from '@auth0/nextjs-auth0' | ||
import Layout from '../../components/layout' | ||
|
||
const ApiProfile = () => { | ||
const { user, isLoading } = useUser() | ||
|
||
const [data, setData] = useState(null) | ||
|
||
useEffect(() => { | ||
(async () => { | ||
const res = await fetch('/api/protected-api') | ||
|
||
const data = await res.json() | ||
|
||
setData(data) | ||
})() | ||
}, []) | ||
|
||
return ( | ||
<Layout user={user} loading={isLoading}> | ||
<h1>Profile</h1> | ||
|
||
<div> | ||
<h3>Public page (client rendered)</h3> | ||
<p>We are fetching data on the client-side :</p> | ||
<p>By making request to '/api/protected-api' serverless function</p> | ||
<p>so without a valid session cookie will fail</p> | ||
<p>{JSON.stringify(data)}</p> | ||
</div> | ||
</Layout> | ||
) | ||
} | ||
|
||
// Public route.(CSR) also accessing API from the client-side. | ||
// data is not cached when redirecting between pages. | ||
export default ApiProfile | ||
import { useEffect, useState } from 'react' | ||
import { useUser } from '@auth0/nextjs-auth0' | ||
import Layout from '../../components/layout' | ||
|
||
const ApiProfile = () => { | ||
const { user, isLoading } = useUser() | ||
|
||
const [data, setData] = useState(null) | ||
|
||
useEffect(() => { | ||
;(async () => { | ||
const res = await fetch('/api/protected-api') | ||
|
||
const data = await res.json() | ||
|
||
setData(data) | ||
})() | ||
}, []) | ||
|
||
return ( | ||
<Layout user={user} loading={isLoading}> | ||
<h1>Profile</h1> | ||
|
||
<div> | ||
<h3>Public page (client rendered)</h3> | ||
<p>We are fetching data on the client-side :</p> | ||
<p>By making request to '/api/protected-api' serverless function</p> | ||
<p>so without a valid session cookie will fail</p> | ||
<p>{JSON.stringify(data)}</p> | ||
</div> | ||
</Layout> | ||
) | ||
} | ||
|
||
// Public route.(CSR) also accessing API from the client-side. | ||
// data is not cached when redirecting between pages. | ||
export default ApiProfile |
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,3 +1,3 @@ | ||
import { handleAuth } from '@auth0/nextjs-auth0' | ||
|
||
export default handleAuth() | ||
import { handleAuth } from '@auth0/nextjs-auth0' | ||
|
||
export default handleAuth() |
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,20 +1,20 @@ | ||
import { withApiAuthRequired, getSession } from '@auth0/nextjs-auth0' | ||
|
||
// Serverless function | ||
// Protected API, requests to '/api/protected' without a valid session cookie will fail | ||
|
||
async function handle(req, res) { | ||
const { user } = getSession(req, res) | ||
|
||
try { | ||
res.status(200).json({ | ||
session: 'true', | ||
id: user.sub, | ||
nickname: user.nickname, | ||
}) | ||
} catch (e) { | ||
res.status(500).json({ error: 'Unable to fetch', description: e }) | ||
} | ||
} | ||
|
||
export default withApiAuthRequired(handle) | ||
import { withApiAuthRequired, getSession } from '@auth0/nextjs-auth0' | ||
|
||
// Serverless function | ||
// Protected API, requests to '/api/protected' without a valid session cookie will fail | ||
|
||
async function handle(req, res) { | ||
const { user } = getSession(req, res) | ||
|
||
try { | ||
res.status(200).json({ | ||
session: 'true', | ||
id: user.sub, | ||
nickname: user.nickname, | ||
}) | ||
} catch (e) { | ||
res.status(500).json({ error: 'Unable to fetch', description: e }) | ||
} | ||
} | ||
|
||
export default withApiAuthRequired(handle) |