clockworklabs/SpacetimeDB · error · anyhow::Error
Token does not look like a JSON web token
Error message
Token does not look like a JSON web token: {token} What it means
decode_identity manually splits the login token on '.' and expects exactly three JWT segments (header.payload.signature) so it can base64-decode the claims without verifying the signature. Any token that is not that shape — truncated, an opaque key, garbage — fails this check before decoding starts.
Solutions
- Log in again to mint a clean token: `spacetime logout && spacetime login`
- Unset any token overriding env vars so the CLI uses its own config
- If scripting, write the token verbatim from the login flow without trimming or line-wrapping
Example fix
# before export SPACETIMEDB_TOKEN="eyJhbGciOi..." # truncated paste # after spacetime logout && spacetime login # fresh, complete JWT stored
Defensive patterns
Strategy: type-guard
Validate before calling
// Rust: check JWT shape before calling decode_identity
fn looks_like_jwt(token: &str) -> bool {
let parts: Vec<&str> = token.split('.').collect();
parts.len() == 3 && parts.iter().all(|p| !p.is_empty())
} Type guard
fn is_jwt_shaped(token: &str) -> std::result::Result<&str, String> {
match token.split('.').count() {
3 => Ok(token),
n => Err(format!("expected 3 JWT segments, found {n}")),
}
} Prevention
- Never hand-edit or retype tokens; always transfer them programmatically from the login flow
- If auth fails after env changes, suspect an overridden token variable and re-login to reset state
When it happens
Trigger: The token read from CLI config or an env override is not a 3-part JWT: hand-edited token file, truncated paste into SPACETIME_TOKEN-style variables, a refresh/session id used where the JWT is expected, or a partially-written token after an interrupted `spacetime login`.
Common situations: Scripting CI auth and storing the wrong secret; clipboard copies that drop characters; switching between auth systems while stale state remains.
Related errors
- Failed to get token
- Failed to request token
- Invalid JSON: failed to parse string
- Issuer empty
- Issuer mismatch: got
AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20).
Data as JSON: /api/errors/1f442786a9228508.
Report an issue: GitHub.
Appendix: source
Thrown at crates/cli/src/util.rs:338
if force {
println!("Skipping confirmation due to --yes");
return Ok(true);
}
let mut input = String::new();
print!("{prompt} [y/N]");
std::io::stdout().flush()?;
std::io::stdin().read_line(&mut input)?;
let input = input.trim().to_lowercase();
Ok(input == "y" || input == "yes")
}
pub fn decode_identity(token: &String) -> anyhow::Result<String> {
// Here, we manually extract and decode the claims from the json web token.
// We do this without using the `jsonwebtoken` crate because it doesn't seem to have a way to skip signature verification.
// But signature verification would require getting the public key from a server, and we don't necessarily want to do that.
let token_parts: Vec<_> = token.split('.').collect();
if token_parts.len() != 3 {
return Err(anyhow::anyhow!("Token does not look like a JSON web token: {token}"));
}
let decoded_bytes = BASE_64_STD_NO_PAD.decode(token_parts[1])?;
let decoded_string = String::from_utf8(decoded_bytes)?;
let claims_data: IncomingClaims = serde_json::from_str(decoded_string.as_str())?;
let claims_data: SpacetimeIdentityClaims = claims_data.try_into()?;
Ok(claims_data.identity.to_string())
}
pub async fn get_login_token_or_log_in(
config: &mut Config,
target_server: Option<&str>,
interactive: bool,
) -> anyhow::Result<String> {
if let Some(token) = config.spacetimedb_token() {
return Ok(token.clone());
}View on GitHub (pinned to 6dee26c6ef)