Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update auth0 example with getServerSideProps #11051

Merged
merged 22 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
54dd562
example improved
alfrejivi Mar 13, 2020
2b96b2c
put user cache back
alfrejivi Mar 13, 2020
3512c41
Merge branch 'canary' into enhancement/auth0-example
alfrejivi Mar 13, 2020
018181e
.env.template is back
alfrejivi Mar 13, 2020
071d351
Merge branch 'enhancement/auth0-example' of https://github.com/alfrej…
alfrejivi Mar 13, 2020
d77646a
.env.template sorting back
alfrejivi Mar 13, 2020
67fadf4
Merge branch 'canary' into enhancement/auth0-example
alfrejivi Mar 14, 2020
6ba4b60
Merge branch 'canary' into enhancement/auth0-example
alfrejivi Mar 15, 2020
8189f36
Merge branch 'canary' into enhancement/auth0-example
alfrejivi Mar 16, 2020
bf706f8
Merge branch 'canary' into enhancement/auth0-example
alfrejivi Mar 23, 2020
1b423a5
Merge branch 'canary' into enhancement/auth0-example
alfrejivi Mar 24, 2020
fc129fc
Header component calls fixed
alfrejivi Mar 24, 2020
b761d7c
Context API removed
alfrejivi Mar 24, 2020
2a08e8d
Context API related text removed from README
alfrejivi Mar 24, 2020
8d16e8c
Merge branch 'canary' into enhancement/auth0-example
alfrejivi Mar 24, 2020
7d54345
put everything back but getServerSideProps
alfrejivi Mar 25, 2020
7fe4971
Merge branch 'enhancement/auth0-example' of https://github.com/alfrej…
alfrejivi Mar 25, 2020
b775b53
Merge branch 'canary' into enhancement/auth0-example
alfrejivi Mar 25, 2020
5477e71
client side code removed from GSSP
alfrejivi Mar 25, 2020
f2d3928
merge changes
alfrejivi Mar 25, 2020
cd190fb
Merge branch 'canary' into enhancement/auth0-example
alfrejivi Mar 31, 2020
1fb4ae1
Updated comments
Mar 31, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 12 additions & 35 deletions examples/auth0/pages/advanced/ssr-profile.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import React from 'react'

// This import is only needed when checking authentication status directly from getInitialProps
// This import is only included in the server build, because it's only used by getServerSideProps
import auth0 from '../../lib/auth0'
import { fetchUser } from '../../lib/user'
import Layout from '../../components/layout'

function Profile({ user }) {
Expand All @@ -20,41 +17,21 @@ function Profile({ user }) {
)
}

Profile.getInitialProps = async ({ req, res }) => {
// On the server-side you can check authentication status directly
// However in general you might want to call API Routes to fetch data
// An example of directly checking authentication:
if (typeof window === 'undefined') {
const { user } = await auth0.getSession(req)
if (!user) {
res.writeHead(302, {
Location: '/api/login',
})
res.end()
return
}
return { user }
}

// To do fetches to API routes you can pass the cookie coming from the incoming request on to the fetch
// so that a request to the API is done on behalf of the user
// keep in mind that server-side fetches need a full URL, meaning that the full url has to be provided to the application
const cookie = req && req.headers.cookie
const user = await fetchUser(cookie)
export async function getServerSideProps({ req, res }) {
Copy link
Member

Choose a reason for hiding this comment

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

In this function, remove anything that's not for the server, because getServerSideProps will only execute in the server, meaning anything that doesn't match typeof window === 'undefined'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lol, I did that before, I forgot to leave it in the last commit

// Here you can check authentication status directly before rendering the page,
// however the page would be a serverless function, which is more expensive and
// slower than a static page with client side authentication
const { user } = await auth0.getSession(req)

// A redirect is needed to authenticate to Auth0
if (!user) {
if (typeof window === 'undefined') {
res.writeHead(302, {
Location: '/api/login',
})
return res.end()
}

window.location.href = '/api/login'
res.writeHead(302, {
Location: '/api/login',
})
res.end()
return
}

return { user }
return { props: { user } }
}

export default Profile
2 changes: 1 addition & 1 deletion examples/auth0/pages/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function ProfileCard({ user }) {
<h1>Profile</h1>

<div>
<h3>Profile (server rendered)</h3>
<h3>Profile (client rendered)</h3>
<img src={user.picture} alt="user picture" />
<p>nickname: {user.nickname}</p>
<p>name: {user.name}</p>
Expand Down