cli/src/commands/auth-status.tsblame
View source
59e66671import { log } from "@clack/prompts";
e93a9782import { loadConfig } from "../config.js";
e93a9783
e93a9784export async function authStatus() {
e93a9785 const config = await loadConfig();
e93a9786
e93a9787 if (!config.token) {
59e66678 log.warn("Not logged in.\nRun: grove auth login");
e93a9789 return;
e93a97810 }
e93a97811
e93a97812 // Decode JWT payload (no verification — just display)
e93a97813 const parts = config.token.split(".");
e93a97814 if (parts.length !== 3) {
59e666715 log.warn("Invalid token stored.\nRun: grove auth login");
e93a97816 return;
e93a97817 }
e93a97818
e93a97819 try {
e93a97820 const payload = JSON.parse(
e93a97821 Buffer.from(parts[1], "base64url").toString()
e93a97822 );
e93a97823
59e666724 const lines = [`Hub: ${config.hub}`, `User: ${payload.username}`];
59e666725 if (payload.display_name) lines.push(`Name: ${payload.display_name}`);
59e666726 lines.push(`Type: ${payload.type}`);
e93a97827
e93a97828 if (payload.exp) {
e93a97829 const expires = new Date(payload.exp * 1000);
e93a97830 const now = new Date();
e93a97831 if (expires < now) {
59e666732 lines.push(`Expired: ${expires.toLocaleDateString()}`);
59e666733 log.warn(lines.join("\n"));
59e666734 return;
e93a97835 } else {
59e666736 lines.push(`Expires: ${expires.toLocaleDateString()}`);
e93a97837 }
e93a97838 }
59e666739
59e666740 log.info(lines.join("\n"));
e93a97841 } catch {
59e666742 log.warn("Could not decode token.\nRun: grove auth login");
e93a97843 }
e93a97844}