diff --git a/src/cli/validateToken.ts b/src/cli/validateToken.ts index 122ffca..08178a0 100644 --- a/src/cli/validateToken.ts +++ b/src/cli/validateToken.ts @@ -1,6 +1,7 @@ import { API as APIInterface } from '@metacall/protocol/protocol'; import { unlink } from 'fs/promises'; -import { configFilePath, save } from '../config'; +import { auth } from '../auth'; +import { configFilePath, defaultPath, load, save } from '../config'; import { exists } from '../utils'; import args from './args'; import { error, info } from './messages'; @@ -32,11 +33,18 @@ const validateToken = async (api: APIInterface): Promise => { (await exists(configFile)) && (await unlink(configFile)); - info('Try to login again!'); + info('Token expired, initiating token-based login...'); - return error( - `Token validation failed, potential causes include:\n\t1) The JWT may be mistranslated (Invalid Signature).\n\t2) JWT might have expired.` - ); + try { + const config = await load(defaultPath); + + const newToken = await auth(config); + // Save the new token + await save({ token: newToken }); + return; + } catch (loginErr) { + return error('Token validation failed. Please try again.'); + } } };