|
| 1 | +import React, { FormEvent } from 'react'; |
| 2 | +import { useId } from '../../hooks/hookPolyfills'; |
| 3 | +import { |
| 4 | + type CoderAuthStatus, |
| 5 | + useCoderAppConfig, |
| 6 | + useCoderAuth, |
| 7 | +} from '../CoderProvider'; |
| 8 | + |
| 9 | +import { Theme, makeStyles } from '@material-ui/core'; |
| 10 | +import TextField from '@material-ui/core/TextField'; |
| 11 | +import { CoderLogo } from '../CoderLogo'; |
| 12 | +import { Link, LinkButton } from '@backstage/core-components'; |
| 13 | +import { VisuallyHidden } from '../VisuallyHidden'; |
| 14 | + |
| 15 | +type UseStyleInput = Readonly<{ status: CoderAuthStatus }>; |
| 16 | +type StyleKeys = |
| 17 | + | 'formContainer' |
| 18 | + | 'authInputFieldset' |
| 19 | + | 'coderLogo' |
| 20 | + | 'authButton' |
| 21 | + | 'warningBanner' |
| 22 | + | 'warningBannerContainer'; |
| 23 | + |
| 24 | +const useStyles = makeStyles<Theme, UseStyleInput, StyleKeys>(theme => ({ |
| 25 | + formContainer: { |
| 26 | + maxWidth: '30em', |
| 27 | + marginLeft: 'auto', |
| 28 | + marginRight: 'auto', |
| 29 | + }, |
| 30 | + |
| 31 | + authInputFieldset: { |
| 32 | + display: 'flex', |
| 33 | + flexFlow: 'column nowrap', |
| 34 | + rowGap: theme.spacing(2), |
| 35 | + margin: `${theme.spacing(-0.5)} 0 0 0`, |
| 36 | + border: 'none', |
| 37 | + padding: 0, |
| 38 | + }, |
| 39 | + |
| 40 | + coderLogo: { |
| 41 | + display: 'block', |
| 42 | + width: 'fit-content', |
| 43 | + marginLeft: 'auto', |
| 44 | + marginRight: 'auto', |
| 45 | + }, |
| 46 | + |
| 47 | + authButton: { |
| 48 | + display: 'block', |
| 49 | + width: 'fit-content', |
| 50 | + marginLeft: 'auto', |
| 51 | + marginRight: 'auto', |
| 52 | + }, |
| 53 | + |
| 54 | + warningBannerContainer: { |
| 55 | + paddingTop: theme.spacing(4), |
| 56 | + paddingLeft: theme.spacing(6), |
| 57 | + paddingRight: theme.spacing(6), |
| 58 | + }, |
| 59 | + |
| 60 | + warningBanner: ({ status }) => { |
| 61 | + let color: string; |
| 62 | + let backgroundColor: string; |
| 63 | + |
| 64 | + if (status === 'invalid') { |
| 65 | + color = theme.palette.error.contrastText; |
| 66 | + backgroundColor = theme.palette.banner.error; |
| 67 | + } else { |
| 68 | + color = theme.palette.text.primary; |
| 69 | + backgroundColor = theme.palette.background.default; |
| 70 | + } |
| 71 | + |
| 72 | + return { |
| 73 | + color, |
| 74 | + backgroundColor, |
| 75 | + borderRadius: theme.shape.borderRadius, |
| 76 | + textAlign: 'center', |
| 77 | + paddingTop: theme.spacing(0.5), |
| 78 | + paddingBottom: theme.spacing(0.5), |
| 79 | + }; |
| 80 | + }, |
| 81 | +})); |
| 82 | + |
| 83 | +export const CoderAuthInputForm = () => { |
| 84 | + const hookId = useId(); |
| 85 | + const appConfig = useCoderAppConfig(); |
| 86 | + const { status, registerNewToken } = useCoderAuth(); |
| 87 | + const styles = useStyles({ status }); |
| 88 | + |
| 89 | + const onSubmit = (event: FormEvent<HTMLFormElement>) => { |
| 90 | + event.preventDefault(); |
| 91 | + const formData = Object.fromEntries(new FormData(event.currentTarget)); |
| 92 | + const newToken = |
| 93 | + typeof formData.authToken === 'string' ? formData.authToken : ''; |
| 94 | + |
| 95 | + registerNewToken(newToken); |
| 96 | + }; |
| 97 | + |
| 98 | + const legendId = `${hookId}-legend`; |
| 99 | + const authTokenInputId = `${hookId}-auth-token`; |
| 100 | + const warningBannerId = `${hookId}-warning-banner`; |
| 101 | + |
| 102 | + return ( |
| 103 | + <form className={styles.formContainer} onSubmit={onSubmit}> |
| 104 | + <div> |
| 105 | + <CoderLogo className={styles.coderLogo} /> |
| 106 | + <p> |
| 107 | + Your Coder session token is {mapAuthStatusToText(status)}. Please |
| 108 | + enter a new token from your{' '} |
| 109 | + <Link |
| 110 | + to={`${appConfig.deployment.accessUrl}/cli-auth`} |
| 111 | + target="_blank" |
| 112 | + > |
| 113 | + Coder deployment's token page |
| 114 | + <VisuallyHidden> (link opens in new tab)</VisuallyHidden> |
| 115 | + </Link> |
| 116 | + . |
| 117 | + </p> |
| 118 | + </div> |
| 119 | + |
| 120 | + <fieldset className={styles.authInputFieldset} aria-labelledby={legendId}> |
| 121 | + <legend hidden id={legendId}> |
| 122 | + Auth input |
| 123 | + </legend> |
| 124 | + |
| 125 | + <TextField |
| 126 | + // Adding the label prop directly to the TextField will place a label |
| 127 | + // in the HTML, so sighted users are fine. But for some reason, it |
| 128 | + // won't connect the label and input together, which breaks |
| 129 | + // accessibility for screen readers. Need to wire up extra IDs, sadly. |
| 130 | + label="Auth token" |
| 131 | + id={authTokenInputId} |
| 132 | + InputLabelProps={{ htmlFor: authTokenInputId }} |
| 133 | + required |
| 134 | + name="authToken" |
| 135 | + type="password" |
| 136 | + defaultValue="" |
| 137 | + aria-errormessage={warningBannerId} |
| 138 | + style={{ width: '100%' }} |
| 139 | + /> |
| 140 | + |
| 141 | + <LinkButton |
| 142 | + disableRipple |
| 143 | + to="" |
| 144 | + component="button" |
| 145 | + type="submit" |
| 146 | + color="primary" |
| 147 | + variant="contained" |
| 148 | + className={styles.authButton} |
| 149 | + > |
| 150 | + Authenticate |
| 151 | + </LinkButton> |
| 152 | + </fieldset> |
| 153 | + |
| 154 | + {(status === 'invalid' || status === 'authenticating') && ( |
| 155 | + <div className={styles.warningBannerContainer}> |
| 156 | + <div id={warningBannerId} className={styles.warningBanner}> |
| 157 | + {status === 'invalid' && 'Invalid token'} |
| 158 | + {status === 'authenticating' && <>Authenticating…</>} |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + )} |
| 162 | + </form> |
| 163 | + ); |
| 164 | +}; |
| 165 | + |
| 166 | +function mapAuthStatusToText(status: CoderAuthStatus): string { |
| 167 | + switch (status) { |
| 168 | + case 'tokenMissing': { |
| 169 | + return 'missing'; |
| 170 | + } |
| 171 | + |
| 172 | + case 'initializing': |
| 173 | + case 'authenticating': { |
| 174 | + return status; |
| 175 | + } |
| 176 | + |
| 177 | + default: { |
| 178 | + return 'invalid'; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments