mirror of
https://github.com/zhigang1992/connect.git
synced 2026-01-12 22:46:28 +08:00
feat: expose profile url in auth payload
This commit is contained in:
5
.changeset/shaggy-kids-unite.md
Normal file
5
.changeset/shaggy-kids-unite.md
Normal 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'.
|
||||
@@ -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,
|
||||
});
|
||||
})
|
||||
|
||||
@@ -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;
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user