From 0eb3421297dd4620e5ca6da75fb807cd9e0ad7db Mon Sep 17 00:00:00 2001 From: Kris Urbas <605420+krzysu@users.noreply.github.com> Date: Tue, 23 Jan 2024 15:37:10 +0100 Subject: [PATCH 1/2] client: allow to authenticate from existing refresh token --- .changeset/angry-seals-rush.md | 5 +++ .changeset/honest-buses-flow.md | 5 +++ .../scripts/authentication/authenticate.ts | 5 --- .../authentication/authenticateWith.ts | 21 +++++++++++ .../scripts/authentication/overwriteOrigin.ts | 36 +++++++++++++++++++ packages/client/package.json | 2 +- .../src/authentication/Authentication.ts | 5 +++ .../src/authentication/IAuthentication.ts | 5 +++ 8 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 .changeset/angry-seals-rush.md create mode 100644 .changeset/honest-buses-flow.md create mode 100644 examples/node/scripts/authentication/authenticateWith.ts create mode 100644 examples/node/scripts/authentication/overwriteOrigin.ts diff --git a/.changeset/angry-seals-rush.md b/.changeset/angry-seals-rush.md new file mode 100644 index 0000000000..f3324e5d28 --- /dev/null +++ b/.changeset/angry-seals-rush.md @@ -0,0 +1,5 @@ +--- +"@lens-protocol/client": patch +--- + +**chore**: Relax node version requirements to >18 <21 diff --git a/.changeset/honest-buses-flow.md b/.changeset/honest-buses-flow.md new file mode 100644 index 0000000000..7c7b1a5265 --- /dev/null +++ b/.changeset/honest-buses-flow.md @@ -0,0 +1,5 @@ +--- +"@lens-protocol/client": minor +--- + +**feat**: Added `authentication.fromRefreshToken` method to allow to authenticate LensClient from an existing refresh token diff --git a/examples/node/scripts/authentication/authenticate.ts b/examples/node/scripts/authentication/authenticate.ts index 63d03b4e3b..1d4f9bee29 100644 --- a/examples/node/scripts/authentication/authenticate.ts +++ b/examples/node/scripts/authentication/authenticate.ts @@ -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(); diff --git a/examples/node/scripts/authentication/authenticateWith.ts b/examples/node/scripts/authentication/authenticateWith.ts new file mode 100644 index 0000000000..a65c8def0f --- /dev/null +++ b/examples/node/scripts/authentication/authenticateWith.ts @@ -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.authenticateWith({ refreshToken: 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(); diff --git a/examples/node/scripts/authentication/overwriteOrigin.ts b/examples/node/scripts/authentication/overwriteOrigin.ts new file mode 100644 index 0000000000..1498383a46 --- /dev/null +++ b/examples/node/scripts/authentication/overwriteOrigin.ts @@ -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(); diff --git a/packages/client/package.json b/packages/client/package.json index 49265eefdf..580257c1bd 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -97,7 +97,7 @@ "typescript": "5.2.2" }, "engines": { - "node": "^18.15.0" + "node": ">=18 <21" }, "prettier": "@lens-protocol/prettier-config", "babel": { diff --git a/packages/client/src/authentication/Authentication.ts b/packages/client/src/authentication/Authentication.ts index 76662f9a73..8351d2e6f3 100644 --- a/packages/client/src/authentication/Authentication.ts +++ b/packages/client/src/authentication/Authentication.ts @@ -35,6 +35,11 @@ export class Authentication implements IAuthentication { this.credentials = new CredentialsStorage(context.storage, context.environment.name); } + async authenticateWith({ refreshToken }: { refreshToken: string }): Promise { + const credentials = new Credentials(undefined, refreshToken); + await this.credentials.set(credentials); + } + async generateChallenge(request: ChallengeRequest): Promise { return this.api.challenge(request); } diff --git a/packages/client/src/authentication/IAuthentication.ts b/packages/client/src/authentication/IAuthentication.ts index e65e39aae7..d3bc6b376b 100644 --- a/packages/client/src/authentication/IAuthentication.ts +++ b/packages/client/src/authentication/IAuthentication.ts @@ -20,6 +20,11 @@ import type { * @group LensClient Modules */ export interface IAuthentication { + /** + * Authenticate with an existing, valid refresh token. + */ + authenticateWith({ refreshToken }: { refreshToken: string }): Promise; + /** * Generate a challenge string for the wallet to sign. * From fe707b72a774c5bb04db15b01fb6d4f9037747f4 Mon Sep 17 00:00:00 2001 From: Kris Urbas <605420+krzysu@users.noreply.github.com> Date: Wed, 24 Jan 2024 13:51:11 +0100 Subject: [PATCH 2/2] fix a few typedoc issues --- .changeset/honest-buses-flow.md | 2 +- packages/client/src/LensClient.ts | 2 +- packages/client/src/graphql/index.ts | 13 +++++++++++++ packages/client/src/submodules/index.ts | 1 + .../src/submodules/publication/Publication.ts | 4 +++- .../react-web/src/inbox/useEnhanceConversation.ts | 2 +- .../react-web/src/inbox/useEnhanceConversations.ts | 2 +- .../react-web/src/inbox/useStartLensConversation.ts | 2 +- packages/react-web/src/inbox/useXmtpClient.ts | 4 ++-- packages/react/src/index.ts | 5 ++++- 10 files changed, 28 insertions(+), 9 deletions(-) diff --git a/.changeset/honest-buses-flow.md b/.changeset/honest-buses-flow.md index 7c7b1a5265..53433929bc 100644 --- a/.changeset/honest-buses-flow.md +++ b/.changeset/honest-buses-flow.md @@ -2,4 +2,4 @@ "@lens-protocol/client": minor --- -**feat**: Added `authentication.fromRefreshToken` method to allow to authenticate LensClient from an existing refresh token +**feat**: Added `authentication.authenticateWith` method to allow to authenticate LensClient with an existing refresh token diff --git a/packages/client/src/LensClient.ts b/packages/client/src/LensClient.ts index 579a05b9ec..d10409a753 100644 --- a/packages/client/src/LensClient.ts +++ b/packages/client/src/LensClient.ts @@ -6,6 +6,7 @@ import { Environment } from './environments'; import { Explore, Feed, + Handle, Invites, Modules, Momoka, @@ -19,7 +20,6 @@ import { Transaction, Wallet, } from './submodules'; -import { Handle } from './submodules/handle'; /** * LensClient configuration diff --git a/packages/client/src/graphql/index.ts b/packages/client/src/graphql/index.ts index 454a2ee4d9..4328990164 100644 --- a/packages/client/src/graphql/index.ts +++ b/packages/client/src/graphql/index.ts @@ -124,10 +124,12 @@ export type { LastLoggedInProfileRequest, LinkHandleToProfileRequest, UnlinkHandleFromProfileRequest, + HandleToAddressRequest, HidePublicationRequest, InviteRequest, LegacyCollectRequest, LensTransactionStatusRequest, + ModuleMetadataRequest, MomokaCommentRequest, MomokaMirrorRequest, MomokaPostRequest, @@ -217,14 +219,22 @@ export type { FeeFollowModuleRedeemInput, FollowModuleInput, FollowModuleRedeemInput, + FraudReasonInput, + IllegalReasonInput, MultirecipientFeeCollectModuleInput, NetworkAddressInput, NftInput, OpenActionModuleInput, + ProfileFraudReasonInput, + ProfileReportingReasonInput, + ProfileSpamReasonInput, PublicationStatsInput, RecipientDataInput, ReferenceModuleInput, + ReportingReasonInput, + SensitiveReasonInput, SimpleCollectOpenActionModuleInput, + SpamReasonInput, UnknownFollowModuleInput, UnknownFollowModuleRedeemInput, UnknownOpenActionActRedeemInput, @@ -245,6 +255,7 @@ export type { ChangeProfileManager, Exact, Follow, + FollowStatusBulk, ImageTransform, InputMaybe, Maybe, @@ -282,11 +293,13 @@ export { MarketplaceMetadataAttributeDisplayType, ModuleType, MomokaValidatorError, + NftCollectionOwnersOrder, NftContractType, NotificationType, OpenActionCategoryType, OpenActionModuleType, PoapTokenLayerType, + PopularNftCollectionsOrder, ProfileActionHistoryType, ProfileInterestTypes, ProfileReportingFraudSubreason, diff --git a/packages/client/src/submodules/index.ts b/packages/client/src/submodules/index.ts index 504b0fd88f..f2bccd4c48 100644 --- a/packages/client/src/submodules/index.ts +++ b/packages/client/src/submodules/index.ts @@ -1,5 +1,6 @@ export * from './explore'; export * from './feed'; +export * from './handle'; export * from './invites'; export * from './modules'; export * from './momoka'; diff --git a/packages/client/src/submodules/publication/Publication.ts b/packages/client/src/submodules/publication/Publication.ts index 3fc9492723..0ce1843718 100644 --- a/packages/client/src/submodules/publication/Publication.ts +++ b/packages/client/src/submodules/publication/Publication.ts @@ -879,12 +879,14 @@ export class Publication { * Predict the next onchain Publication id for a Profile. * * @param request - Request object for the method - * @param request.from - ProfileId of the profile to predict the next onchain publication id for * @returns Publication Id */ async predictNextOnChainPublicationId({ from, }: { + /** + * Profile Id of the profile to predict the next onchain Publication Id for + */ from: Scalars['ProfileId']['input']; }): Promise { const result = await this.fetchAll({ diff --git a/packages/react-web/src/inbox/useEnhanceConversation.ts b/packages/react-web/src/inbox/useEnhanceConversation.ts index f711f6fd69..ee61be842d 100644 --- a/packages/react-web/src/inbox/useEnhanceConversation.ts +++ b/packages/react-web/src/inbox/useEnhanceConversation.ts @@ -29,7 +29,7 @@ export type UseEnhanceConversationResult = { /** * Enhance XMTP conversation with a profile of the conversation's peer * - * You MUST be authenticated via {@link useLogin} to use this hook. + * You MUST be authenticated via `useLogin` to use this hook. * * @example * ```tsx diff --git a/packages/react-web/src/inbox/useEnhanceConversations.ts b/packages/react-web/src/inbox/useEnhanceConversations.ts index 8ed51de444..af07d1ef84 100644 --- a/packages/react-web/src/inbox/useEnhanceConversations.ts +++ b/packages/react-web/src/inbox/useEnhanceConversations.ts @@ -19,7 +19,7 @@ export type UseEnhanceConversationsResult = Prettify< * Enhance XMTP conversations with profiles of the conversations' peers, * if conversation is between two Lens profiles. * - * You MUST be authenticated via {@link useLogin} to use this hook. + * You MUST be authenticated via `useLogin` to use this hook. * * @example * ```tsx diff --git a/packages/react-web/src/inbox/useStartLensConversation.ts b/packages/react-web/src/inbox/useStartLensConversation.ts index 620a87ec54..cb52b3bb33 100644 --- a/packages/react-web/src/inbox/useStartLensConversation.ts +++ b/packages/react-web/src/inbox/useStartLensConversation.ts @@ -22,7 +22,7 @@ export type UseStartLensConversationResult = ReturnType