Skip to content

Commit

Permalink
client: add fetch current session
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysu committed Oct 26, 2023
1 parent 8120f67 commit 27bf96e
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-parrots-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lens-protocol/client": patch
---

Added `authentication.fetch` to get current session details
16 changes: 16 additions & 0 deletions examples/node/scripts/authentication/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getAuthenticatedClient } from '../shared/getAuthenticatedClient';
import { setupWallet } from '../shared/setupWallet';

async function main() {
const wallet = setupWallet();
const client = await getAuthenticatedClient(wallet);
const profileId = await client.authentication.getProfileId();

const result = await client.authentication.fetch();

const value = result.unwrap();

console.log(`Current sessions for ${profileId}: `, value);
}

main();
6 changes: 6 additions & 0 deletions packages/client/src/authentication/Authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export class Authentication implements IAuthentication {
await this.credentials.reset();
}

async fetch() {
return requireAuthHeaders(this, async (headers) => {
return this.api.currentSession(headers);
});
}

async fetchAll(request: ApprovedAuthenticationRequest = {}) {
return requireAuthHeaders(this, async (headers) => {
return this.api.approvedAuthentications(request, headers);
Expand Down
18 changes: 17 additions & 1 deletion packages/client/src/authentication/IAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,23 @@ export interface IAuthentication {
logout(): Promise<void>;

/**
* Fetch active authentication sessions.
* Fetch current active authentication session.
*
* ⚠️ Requires authenticated LensClient.
*
* @returns {@link ApprovedAuthenticationFragment}
*
* ```ts
* const result = await client.authentication.fetch();
* ```
*/
fetch(): PromiseResult<
ApprovedAuthenticationFragment,
CredentialsExpiredError | NotAuthenticatedError
>;

/**
* Fetch all active authentication sessions.
*
* ⚠️ Requires authenticated LensClient.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export class AuthenticationApi {
return credentials;
}

async currentSession(headers?: Record<string, string>): Promise<ApprovedAuthenticationFragment> {
const result = await this.sdk.CurrentSession({}, headers);

return result.data.result;
}

async approvedAuthentications(
request: ApprovedAuthenticationRequest,
headers?: Record<string, string>,
Expand Down
96 changes: 85 additions & 11 deletions packages/client/src/authentication/graphql/auth.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ import { GraphQLClient } from 'graphql-request';
import { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types';
import { print } from 'graphql';
import { DocumentNode } from 'graphql';
export type AuthChallengeFragment = { id: string; text: string };
export type AuthChallengeFragment = { __typename: 'AuthChallengeResult'; id: string; text: string };

export type ApprovedAuthenticationFragment = {
__typename: 'ApprovedAuthentication';
authorizationId: string;
browser: string | null;
device: string | null;
os: string | null;
origin: string | null;
expiresAt: string;
createdAt: string;
updatedAt: string;
};

export type AuthChallengeQueryVariables = Types.Exact<{
request: Types.ChallengeRequest;
Expand All @@ -20,16 +32,9 @@ export type AuthVerifyQueryVariables = Types.Exact<{

export type AuthVerifyQuery = { result: boolean };

export type ApprovedAuthenticationFragment = {
authorizationId: string;
browser: string | null;
device: string | null;
os: string | null;
origin: string | null;
expiresAt: string;
createdAt: string;
updatedAt: string;
};
export type CurrentSessionQueryVariables = Types.Exact<{ [key: string]: never }>;

export type CurrentSessionQuery = { result: ApprovedAuthenticationFragment };

export type ApprovedAuthenticationsQueryVariables = Types.Exact<{
request: Types.ApprovedAuthenticationRequest;
Expand Down Expand Up @@ -67,6 +72,7 @@ export const AuthChallengeFragmentDoc = {
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: '__typename' } },
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
{ kind: 'Field', name: { kind: 'Name', value: 'text' } },
],
Expand All @@ -84,6 +90,7 @@ export const ApprovedAuthenticationFragmentDoc = {
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: '__typename' } },
{ kind: 'Field', name: { kind: 'Name', value: 'authorizationId' } },
{ kind: 'Field', name: { kind: 'Name', value: 'browser' } },
{ kind: 'Field', name: { kind: 'Name', value: 'device' } },
Expand Down Expand Up @@ -145,6 +152,7 @@ export const AuthChallengeDocument = {
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: '__typename' } },
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
{ kind: 'Field', name: { kind: 'Name', value: 'text' } },
],
Expand Down Expand Up @@ -189,6 +197,51 @@ export const AuthVerifyDocument = {
},
],
} as unknown as DocumentNode;
export const CurrentSessionDocument = {
kind: 'Document',
definitions: [
{
kind: 'OperationDefinition',
operation: 'query',
name: { kind: 'Name', value: 'CurrentSession' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{
kind: 'Field',
alias: { kind: 'Name', value: 'result' },
name: { kind: 'Name', value: 'currentSession' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'FragmentSpread', name: { kind: 'Name', value: 'ApprovedAuthentication' } },
],
},
},
],
},
},
{
kind: 'FragmentDefinition',
name: { kind: 'Name', value: 'ApprovedAuthentication' },
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'ApprovedAuthentication' } },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: '__typename' } },
{ kind: 'Field', name: { kind: 'Name', value: 'authorizationId' } },
{ kind: 'Field', name: { kind: 'Name', value: 'browser' } },
{ kind: 'Field', name: { kind: 'Name', value: 'device' } },
{ kind: 'Field', name: { kind: 'Name', value: 'os' } },
{ kind: 'Field', name: { kind: 'Name', value: 'origin' } },
{ kind: 'Field', name: { kind: 'Name', value: 'expiresAt' } },
{ kind: 'Field', name: { kind: 'Name', value: 'createdAt' } },
{ kind: 'Field', name: { kind: 'Name', value: 'updatedAt' } },
],
},
},
],
} as unknown as DocumentNode;
export const ApprovedAuthenticationsDocument = {
kind: 'Document',
definitions: [
Expand Down Expand Up @@ -265,6 +318,7 @@ export const ApprovedAuthenticationsDocument = {
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: '__typename' } },
{ kind: 'Field', name: { kind: 'Name', value: 'authorizationId' } },
{ kind: 'Field', name: { kind: 'Name', value: 'browser' } },
{ kind: 'Field', name: { kind: 'Name', value: 'device' } },
Expand Down Expand Up @@ -427,6 +481,7 @@ export type SdkFunctionWrapper = <T>(
const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action();
const AuthChallengeDocumentString = print(AuthChallengeDocument);
const AuthVerifyDocumentString = print(AuthVerifyDocument);
const CurrentSessionDocumentString = print(CurrentSessionDocument);
const ApprovedAuthenticationsDocumentString = print(ApprovedAuthenticationsDocument);
const AuthAuthenticateDocumentString = print(AuthAuthenticateDocument);
const AuthRefreshDocumentString = print(AuthRefreshDocument);
Expand Down Expand Up @@ -466,6 +521,25 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
'query',
);
},
CurrentSession(
variables?: CurrentSessionQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
): Promise<{
data: CurrentSessionQuery;
extensions?: any;
headers: Dom.Headers;
status: number;
}> {
return withWrapper(
(wrappedRequestHeaders) =>
client.rawRequest<CurrentSessionQuery>(CurrentSessionDocumentString, variables, {
...requestHeaders,
...wrappedRequestHeaders,
}),
'CurrentSession',
'query',
);
},
ApprovedAuthentications(
variables: ApprovedAuthenticationsQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
Expand Down
26 changes: 17 additions & 9 deletions packages/client/src/authentication/graphql/auth.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
fragment AuthChallenge on AuthChallengeResult {
__typename
id
text
}

fragment ApprovedAuthentication on ApprovedAuthentication {
__typename
authorizationId
browser
device
os
origin
expiresAt
createdAt
updatedAt
}

query AuthChallenge($request: ChallengeRequest!) {
result: challenge(request: $request) {
...AuthChallenge
Expand All @@ -13,15 +26,10 @@ query AuthVerify($request: VerifyRequest!) {
result: verify(request: $request)
}

fragment ApprovedAuthentication on ApprovedAuthentication {
authorizationId
browser
device
os
origin
expiresAt
createdAt
updatedAt
query CurrentSession {
result: currentSession {
...ApprovedAuthentication
}
}

query ApprovedAuthentications($request: ApprovedAuthenticationRequest!) {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/graphql/fragments.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ export type CommentBaseFragment = {
};

export type CommentFragment = {
root: PostFragment;
root: PostFragment | {};
commentOn: CommentBaseFragment | PostFragment | QuoteBaseFragment;
firstComment: CommentBaseFragment | null;
stats: PublicationStatsFragment;
Expand Down
1 change: 0 additions & 1 deletion packages/client/src/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export type {
BroadcastRequest,
ChallengeRequest,
ChangeProfileManagersRequest,
ClaimProfileWithHandleRequest,
CreateProfileWithHandleRequest,
DefaultProfileRequest,
DismissRecommendedProfilesRequest,
Expand Down
7 changes: 3 additions & 4 deletions packages/client/src/graphql/types.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export enum DecryptFailReasonType {
PublicationIsNotGated = 'PUBLICATION_IS_NOT_GATED',
UnauthorizedAddress = 'UNAUTHORIZED_ADDRESS',
UnauthorizedBalance = 'UNAUTHORIZED_BALANCE',
Unsupported = 'UNSUPPORTED',
}

export type DefaultProfileRequest = {
Expand Down Expand Up @@ -486,8 +487,8 @@ export type InternalAllowedDomainsRequest = {

export type InternalClaimRequest = {
address: Scalars['EvmAddress']['input'];
freeTextHandle: Scalars['Boolean']['input'];
handle: Scalars['CreateHandle']['input'];
freeTextHandle?: InputMaybe<Scalars['Boolean']['input']>;
handle?: InputMaybe<Scalars['CreateHandle']['input']>;
overrideAlreadyClaimed: Scalars['Boolean']['input'];
overrideTradeMark: Scalars['Boolean']['input'];
secret: Scalars['String']['input'];
Expand Down Expand Up @@ -1171,8 +1172,6 @@ export type PublicationBookmarksRequest = {
};

export type PublicationBookmarksWhere = {
cursor?: InputMaybe<Scalars['Cursor']['input']>;
limit?: InputMaybe<LimitType>;
metadata?: InputMaybe<PublicationMetadataFilters>;
};

Expand Down
15 changes: 0 additions & 15 deletions packages/client/src/submodules/profile/Profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,21 +391,6 @@ export class Profile {
});
}

/**
* TODO: Claim a profile
*/
// async claim(
// request: ClaimProfileWithHandleRequest,
// ): PromiseResult<
// RelaySuccessFragment | ClaimProfileWithHandleErrorResultFragment,
// CredentialsExpiredError | NotAuthenticatedError
// > {
// return requireAuthHeaders(this.authentication, async (headers) => {
// const result = await this.sdk.ClaimProfileWithHandle({ request }, headers);
// return result.data.result;
// });
// }

/**
* Create a new profile.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { GraphQLClientRequestHeaders } from 'graphql-request/build/cjs/types';
import { print } from 'graphql';
import { DocumentNode } from 'graphql';
export type LensTransactionResultFragment = {
__typename: 'LensTransactionResult';
status: Types.LensTransactionStatusType;
txHash: string;
reason: Types.LensTransactionFailureType | null;
Expand All @@ -38,6 +39,7 @@ export type TxIdToTxHashQueryVariables = Types.Exact<{
export type TxIdToTxHashQuery = { result: string | null };

export type RelayQueueResultFragment = {
__typename: 'RelayQueueResult';
key: Types.RelayRoleKey;
queue: number;
relay: NetworkAddressFragment;
Expand Down Expand Up @@ -77,6 +79,7 @@ export const LensTransactionResultFragmentDoc = {
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: '__typename' } },
{ kind: 'Field', name: { kind: 'Name', value: 'status' } },
{ kind: 'Field', name: { kind: 'Name', value: 'txHash' } },
{ kind: 'Field', name: { kind: 'Name', value: 'reason' } },
Expand All @@ -96,6 +99,7 @@ export const RelayQueueResultFragmentDoc = {
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: '__typename' } },
{ kind: 'Field', name: { kind: 'Name', value: 'key' } },
{
kind: 'Field',
Expand Down Expand Up @@ -194,6 +198,7 @@ export const RelayQueuesDocument = {
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: '__typename' } },
{ kind: 'Field', name: { kind: 'Name', value: 'key' } },
{
kind: 'Field',
Expand Down Expand Up @@ -275,6 +280,7 @@ export const LensTransactionStatusDocument = {
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: '__typename' } },
{ kind: 'Field', name: { kind: 'Name', value: 'status' } },
{ kind: 'Field', name: { kind: 'Name', value: 'txHash' } },
{ kind: 'Field', name: { kind: 'Name', value: 'reason' } },
Expand Down
Loading

0 comments on commit 27bf96e

Please sign in to comment.