Skip to content

Commit

Permalink
feat: adds client.authentication.getRefreshToken() method
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarenaldi committed Jun 7, 2024
1 parent e9a2882 commit 4621a66
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-turkeys-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lens-protocol/client": patch
---

**feat:** adds `client.authentication.getRefreshToken()` method.
25 changes: 25 additions & 0 deletions packages/client/src/authentication/Authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@ export class Authentication implements IAuthentication {
return failure(new CredentialsExpiredError());
}

async getRefreshToken(): PromiseResult<string, CredentialsExpiredError | NotAuthenticatedError> {
const credentials = await this.credentials.get();

if (!credentials) {
return failure(new NotAuthenticatedError());
}

if (!credentials.shouldRefresh()) {
return success(credentials.refreshToken);
}

if (credentials.canRefresh()) {
const newCredentials = await this.api.refresh(credentials.refreshToken);
await this.credentials.set(newCredentials);

if (!newCredentials.refreshToken) {
return failure(new CredentialsExpiredError());
}

return success(newCredentials.refreshToken);
}

return failure(new CredentialsExpiredError());
}

async getIdentityToken(): PromiseResult<string, CredentialsExpiredError | NotAuthenticatedError> {
const credentials = await this.credentials.get();

Expand Down
11 changes: 9 additions & 2 deletions packages/client/src/authentication/IAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,21 @@ export interface IAuthentication {
isAuthenticated(): Promise<boolean>;

/**
* Get the access token. If it expired, try to refresh it.
* Get the access token.
*
* @returns A Result with the access token or possible error scenarios
*/
getAccessToken(): PromiseResult<string, CredentialsExpiredError | NotAuthenticatedError>;

/**
* Get the identity token. If it expired, try to refresh it.
* Get the refresh token.
*
* @returns A Result with the refresh token or possible error scenarios
*/
getRefreshToken(): PromiseResult<string, CredentialsExpiredError | NotAuthenticatedError>;

/**
* Get the identity token.
*
* @returns A Result with the identity token or possible error scenarios
*/
Expand Down

0 comments on commit 4621a66

Please sign in to comment.