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

feat: adds client.authentication.getRefreshToken() method #951

Merged
merged 1 commit into from
Jun 13, 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
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
Loading