forked from vercel/next.js
/
index.js
174 lines (161 loc) · 4.99 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import React from 'react'
import Head from 'next/head'
import styles from '../styles/Home.module.css'
import ThirdPartyEmailPassword from 'supertokens-auth-react/recipe/thirdpartyemailpassword'
import dynamic from 'next/dynamic'
import supertokensNode from 'supertokens-node'
import { backendConfig } from '../config/backendConfig'
import Session from 'supertokens-node/recipe/session'
const ThirdPartyEmailPasswordAuthNoSSR = dynamic(
new Promise((res) =>
res(ThirdPartyEmailPassword.ThirdPartyEmailPasswordAuth)
),
{ ssr: false }
)
export async function getServerSideProps(context) {
// this runs on the backend, so we must call init on supertokens-node SDK
supertokensNode.init(backendConfig())
let session
try {
session = await Session.getSession(context.req, context.res)
} catch (err) {
if (err.type === Session.Error.TRY_REFRESH_TOKEN) {
return { props: { fromSupertokens: 'needs-refresh' } }
} else if (err.type === Session.Error.UNAUTHORISED) {
return { props: {} }
} else {
throw err
}
}
return {
props: { userId: session.getUserId() },
}
}
export default function Home(props) {
return (
<ThirdPartyEmailPasswordAuthNoSSR>
<ProtectedPage userId={props.userId} />
</ThirdPartyEmailPasswordAuthNoSSR>
)
}
function ProtectedPage({ userId }) {
async function logoutClicked() {
await ThirdPartyEmailPassword.signOut()
ThirdPartyEmailPassword.redirectToAuth()
}
async function fetchUserData() {
const res = await fetch('/api/user')
if (res.status === 200) {
const json = await res.json()
alert(JSON.stringify(json))
}
}
return (
<div className={styles.container}>
<Head>
<title>SuperTokens 💫</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main className={styles.main}>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<p className={styles.description}>
You are authenticated with SuperTokens! (UserID: {userId})
</p>
<div
style={{
display: 'flex',
height: '70px',
alignItems: 'center',
justifyContent: 'flex-end',
paddingLeft: '75px',
paddingRight: '75px',
}}
>
<div
onClick={logoutClicked}
style={{
display: 'flex',
width: '116px',
height: '42px',
backgroundColor: '#000000',
borderRadius: '10px',
cursor: 'pointer',
alignItems: 'center',
justifyContent: 'center',
color: '#ffffff',
fontWeight: 'bold',
}}
>
SIGN OUT
</div>
</div>
<div
style={{
display: 'flex',
height: '70px',
alignItems: 'center',
justifyContent: 'flex-end',
paddingLeft: '75px',
paddingRight: '75px',
}}
>
<div
onClick={fetchUserData}
style={{
display: 'flex',
width: '150px',
height: '42px',
backgroundColor: 'rgb(247 54 54)',
borderRadius: '10px',
cursor: 'pointer',
alignItems: 'center',
justifyContent: 'center',
color: '#ffffff',
fontWeight: 'bold',
}}
>
FETCH USER API
</div>
</div>
<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h3>Documentation →</h3>
<p>Find in-depth information about Next.js features and API.</p>
</a>
<a href="https://nextjs.org/learn" className={styles.card}>
<h3>Learn →</h3>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/canary/examples"
className={styles.card}
>
<h3>Examples →</h3>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>
<a
href="https://vercel.com/import?filter=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
className={styles.card}
>
<h3>Deploy →</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<img src="/vercel.svg" alt="Vercel Logo" className={styles.logo} />
</a>
</footer>
</div>
)
}