Skip to content

Commit

Permalink
dev(auth): add distinct method to parse auth props
Browse files Browse the repository at this point in the history
  • Loading branch information
theborakompanioni committed Oct 4, 2023
1 parent 470cb80 commit bd4670c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 36 deletions.
8 changes: 1 addition & 7 deletions src/components/CreateWallet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,7 @@ export default function CreateWallet({ parentRoute, startWallet }) {
const body = await (res.ok ? res.json() : Api.Helper.throwError(res))

const { seedphrase, walletname: createdWalletFileName } = body
const auth = {
token: body.token,
token_type: body.token_type,
expires_in: body.expires_in,
scope: body.scope,
refresh_token: body.refresh_token,
}
const auth = Api.Helper.parseAuthProps(body)
setCreatedWallet({ walletFileName: createdWalletFileName, seedphrase, password, auth })
} catch (e) {
const message = t('create_wallet.error_creating_failed', {
Expand Down
16 changes: 2 additions & 14 deletions src/components/ImportWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,7 @@ export default function ImportWallet({ parentRoute, startWallet }: ImportWalletP
const recoverBody = await (recoverResponse.ok ? recoverResponse.json() : Api.Helper.throwError(recoverResponse))

const { walletname: importedWalletFileName } = recoverBody
let auth: Api.ApiAuthContext = {
token: recoverBody.token,
token_type: recoverBody.token_type,
expires_in: recoverBody.token_type,
scope: recoverBody.token_type,
refresh_token: recoverBody.token_type,
}
let auth: Api.ApiAuthContext = Api.Helper.parseAuthProps(recoverBody)
setRecoveredWallet({ walletFileName: importedWalletFileName, auth })

// Step #2: update the gaplimit config value if necessary
Expand Down Expand Up @@ -482,13 +476,7 @@ export default function ImportWallet({ parentRoute, startWallet }: ImportWalletP

const unlockResponse = await Api.postWalletUnlock({ walletName: importedWalletFileName }, { password })
const unlockBody = await (unlockResponse.ok ? unlockResponse.json() : Api.Helper.throwError(unlockResponse))
auth = {
token: unlockBody.token,
token_type: unlockBody.token_type,
expires_in: unlockBody.expires_in,
scope: unlockBody.scope,
refresh_token: unlockBody.refresh_token,
}
auth = Api.Helper.parseAuthProps(unlockBody)

// Step #4: reset `gaplimit´ to previous value if necessary
if (gaplimitUpdateNecessary) {
Expand Down
9 changes: 2 additions & 7 deletions src/components/Wallets.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,8 @@ export default function Wallets({ currentWallet, startWallet, stopWallet }) {

setUnlockWalletName(undefined)

const auth = {
token: body.token,
token_type: body.token_type,
expires_in: body.expires_in,
scope: body.scope,
refresh_token: body.refresh_token,
}
const auth = Api.Helper.parseAuthProps(body)

startWallet(body.walletname, auth)
navigate(routes.wallet)
} catch (e) {
Expand Down
9 changes: 2 additions & 7 deletions src/context/WalletContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,8 @@ const WalletProvider = ({ children }: PropsWithChildren<any>) => {
)
.then((res) => (res.ok ? res.json() : Api.Helper.throwError(res)))
.then((body) => {
const auth = {
token: body.token,
token_type: body.token_type,
expires_in: body.expires_in,
scope: body.scope,
refresh_token: body.refresh_token,
}
const auth = Api.Helper.parseAuthProps(body)

setSession({ name: currentWallet.name, auth })
currentWallet.updateToken(auth.token)
console.debug('Successfully renewed auth token.')
Expand Down
16 changes: 15 additions & 1 deletion src/libs/JmWalletApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ interface ApiError {
type WalletType = 'sw-fb'

interface TokenRequest {
grant_type: string
grant_type: 'refresh_token' | string
refresh_token: string
}

Expand Down Expand Up @@ -224,11 +224,25 @@ const Helper = (() => {
return { 'x-jm-authorization': `Bearer ${token}` }
}

// Simple helper method to parse auth properties.
// TODO: This can be removed when the API methods
// return typed responses (see #670)
const parseAuthProps = (body: any): ApiAuthContext => {
return {
token: body.token,
token_type: body.token_type,
expires_in: body.expires_in,
scope: body.scope,
refresh_token: body.refresh_token,
}
}

return {
throwError,
throwResolved,
extractErrorMessage,
buildAuthHeader,
parseAuthProps,
}
})()

Expand Down

0 comments on commit bd4670c

Please sign in to comment.