-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
SignInPage.tsx
286 lines (259 loc) · 11.2 KB
/
SignInPage.tsx
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import React, { useEffect, useState } from 'react'
import { mdiBitbucket, mdiChevronLeft, mdiGithub, mdiGitlab, mdiKeyVariant, mdiMicrosoftAzureDevops } from '@mdi/js'
import classNames from 'classnames'
import { partition } from 'lodash'
import { Navigate, useLocation, useSearchParams } from 'react-router-dom'
import type { TelemetryV2Props } from '@sourcegraph/shared/src/telemetry'
import { EVENT_LOGGER } from '@sourcegraph/shared/src/telemetry/web/eventLogger'
import { Alert, Icon, Text, Link, Button, ErrorAlert, AnchorLink, Container } from '@sourcegraph/wildcard'
import type { AuthenticatedUser } from '../auth'
import { PageTitle } from '../components/PageTitle'
import type { AuthProvider, SourcegraphContext } from '../jscontext'
import { checkRequestAccessAllowed } from '../util/checkRequestAccessAllowed'
import { AuthPageWrapper } from './AuthPageWrapper'
import { OrDivider } from './OrDivider'
import { getReturnTo } from './SignInSignUpCommon'
import { UsernamePasswordSignInForm } from './UsernamePasswordSignInForm'
import styles from './SignInPage.module.scss'
export interface SignInPageProps extends TelemetryV2Props {
authenticatedUser: AuthenticatedUser | null
context: Pick<
SourcegraphContext,
| 'allowSignup'
| 'authProviders'
| 'sourcegraphDotComMode'
| 'primaryLoginProvidersCount'
// needed for checkRequestAccessAllowed
| 'authAccessRequest'
// needed for UsernamePasswordSignInForm
| 'xhrHeaders'
| 'resetPasswordEnabled'
>
}
export const SignInPage: React.FunctionComponent<React.PropsWithChildren<SignInPageProps>> = props => {
const { context, authenticatedUser } = props
useEffect(() => {
EVENT_LOGGER.logViewEvent('SignIn', null, false)
props.telemetryRecorder.recordEvent('auth.signIn', 'view')
}, [props.telemetryRecorder])
const location = useLocation()
const [error, setError] = useState<Error | null>(null)
const [searchParams, setSearchParams] = useSearchParams()
const isRequestAccessAllowed = checkRequestAccessAllowed(props.context)
const returnTo = getReturnTo(location)
if (authenticatedUser) {
return <Navigate to={returnTo} replace={true} />
}
const [[builtInAuthProvider], nonBuiltinAuthProviders] = partition(
context.authProviders,
provider => provider.isBuiltin
)
const shouldShowProvider = function (provider: AuthProvider): boolean {
if (provider.noSignIn) {
return false
}
// Hide the Sourcegraph Operator authentication provider by default because it is
// not useful to customer users and may even cause confusion.
if (provider.serviceType === 'sourcegraph-operator') {
return searchParams.has('sourcegraph-operator')
}
if (provider.serviceType === 'gerrit') {
return false
}
return true
}
const toggleMoreProviders = (showMore: boolean): void => {
const param = 'showMore'
if (showMore) {
searchParams.set(param, '')
} else {
searchParams.delete(param)
}
setSearchParams(searchParams)
}
const thirdPartyAuthProviders = nonBuiltinAuthProviders.filter(provider => shouldShowProvider(provider))
// If there is only one auth provider that is going to be displayed, and request access is disabled, we want to redirect to the auth provider directly.
if (context.sourcegraphDotComMode && thirdPartyAuthProviders.length === 1) {
// Add '?returnTo=' + encodeURIComponent(returnTo) to thirdPartyAuthProviders[0].authenticationURL in a safe way.
const redirectUrl = new URL(thirdPartyAuthProviders[0].authenticationURL, window.location.href)
if (returnTo) {
redirectUrl.searchParams.set('returnTo', new URL(returnTo, window.location.href).toString())
}
window.location.replace(redirectUrl)
return (
<>
<PageTitle title="Signing in..." />
<AuthPageWrapper
title="Redirecting to sign in..."
sourcegraphDotComMode={context.sourcegraphDotComMode}
className={styles.wrapper}
>
<Alert className="mt-3" variant="info">
You are being redirected to sign in with {thirdPartyAuthProviders[0].displayName}.
</Alert>
</AuthPageWrapper>
</>
)
}
const primaryProviders = thirdPartyAuthProviders.slice(0, context.primaryLoginProvidersCount)
const moreProviders = thirdPartyAuthProviders.slice(context.primaryLoginProvidersCount)
const showMoreProviders = searchParams.has('showMore') && (builtInAuthProvider || moreProviders.length > 0)
const hasProviders = builtInAuthProvider || thirdPartyAuthProviders.length > 0
const showMoreWaysToLogin =
!showMoreProviders && (moreProviders.length > 0 || (primaryProviders.length > 0 && builtInAuthProvider))
const providers = showMoreProviders ? moreProviders : primaryProviders
const body = !hasProviders ? (
<Alert className="mt-3" variant="info">
No authentication providers are available. Contact a site administrator for help.
</Alert>
) : (
<>
{error && <ErrorAlert error={error} />}
<Container className="test-signin-form">
{showMoreProviders && (
<div className="mb-3 text-left">
<Button
variant="link"
className="p-0 border-0 font-weight-normal"
onClick={() => toggleMoreProviders(false)}
>
<Icon aria-hidden={true} svgPath={mdiChevronLeft} />
Back
</Button>
</div>
)}
{builtInAuthProvider && (showMoreProviders || thirdPartyAuthProviders.length === 0) && (
<UsernamePasswordSignInForm
{...props}
onAuthError={setError}
className={classNames({ 'mb-3': providers.length > 0 })}
/>
)}
{builtInAuthProvider && showMoreProviders && providers.length > 0 && (
<OrDivider className="mb-3 py-1" />
)}
{providers.map((provider, index) => {
// Use index as key because display name may not be unique. This is OK
// here because this list will not be updated during this component's lifetime.
/* eslint-disable react/no-array-index-key */
const authURL = new URL(provider.authenticationURL, window.location.href)
if (returnTo) {
// propagate return to callback parameter
authURL.searchParams.set('returnTo', returnTo)
}
return (
// Only add botton margin to every but the last providers.
<div className={classNames(index !== providers.length - 1 && 'mb-2')} key={index}>
<Button
to={authURL.toString()}
display="block"
variant={showMoreProviders ? 'secondary' : 'primary'}
as={AnchorLink}
>
<ProviderIcon serviceType={provider.serviceType} />{' '}
{provider.displayPrefix ?? 'Continue with'} {provider.displayName}
</Button>
</div>
)
})}
{showMoreWaysToLogin && (
<Button
display="block"
className="mt-2"
variant="secondary"
onClick={() => toggleMoreProviders(true)}
>
<Icon aria-hidden={true} svgPath={mdiKeyVariant} /> Other login methods
</Button>
)}
</Container>
<SignUpNotice
allowSignup={context.allowSignup}
isRequestAccessAllowed={isRequestAccessAllowed}
sourcegraphDotComMode={context.sourcegraphDotComMode}
telemetryRecorder={props.telemetryRecorder}
/>
</>
)
return (
<>
<PageTitle title="Sign in" />
<AuthPageWrapper
title="Sign in to Sourcegraph"
sourcegraphDotComMode={context.sourcegraphDotComMode}
className={styles.wrapper}
>
{body}
</AuthPageWrapper>
</>
)
}
const ProviderIcon: React.FunctionComponent<{ serviceType: AuthProvider['serviceType'] }> = ({ serviceType }) => {
switch (serviceType) {
case 'github': {
return <Icon aria-hidden={true} svgPath={mdiGithub} />
}
case 'gitlab': {
return <Icon aria-hidden={true} svgPath={mdiGitlab} />
}
case 'bitbucketCloud': {
return <Icon aria-hidden={true} svgPath={mdiBitbucket} />
}
case 'azuredevops': {
return <Icon aria-hidden={true} svgPath={mdiMicrosoftAzureDevops} />
}
default: {
return null
}
}
}
interface SignUpNoticeProps extends TelemetryV2Props {
allowSignup: boolean
sourcegraphDotComMode: boolean
isRequestAccessAllowed: boolean
}
const SignUpNotice: React.FunctionComponent<SignUpNoticeProps> = ({
allowSignup,
sourcegraphDotComMode,
isRequestAccessAllowed,
telemetryRecorder,
}) => {
const dotcomCTAs = (
<>
<Link
to="https://sourcegraph.com/get-started?t=enterprise"
onClick={() => {
EVENT_LOGGER.log('ClickedOnEnterpriseCTA', { location: 'SignInPage' })
telemetryRecorder.recordEvent('auth.enterpriseCTA', 'click')
}}
>
consider Sourcegraph Enterprise
</Link>
.
</>
)
if (allowSignup) {
return (
<Text className="mt-3 text-center">
New to Sourcegraph? <Link to="/sign-up">Sign up</Link>{' '}
{sourcegraphDotComMode && <>To use Sourcegraph on private repositories, {dotcomCTAs}</>}
</Text>
)
}
if (isRequestAccessAllowed) {
return (
<Text className="mt-3 text-center text-muted">
Need an account? <Link to="/request-access">Request access</Link> or contact your site admin.
</Text>
)
}
if (sourcegraphDotComMode) {
return (
<Text className="mt-3 text-center text-muted">
Currently, we are unable to create accounts using email. Please use the providers listed above to
continue. <br /> For private code, {dotcomCTAs}
</Text>
)
}
return <Text className="mt-3 text-center text-muted">Need an account? Contact your site admin.</Text>
}