From c7bd34e77f874fcf38196cd48d5b514e4c49ec2f Mon Sep 17 00:00:00 2001 From: fbwoolf Date: Tue, 25 May 2021 15:15:38 -0500 Subject: [PATCH] feat: expose profile url in auth payload --- .changeset/shaggy-kids-unite.md | 5 +++++ packages/connect/src/auth.ts | 15 +++++++++------ packages/connect/src/types/auth.ts | 30 ++++++++++++++++++++++++------ 3 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 .changeset/shaggy-kids-unite.md diff --git a/.changeset/shaggy-kids-unite.md b/.changeset/shaggy-kids-unite.md new file mode 100644 index 0000000..d9cbb23 --- /dev/null +++ b/.changeset/shaggy-kids-unite.md @@ -0,0 +1,5 @@ +--- +'@stacks/connect': major +--- + +This exposes the profile_url and other properties in the auth response. It also removes the deprecated 'finished' prop from AuthOptions as a breaking change. We will now only support the use of 'onFinish'. diff --git a/packages/connect/src/auth.ts b/packages/connect/src/auth.ts index 648c878..ebd68f7 100644 --- a/packages/connect/src/auth.ts +++ b/packages/connect/src/auth.ts @@ -1,5 +1,6 @@ import { AppConfig, UserSession } from '@stacks/auth'; -import type { AuthOptions } from './types'; +import { decodeToken } from 'jsontokens'; +import type { AuthOptions, AuthResponsePayload } from './types'; import { getStacksProvider } from './utils'; @@ -37,7 +38,7 @@ export const getOrCreateUserSession = (userSession?: UserSession): UserSession = return userSession; }; -export const authenticate = (authOptions: AuthOptions) => { +export const authenticate = async (authOptions: AuthOptions) => { const provider = getStacksProvider(); if (!provider) { throw new Error('Unable to authenticate without Stacks Wallet extension'); @@ -46,7 +47,6 @@ export const authenticate = (authOptions: AuthOptions) => { const { redirectTo = '/', manifestPath, - finished, onFinish, onCancel, sendToSignIn = false, @@ -72,13 +72,16 @@ export const authenticate = (authOptions: AuthOptions) => { } ); - void provider + await provider .authenticationRequest(authRequest) .then(async authResponse => { await userSession.handlePendingSignIn(authResponse); - const success = onFinish || finished; - success?.({ + const token = decodeToken(authResponse); + const payload = token?.payload; + const authResponsePayload = (payload as unknown) as AuthResponsePayload; + onFinish?.({ authResponse, + authResponsePayload, userSession, }); }) diff --git a/packages/connect/src/types/auth.ts b/packages/connect/src/types/auth.ts index ba80041..0348246 100644 --- a/packages/connect/src/types/auth.ts +++ b/packages/connect/src/types/auth.ts @@ -1,7 +1,26 @@ import type { UserSession } from '@stacks/auth'; -export interface FinishedData { +export interface AuthResponsePayload { + private_key: string; + username: string | null; + hubUrl: string; + associationToken: string; + blockstackAPIUrl: string | null; + core_token: string | null; + email: string | null; + exp: number; + iat: number; + iss: string; + jti: string; + version: string; + profile: any; + profile_url: string; + public_keys: string[]; +} + +export interface FinishedAuthData { authResponse: string; + authResponsePayload: AuthResponsePayload; userSession: UserSession; } @@ -17,15 +36,14 @@ export interface AuthOptions { /** The URL you want the user to be redirected to after authentication. */ redirectTo?: string; manifestPath?: string; - /** @deprecated use `onFinish` */ - finished?: (payload: FinishedData) => void; /** * This callback is fired after authentication is finished. - * The callback is called with a single object argument, with two keys: - * `userSession`: a UserSession object with `userData` included + * The callback is called with a single object argument, with three keys: * `authResponse`: the raw `authResponse` string that is returned from authentication + * `authResponsePayload`: an AuthResponsePayload object + * `userSession`: a UserSession object with `userData` included * */ - onFinish?: (payload: FinishedData) => void; + onFinish?: (payload: FinishedAuthData) => void; /** This callback is fired if the user exits before finishing */ onCancel?: () => void; /**