Skip to content

Commit

Permalink
client: allow to authenticate from existing refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysu committed Jan 24, 2024
1 parent a1c5844 commit cf756a1
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-seals-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lens-protocol/client": patch
---

**chore**: Relax node version requirements to >18 <21
5 changes: 5 additions & 0 deletions .changeset/honest-buses-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lens-protocol/client": minor
---

**feat**: Added `authentication.fromRefreshToken` method to allow to authenticate LensClient from an existing refresh token
5 changes: 0 additions & 5 deletions examples/node/scripts/authentication/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import { setupWallet } from '../shared/setupWallet';
async function main() {
const client = new LensClient({
environment: development,
headers: {
origin: 'https://lens-scripts.example',
'user-agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
},
});

const wallet = setupWallet();
Expand Down
21 changes: 21 additions & 0 deletions examples/node/scripts/authentication/fromRefreshToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { LensClient, development } from '@lens-protocol/client';

async function main() {
const client = new LensClient({
environment: development,
});

const token = 'YOUR_VALID_REFRESH_TOKEN_HERE';
await client.authentication.fromRefreshToken(token);

console.log(`Is LensClient authenticated? `, await client.authentication.isAuthenticated());

const accessTokenResult = await client.authentication.getAccessToken();
const accessToken = accessTokenResult.unwrap();
const profileId = await client.authentication.getProfileId();

console.log(`Authenticated profileId: `, profileId);
console.log(`Access token: `, accessToken);
}

main();
36 changes: 36 additions & 0 deletions examples/node/scripts/authentication/overwriteOrigin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { LensClient, development } from '@lens-protocol/client';

import { setupWallet } from '../shared/setupWallet';

async function main() {
const client = new LensClient({
environment: development,
headers: {
origin: 'https://lens-scripts.example',
},
});

const wallet = setupWallet();
const address = await wallet.getAddress();

const managedProfiles = await client.wallet.profilesManaged({ for: wallet.address });

if (managedProfiles.items.length === 0) {
throw new Error(`You don't manage any profiles, create one first`);
}

const { id, text } = await client.authentication.generateChallenge({
signedBy: address,
for: managedProfiles.items[0].id,
});

console.log(`Challenge: `, text); // Notice the origin URL in the challenge message

const signature = await wallet.signMessage(text);

await client.authentication.authenticate({ id, signature });

console.log(`Is LensClient authenticated? `, await client.authentication.isAuthenticated());
}

main();
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
"typescript": "5.2.2"
},
"engines": {
"node": "^18.15.0"
"node": ">=18 <21"
},
"prettier": "@lens-protocol/prettier-config",
"babel": {
Expand Down
5 changes: 5 additions & 0 deletions packages/client/src/authentication/Authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export class Authentication implements IAuthentication {
this.credentials = new CredentialsStorage(context.storage, context.environment.name);
}

async fromRefreshToken(refreshToken: string): Promise<void> {
const credentials = new Credentials(undefined, refreshToken);
await this.credentials.set(credentials);
}

async generateChallenge(request: ChallengeRequest): Promise<AuthChallengeFragment> {
return this.api.challenge(request);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/client/src/authentication/IAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import type {
* @group LensClient Modules
*/
export interface IAuthentication {
/**
* Authenticate from an existing, valid refresh token.
*
* @param refreshToken - The refresh token
*/
fromRefreshToken(refreshToken: string): Promise<void>;

/**
* Generate a challenge string for the wallet to sign.
*
Expand Down

0 comments on commit cf756a1

Please sign in to comment.