-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.tsx
More file actions
215 lines (197 loc) · 7.62 KB
/
index.tsx
File metadata and controls
215 lines (197 loc) · 7.62 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import React, { useState } from 'react'
import Image from 'next/image'
import supertokensNode from 'supertokens-node'
import * as SuperTokensConfig from '../../config/backendConfig'
import Session from 'supertokens-node/recipe/session'
import { GetServerSideProps } from 'next'
import Page from 'components/Page'
import ChangePassword from 'components/Account/ChangePassword'
import ChangeFiatCurrency from 'components/Account/ChangeFiatCurrency'
import CsvRowCollapsing from 'components/Account/CsvRowCollapsing'
import style from './account.module.css'
import { fetchUserProfileFromId, fetchUserWithSupertokens, getUserPublicKeyHex, UserWithSupertokens } from 'services/userService'
import CopyIcon from '../../assets/copy-black.png'
import { ViewOrganization } from 'components/Organization'
import { fetchOrganizationForUser, fetchOrganizationMembers } from 'services/organizationService'
import { Organization, UserProfile } from '@prisma/client'
import { removeDateFields, removeUnserializableFields } from 'utils/index'
import TopBar from 'components/TopBar'
import TimezoneSelector from 'components/Timezone Selector'
import moment from 'moment-timezone'
import config from 'config'
import ProDisplay from 'components/Account/ProDisplay'
export const getServerSideProps: GetServerSideProps = async (context) => {
supertokensNode.init(SuperTokensConfig.backendConfig())
let session
try {
session = await Session.getSession(context.req, context.res)
} catch (err: any) {
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
}
}
if (session === undefined) return
const userId = session.getUserId()
const user = await fetchUserWithSupertokens(userId)
removeUnserializableFields(user.userProfile)
const organization = await fetchOrganizationForUser(userId)
const userProfile = await fetchUserProfileFromId(userId)
let serializableOrg = null
let members: UserProfile[] = []
if (organization !== null) {
members = await fetchOrganizationMembers(organization.id)
members.forEach(m => removeUnserializableFields(m))
serializableOrg = removeDateFields(organization)
} else {
members = []
}
removeUnserializableFields(userProfile)
return {
props: {
organization: serializableOrg,
orgMembersProps: members,
user,
userPublicKey: await getUserPublicKeyHex(userId),
userProfile
}
}
}
interface IProps {
user: UserWithSupertokens
userPublicKey: string
organization: Organization
orgMembersProps: UserProfile[]
userProfile: UserProfile
}
export default function Account ({ user, userPublicKey, organization, orgMembersProps, userProfile }: IProps): React.ReactElement {
const [changePassword, setChangePassword] = useState(false)
const [publicKeyInfo, setPublicKeyInfo] = useState(false)
const [isCopied, setIsCopied] = useState(false)
const [orgMembers, setOrgMembers] = useState(orgMembersProps)
const [timezone, setTimezone] = useState(userProfile?.preferredTimezone !== '' ? userProfile?.preferredTimezone : moment.tz.guess())
const handleTimezoneChange = (selectedOption: any): void => {
setTimezone(selectedOption)
}
const toggleChangePassword = (): void => {
setChangePassword(!changePassword)
}
const togglePublicKeyInfo = (): void => {
setPublicKeyInfo(!publicKeyInfo)
}
const handleCopyClick = async (): Promise<void> => {
const textToCopy = userPublicKey
try {
await navigator.clipboard.writeText(textToCopy)
setIsCopied(true)
setTimeout(() => {
setIsCopied(false)
}, 1000)
} catch (error) {
console.error('Copy failed:', error)
}
}
if (user != null) {
return (
<div className={style.account_ctn}>
<TopBar title="Account" user={user.stUser?.email} />
<div className={style.account_columns}>
<div className={style.account_column}>
<h4 className={style.section_header}>General</h4>
<div className={style.account_card}>
<div className={style.account_row}>
<div className={style.label}>Email</div>
<div className={style.value}>{user.stUser?.email}</div>
</div>
<div className={style.account_row}>
<div className={style.label}>{changePassword ? 'Update ' : ''}Password</div>
{changePassword
? null
: <div className={style.value}>••••••••</div>}
<div
onClick={() => setChangePassword(!changePassword)}
className={style.change_pw_btn}
>
{changePassword ? 'Cancel' : 'Update'}
</div>
{changePassword && <ChangePassword toggleChangePassword={toggleChangePassword} /> }
</div>
<div className={style.account_row}>
<div className={style.label}>Currency</div>
<div className={style.value}>
<ChangeFiatCurrency
preferredCurrencyId={userProfile.preferredCurrencyId}
/>
</div>
</div>
<div className={style.account_row}>
<div className={style.label}>Timezone</div>
<TimezoneSelector
value={timezone}
onChange={handleTimezoneChange}
/>
</div>
<div className={style.account_row}>
<div className={style.label}>CSV Row Collapsing</div>
<div className={style.value}>
<CsvRowCollapsing
initialValue={userProfile.csvRowCollapsing}
/>
</div>
</div>
<div className={style.account_row}>
<div className={style.label}>
Public Key{' '}
<div
onClick={() => togglePublicKeyInfo()}
className={style.whats_this_btn}
>
{publicKeyInfo ? 'Close' : 'What is this?'}
</div>
</div>
<div className={style.pk_ctn}>
<div className={style.pk_card}>
{userPublicKey}
{isCopied && (
<div className={style.copied_confirmation}>Copied!</div>
)}
</div>
<div
className={style.pk_copy}
onClick={() => {
void handleCopyClick()
}}
>
<Image src={CopyIcon} alt="copy" width={20} height={20} />
</div>
</div>
{publicKeyInfo && (
<div className={style.public_key_info_ctn}>
This can be used to verify the authenticity of a message received from a paybutton trigger.
<br/>
<br/>
To verify, check variable <signature> variable. It should contain two keys:
<br/>
- payload: The transaction data variables present in the POST request concatenated using the plus (+) symbol as a separator.
<br/>
- signature: The signature of the payload.
<br/>
<br/>
Check if the payload's signature came from the private key paired to this public key using your preferred method.
</div>
)}
</div>
</div>
</div>
<div className={style.account_column}>
{config.proSettings.enabled && <ProDisplay/>}
<ViewOrganization user={user} orgMembers={orgMembers} setOrgMembers={setOrgMembers} organization={organization}/>
</div>
</div>
</div>)
}
return <Page />
}