@withkeystone/cli
Version:
Keystone CLI - Test automation for modern web apps
39 lines • 1.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAuthenticatedClient = getAuthenticatedClient;
exports.isAuthenticated = isAuthenticated;
exports.getAccessToken = getAccessToken;
const AuthenticatedClient_js_1 = require("./AuthenticatedClient.js");
const TokenStorage_js_1 = require("./TokenStorage.js");
/**
* Get an authenticated client using stored JWT tokens.
* Throws an error if the user is not authenticated.
*/
async function getAuthenticatedClient(config) {
const apiUrl = config?.apiUrl || process.env.KEYSTONE_API_URL || 'https://api.withkeystone.com';
const tokenStorage = new TokenStorage_js_1.TokenStorage(apiUrl);
const tokens = await tokenStorage.getTokens();
if (!tokens) {
throw new Error('Not authenticated. Please run "keystone init" to login.');
}
return new AuthenticatedClient_js_1.AuthenticatedClient(apiUrl);
}
/**
* Check if the user is authenticated
*/
async function isAuthenticated() {
const apiUrl = process.env.KEYSTONE_API_URL || 'https://api.withkeystone.com';
const tokenStorage = new TokenStorage_js_1.TokenStorage(apiUrl);
const tokens = await tokenStorage.getTokens();
return tokens !== null;
}
/**
* Get the current access token if authenticated
*/
async function getAccessToken() {
const apiUrl = process.env.KEYSTONE_API_URL || 'https://api.withkeystone.com';
const tokenStorage = new TokenStorage_js_1.TokenStorage(apiUrl);
const tokens = await tokenStorage.getTokens();
return tokens?.access_token || null;
}
//# sourceMappingURL=getAuthenticatedClient.js.map