-
Notifications
You must be signed in to change notification settings - Fork 343
feat(clerk-js,clerk-react,types,localization): <ApiKeys />
AIO MVP
#5858
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Draft
wobsoriano
wants to merge
35
commits into
main
Choose a base branch
from
rob/robo-20-manage-api-keys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
89ceb0e
chore: set initial files for new component
wobsoriano bdafb68
chore: add resources and test methods
wobsoriano cf45005
chore(clerk-js): Remove Clerk.commerce (#5846)
brkalow 99df5e1
chore: add simple table with calls to fapi
wobsoriano 547f9e3
chore: use built in components
wobsoriano 430a292
chore: add fake form
wobsoriano 6981d7e
chore: fix bad rebase
wobsoriano c473fcd
chore: fix bad rebase
wobsoriano 866b8e3
chore: add create api key func
wobsoriano 926c455
chore: add prop types and improve fetching
wobsoriano f50e396
chore: add React component
wobsoriano e152dc2
chore: accept props
wobsoriano 9257d60
chore: add copy button functionality
wobsoriano 55d6559
chore: fetch secret on clipboard copy
wobsoriano fe52297
chore: add api key revokation
wobsoriano 5d5b67e
chore: set minimum fields
wobsoriano aaef70c
chore: add pagination and improve form
wobsoriano 97eb691
chore: try refetch
wobsoriano 55a9c10
chore: fix revalidation and more styling
wobsoriano 80f7e91
chore: rename component to <ApiKeys />
wobsoriano c85d387
chore: add expiration field
wobsoriano 874f719
chore: add api keys component or user and org profile
wobsoriano 709a340
chore: add missing org profile sidebar nav
wobsoriano 5be01e5
chore: clean up props
wobsoriano 3c67fb9
chore: clean up props
wobsoriano 636da55
Merge branch 'main' into rob/robo-20-manage-api-keys
wobsoriano 692fb02
chore: add api key secret fetcher and clean up components
wobsoriano 74224cb
chore: adjust table heading widths
wobsoriano c4d8e6f
chore: improve secret fetching
wobsoriano 278435a
chore: improve secret fetching
wobsoriano bc95160
chore: add locales
wobsoriano 201f811
Merge branch 'main' into rob/robo-20-manage-api-keys
wobsoriano be26494
chore: action locales
wobsoriano 1f34bac
chore: add locales to api keys page in user and org profile
wobsoriano 338186a
Merge branch 'main' into rob/robo-20-manage-api-keys
wobsoriano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import type { | ||
ApiKeyJSON, | ||
ApiKeyResource, | ||
CreateApiKeyParams, | ||
GetApiKeysParams, | ||
RevokeApiKeyParams, | ||
} from '@clerk/types'; | ||
|
||
import { unixEpochToDate } from '../../utils/date'; | ||
import { BaseResource } from './internal'; | ||
|
||
export class ApiKey extends BaseResource implements ApiKeyResource { | ||
pathRoot = '/api_keys'; | ||
|
||
id!: string; | ||
type!: string; | ||
name!: string; | ||
subject!: string; | ||
scopes!: string[]; | ||
claims!: Record<string, any> | null; | ||
revoked!: boolean; | ||
revocationReason!: string | null; | ||
expired!: boolean; | ||
expiration!: Date | null; | ||
createdBy!: string | null; | ||
creationReason!: string | null; | ||
createdAt!: Date; | ||
updatedAt!: Date; | ||
|
||
constructor(data: ApiKeyJSON) { | ||
super(); | ||
this.fromJSON(data); | ||
} | ||
|
||
protected fromJSON(data: ApiKeyJSON | null): this { | ||
if (!data) { | ||
return this; | ||
} | ||
|
||
this.id = data.id; | ||
this.type = data.type; | ||
this.name = data.name; | ||
this.subject = data.subject; | ||
this.scopes = data.scopes; | ||
this.claims = data.claims; | ||
this.revoked = data.revoked; | ||
this.revocationReason = data.revocation_reason; | ||
this.expired = data.expired; | ||
this.expiration = data.expiration ? unixEpochToDate(data.expiration) : null; | ||
this.createdBy = data.created_by; | ||
this.creationReason = data.creation_reason; | ||
this.updatedAt = unixEpochToDate(data.updated_at); | ||
this.createdAt = unixEpochToDate(data.created_at); | ||
return this; | ||
} | ||
|
||
static async getAll(params?: GetApiKeysParams): Promise<ApiKeyResource[]> { | ||
return this.clerk | ||
.getFapiClient() | ||
.request<{ api_keys: ApiKeyJSON[] }>({ | ||
method: 'GET', | ||
path: '/api_keys', | ||
pathPrefix: '', | ||
search: { | ||
subject: params?.subject ?? this.clerk.organization?.id ?? this.clerk.user?.id ?? '', | ||
}, | ||
headers: { | ||
Authorization: `Bearer ${await this.clerk.session?.getToken()}`, | ||
}, | ||
credentials: 'same-origin', | ||
}) | ||
.then(res => { | ||
const apiKeysJSON = res.payload as unknown as { api_keys: ApiKeyJSON[] }; | ||
return apiKeysJSON.api_keys.map(json => new ApiKey(json)); | ||
}) | ||
.catch(() => []); | ||
} | ||
|
||
static async getSecret(id: string): Promise<string> { | ||
return this.clerk | ||
.getFapiClient() | ||
.request<{ secret: string }>({ | ||
method: 'GET', | ||
path: `/api_keys/${id}/secret`, | ||
credentials: 'same-origin', | ||
pathPrefix: '', | ||
headers: { | ||
Authorization: `Bearer ${await this.clerk.session?.getToken()}`, | ||
}, | ||
}) | ||
.then(res => { | ||
const { secret } = res.payload as unknown as { secret: string }; | ||
return secret; | ||
}) | ||
.catch(() => ''); | ||
} | ||
|
||
static async create(params: CreateApiKeyParams): Promise<ApiKeyResource> { | ||
const json = ( | ||
await BaseResource._fetch<ApiKeyJSON>({ | ||
path: '/api_keys', | ||
method: 'POST', | ||
pathPrefix: '', | ||
headers: { | ||
Authorization: `Bearer ${await this.clerk.session?.getToken()}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
credentials: 'same-origin', | ||
body: JSON.stringify({ | ||
type: params.type ?? 'api_key', | ||
name: params.name, | ||
subject: params.subject ?? this.clerk.organization?.id ?? this.clerk.user?.id ?? '', | ||
creation_reason: params.creationReason, | ||
seconds_until_expiration: params.secondsUntilExpiration, | ||
}), | ||
}) | ||
)?.response as ApiKeyJSON; | ||
|
||
return new ApiKey(json); | ||
} | ||
|
||
static async revoke(params: RevokeApiKeyParams): Promise<ApiKeyResource> { | ||
const json = ( | ||
await BaseResource._fetch<ApiKeyJSON>({ | ||
path: `/api_keys/${params.apiKeyID}/revoke`, | ||
method: 'POST', | ||
pathPrefix: '', | ||
headers: { | ||
Authorization: `Bearer ${await this.clerk.session?.getToken()}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
credentials: 'same-origin', | ||
body: JSON.stringify({ | ||
revocation_reason: params.revocationReason, | ||
}), | ||
}) | ||
)?.response as ApiKeyJSON; | ||
|
||
return new ApiKey(json); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { useOrganization, useUser } from '@clerk/shared/react'; | ||
|
||
import { useApiKeysContext } from '../../contexts'; | ||
import { Box, Button, Col, Flex, Flow, Icon, localizationKeys, useLocalizations } from '../../customizables'; | ||
import { Card, InputWithIcon, Pagination, withCardStateProvider } from '../../elements'; | ||
import { Action } from '../../elements/Action'; | ||
import { MagnifyingGlass } from '../../icons'; | ||
import { ApiKeysTable } from './ApiKeysTable'; | ||
import { CreateApiKeyForm } from './CreateApiKeyForm'; | ||
import { useApiKeys } from './useApiKeys'; | ||
|
||
export const ApiKeysInternal = ({ subject, perPage }: { subject: string; perPage?: number }) => { | ||
const { | ||
apiKeys, | ||
isLoading, | ||
revokeApiKey, | ||
search, | ||
setSearch, | ||
page, | ||
setPage, | ||
pageCount, | ||
itemCount, | ||
startingRow, | ||
endingRow, | ||
handleCreate, | ||
} = useApiKeys({ subject, perPage }); | ||
const { t } = useLocalizations(); | ||
|
||
return ( | ||
<Col gap={4}> | ||
<Action.Root> | ||
<Flex | ||
justify='between' | ||
align='center' | ||
> | ||
<Box> | ||
<InputWithIcon | ||
placeholder={t(localizationKeys('apiKey.action__search'))} | ||
leftIcon={<Icon icon={MagnifyingGlass} />} | ||
value={search} | ||
onChange={e => { | ||
setSearch(e.target.value); | ||
setPage(1); | ||
}} | ||
/> | ||
</Box> | ||
<Action.Trigger value='add'> | ||
<Button | ||
variant='solid' | ||
localizationKey={localizationKeys('apiKey.action__add')} | ||
/> | ||
</Action.Trigger> | ||
</Flex> | ||
<Action.Open value='add'> | ||
<Flex sx={t => ({ paddingTop: t.space.$6, paddingBottom: t.space.$6 })}> | ||
<Action.Card sx={{ width: '100%' }}> | ||
<CreateApiKeyForm onCreate={params => void handleCreate(params)} /> | ||
</Action.Card> | ||
</Flex> | ||
</Action.Open> | ||
</Action.Root> | ||
<ApiKeysTable | ||
rows={apiKeys} | ||
isLoading={isLoading} | ||
onRevoke={revokeApiKey} | ||
/> | ||
{itemCount > 5 && ( | ||
<Pagination | ||
count={pageCount} | ||
page={page} | ||
onChange={setPage} | ||
siblingCount={1} | ||
rowInfo={{ allRowsCount: itemCount, startingRow, endingRow }} | ||
/> | ||
)} | ||
</Col> | ||
); | ||
}; | ||
|
||
export const ApiKeys = withCardStateProvider(() => { | ||
const ctx = useApiKeysContext(); | ||
const { user } = useUser(); | ||
const { organization } = useOrganization(); | ||
|
||
return ( | ||
<Flow.Root flow='apiKey'> | ||
<Card.Root sx={{ width: '100%' }}> | ||
<Card.Content sx={{ textAlign: 'left' }}> | ||
<ApiKeysInternal | ||
subject={ctx.subject ?? organization?.id ?? user?.id ?? ''} | ||
perPage={ctx.perPage} | ||
/> | ||
</Card.Content> | ||
</Card.Root> | ||
</Flow.Root> | ||
); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Temporary fix for cors issue