-
Notifications
You must be signed in to change notification settings - Fork 11
/
ssr-profile.js
37 lines (31 loc) · 1005 Bytes
/
ssr-profile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// This import is only included in the server build, because it's only used by getServerSideProps
import auth0 from '../../lib/auth0'
import Layout from '../../components/layout'
function Profile({ user }) {
return (
<Layout user={user}>
<h1>Profile</h1>
<div>
<h3>Profile (server rendered)</h3>
<img src={user.picture} alt="user" />
<p>Nickname: {user.nickname}</p>
<p>Name: {user.name}</p>
</div>
</Layout>
)
}
export async function getServerSideProps({ req, res }) {
// 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 session = await auth0.getSession(req)
if (!session || !session.user) {
res.writeHead(302, {
Location: '/api/login',
})
res.end()
return
}
return { props: { user: session.user } }
}
export default Profile