mirror of
https://github.com/DeBrosOfficial/network-ts-sdk.git
synced 2025-12-12 18:28:50 +00:00
Refactor AuthClient to streamline imports and add wallet authentication methods including challenge, verify, and getApiKey; improve error logging during logout process.
This commit is contained in:
parent
c6dfb0bfed
commit
dcf8efe428
@ -1,10 +1,5 @@
|
|||||||
import { HttpClient } from "../core/http";
|
import { HttpClient } from "../core/http";
|
||||||
import {
|
import { AuthConfig, WhoAmI, StorageAdapter, MemoryStorage } from "./types";
|
||||||
AuthConfig,
|
|
||||||
WhoAmI,
|
|
||||||
StorageAdapter,
|
|
||||||
MemoryStorage,
|
|
||||||
} from "./types";
|
|
||||||
|
|
||||||
export class AuthClient {
|
export class AuthClient {
|
||||||
private httpClient: HttpClient;
|
private httpClient: HttpClient;
|
||||||
@ -75,10 +70,13 @@ export class AuthClient {
|
|||||||
await this.httpClient.post("/v1/auth/logout", { all: true });
|
await this.httpClient.post("/v1/auth/logout", { all: true });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Log warning but don't fail - local cleanup is more important
|
// Log warning but don't fail - local cleanup is more important
|
||||||
console.warn('Server-side logout failed, continuing with local cleanup:', error);
|
console.warn(
|
||||||
|
"Server-side logout failed, continuing with local cleanup:",
|
||||||
|
error
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always clear local state
|
// Always clear local state
|
||||||
this.currentApiKey = undefined;
|
this.currentApiKey = undefined;
|
||||||
this.currentJwt = undefined;
|
this.currentJwt = undefined;
|
||||||
@ -94,4 +92,78 @@ export class AuthClient {
|
|||||||
this.httpClient.setJwt(undefined);
|
this.httpClient.setJwt(undefined);
|
||||||
await this.storage.clear();
|
await this.storage.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Request a challenge nonce for wallet authentication
|
||||||
|
*/
|
||||||
|
async challenge(params: {
|
||||||
|
wallet: string;
|
||||||
|
purpose?: string;
|
||||||
|
namespace?: string;
|
||||||
|
}): Promise<{
|
||||||
|
nonce: string;
|
||||||
|
wallet: string;
|
||||||
|
namespace: string;
|
||||||
|
expires_at: string;
|
||||||
|
}> {
|
||||||
|
const response = await this.httpClient.post("/v1/auth/challenge", {
|
||||||
|
wallet: params.wallet,
|
||||||
|
purpose: params.purpose || "authentication",
|
||||||
|
namespace: params.namespace || "default",
|
||||||
|
});
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify wallet signature and get JWT token
|
||||||
|
*/
|
||||||
|
async verify(params: {
|
||||||
|
wallet: string;
|
||||||
|
nonce: string;
|
||||||
|
signature: string;
|
||||||
|
namespace?: string;
|
||||||
|
}): Promise<{
|
||||||
|
access_token: string;
|
||||||
|
refresh_token: string;
|
||||||
|
subject: string;
|
||||||
|
namespace: string;
|
||||||
|
}> {
|
||||||
|
const response = await this.httpClient.post("/v1/auth/verify", {
|
||||||
|
wallet: params.wallet,
|
||||||
|
nonce: params.nonce,
|
||||||
|
signature: params.signature,
|
||||||
|
namespace: params.namespace || "default",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Automatically set the JWT
|
||||||
|
this.setJwt(response.access_token);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get API key for wallet (creates namespace ownership)
|
||||||
|
*/
|
||||||
|
async getApiKey(params: {
|
||||||
|
wallet: string;
|
||||||
|
nonce: string;
|
||||||
|
signature: string;
|
||||||
|
namespace?: string;
|
||||||
|
}): Promise<{
|
||||||
|
api_key: string;
|
||||||
|
namespace: string;
|
||||||
|
wallet: string;
|
||||||
|
}> {
|
||||||
|
const response = await this.httpClient.post("/v1/auth/api-key", {
|
||||||
|
wallet: params.wallet,
|
||||||
|
nonce: params.nonce,
|
||||||
|
signature: params.signature,
|
||||||
|
namespace: params.namespace || "default",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Automatically set the API key
|
||||||
|
this.setApiKey(response.api_key);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user