Overview
TheisAuthenticated() function verifies whether a user is currently authenticated by validating the JWT token stored in the ScryxCLI configuration.
Function Signature
Return Value
Returns
true if the user has a valid JWT token with a userId claim, false otherwise.How It Works
The function performs the following checks:- Retrieves Configuration: Loads the ScryxCLI configuration using
getConfig() - Token Existence: Checks if
config.user.tokenexists - JWT Secret Validation: Verifies that
JWT_SECRETenvironment variable is defined - Token Verification: Uses
jsonwebtokento verify the token signature - Payload Validation: Ensures the decoded token contains a
userIdfield
Any errors during the authentication check (invalid token, missing secret, etc.) result in
false being returned.Authentication Requirements
For authentication to be considered valid, all of the following must be true:- User configuration contains a token (
config.user.token) JWT_SECRETenvironment variable is set- Token signature is valid and verified
- Decoded token payload contains a
userIdproperty
Implementation Details
Usage Example
Error Handling
The function uses a try-catch block to handle all potential errors gracefully:- Missing Token: Returns
falseif no token exists in configuration - Invalid Token: Returns
falseif JWT verification fails - Expired Token: Returns
falseif token has expired - Missing JWT_SECRET: Returns
falseif environment variable is not set - Malformed Token: Returns
falseif token cannot be decoded
The function never throws exceptions - it always returns a boolean value, making it safe to use in conditional statements.
Related Functions
getConfig()- Retrieves the ScryxCLI configurationisModelSelected()- Checks if a model has been configured
Source Location
src/lib/auth.ts:8