Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: improve PAT login experience #999

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/fields/FieldInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const FieldInput: FC<IProps> = ({

<input
type={type}
className="appearance-none block w-full dark:text-gray-800 bg-gray-100 border border-red rounded py-1.5 px-4 mb-2 focus:bg-gray-200 focus:outline-none"
className="appearance-none block w-full dark:text-gray-800 bg-gray-100 border border-red rounded py-1.5 px-4 mb-2 focus:bg-gray-200 focus:outline-none text-sm"
id={input.name}
placeholder={placeholder}
required={required}
Expand Down
4 changes: 2 additions & 2 deletions src/routes/LoginWithToken.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('routes/LoginWithToken.tsx', () => {
expect(validate(values).token).toBe('Invalid token.');
});

it("should click on the 'personal access tokens' link and open the browser", async () => {
it("should click on the 'Generate a PAT' link and open the browser", async () => {
render(
<AppContext.Provider value={{ validateToken: mockValidateToken }}>
<MemoryRouter>
Expand All @@ -81,7 +81,7 @@ describe('routes/LoginWithToken.tsx', () => {
</AppContext.Provider>,
);

fireEvent.click(screen.getByText('personal access tokens'));
fireEvent.click(screen.getByText('Generate a PAT'));

expect(openExternalMock).toHaveBeenCalledTimes(1);
});
Expand Down
61 changes: 39 additions & 22 deletions src/routes/LoginWithToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type { AuthTokenOptions } from '../types';
import { openExternalLink } from '../utils/comms';
import { Constants } from '../utils/constants';

import { format } from 'date-fns';

interface IValues {
token?: string;
hostname?: string;
Expand Down Expand Up @@ -53,40 +55,46 @@ export const LoginWithToken: FC = () => {
const renderForm = (formProps: FormRenderProps) => {
const { handleSubmit, submitting, pristine, values } = formProps;

const buttonClasses =
'rounded bg-gray-300 font-semibold rounded text-sm text-center hover:bg-gray-500 hover:text-white dark:text-black focus:outline-none cursor-pointer';

return (
<form onSubmit={handleSubmit}>
<FieldInput
name="token"
label="Token"
placeholder="The 40 characters token generated on GitHub"
helpText={
<>
To generate a token, go to GitHub,{' '}
<button
type="button"
className="underline hover:text-gray-500 dark:hover:text-gray-300 cursor-pointer"
onClick={() =>
openLink(
'https://github.com/settings/tokens/new?scopes=notifications,read:user,repo&description=gitify_token',
)
}
>
personal access tokens
</button>{' '}
and create one with the {Constants.AUTH_SCOPE.length} scopes{' '}
<span className="underline font-extrabold text-yellow-500">
{Constants.AUTH_SCOPE.join(', ')}{' '}
</span>
.
</>
<div>
<div>
Create a Personal Access Token on GitHub and paste above.
</div>
<div>
The required scopes will be selected for you.{' '}
<button
type="button"
className={`px-2 py-1 my-2 text-xs ${buttonClasses}`}
onClick={() => openLink(getNewTokenURL())}
>
Generate a PAT
</button>
</div>
</div>
}
/>

<FieldInput
name="hostname"
label="Hostname"
placeholder="github.company.com"
helpText="Defaults to github.com. Change only if you are using GitHub for Enterprise."
helpText={
<div>
<div>Defaults to github.com.</div>
<div className="italic">
Change only if you are using GitHub Enterprise Server.
</div>
</div>
}
/>

{!isValidToken && (
Expand All @@ -96,7 +104,7 @@ export const LoginWithToken: FC = () => {
)}

<button
className="float-right px-4 py-2 my-4 bg-gray-300 font-semibold rounded text-sm text-center hover:bg-gray-500 hover:text-white dark:text-black focus:outline-none"
className={`float-right px-4 py-2 my-4 ${buttonClasses}`}
title="Submit Button"
disabled={submitting || pristine}
type="submit"
Expand Down Expand Up @@ -134,7 +142,7 @@ export const LoginWithToken: FC = () => {
</button>

<h3 className="text-lg font-semibold">
Login with personal access token
Login with Personal Access Token
</h3>
</div>

Expand All @@ -153,3 +161,12 @@ export const LoginWithToken: FC = () => {
</div>
);
};

function getNewTokenURL(): string {
const date = format(new Date(), 'PP p');
const newTokenURL = new URL('https://github.com/settings/tokens/new');
newTokenURL.searchParams.append('description', `Gitify (Created on ${date})`);
newTokenURL.searchParams.append('scopes', Constants.AUTH_SCOPE.join(','));

return newTokenURL.toString();
}
6 changes: 3 additions & 3 deletions src/routes/__snapshots__/LoginEnterprise.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 30 additions & 26 deletions src/routes/__snapshots__/LoginWithToken.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.