feat: expose profile url in auth payload

This commit is contained in:
fbwoolf
2021-05-25 15:15:38 -05:00
committed by Fara Woolf
parent e2b748a4d3
commit c7bd34e77f
3 changed files with 38 additions and 12 deletions

View File

@@ -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'.

View File

@@ -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,
});
})

View File

@@ -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;
/**