Unverified Commit f2315ffc authored by Freddy Jimenez's avatar Freddy Jimenez Committed by GitHub

update auth0 example with getServerSideProps (#11051)

* example improved

* put user cache back

* .env.template is back

* .env.template sorting back

* Header component calls fixed

* Context API removed

* Context API related text removed from README

* put everything back but getServerSideProps

* client side code removed from GSSP

* Updated comments
Co-authored-by: 's avatarLuis Alvarez <luis@zeit.co>
parent b307ed91
import React from 'react' // This import is only included in the server build, because it's only used by getServerSideProps
// This import is only needed when checking authentication status directly from getInitialProps
import auth0 from '../../lib/auth0' import auth0 from '../../lib/auth0'
import { fetchUser } from '../../lib/user'
import Layout from '../../components/layout' import Layout from '../../components/layout'
function Profile({ user }) { function Profile({ user }) {
...@@ -20,12 +17,12 @@ function Profile({ user }) { ...@@ -20,12 +17,12 @@ function Profile({ user }) {
) )
} }
Profile.getInitialProps = async ({ req, res }) => { export async function getServerSideProps({ req, res }) {
// On the server-side you can check authentication status directly // Here you can check authentication status directly before rendering the page,
// However in general you might want to call API Routes to fetch data // however the page would be a serverless function, which is more expensive and
// An example of directly checking authentication: // slower than a static page with client side authentication
if (typeof window === 'undefined') {
const { user } = await auth0.getSession(req) const { user } = await auth0.getSession(req)
if (!user) { if (!user) {
res.writeHead(302, { res.writeHead(302, {
Location: '/api/login', Location: '/api/login',
...@@ -33,28 +30,8 @@ Profile.getInitialProps = async ({ req, res }) => { ...@@ -33,28 +30,8 @@ Profile.getInitialProps = async ({ req, res }) => {
res.end() res.end()
return 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)
// 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'
}
return { user } return { props: { user } }
} }
export default Profile export default Profile
...@@ -11,7 +11,7 @@ function ProfileCard({ user }) { ...@@ -11,7 +11,7 @@ function ProfileCard({ user }) {
<h1>Profile</h1> <h1>Profile</h1>
<div> <div>
<h3>Profile (server rendered)</h3> <h3>Profile (client rendered)</h3>
<img src={user.picture} alt="user picture" /> <img src={user.picture} alt="user picture" />
<p>nickname: {user.nickname}</p> <p>nickname: {user.nickname}</p>
<p>name: {user.name}</p> <p>name: {user.name}</p>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment