{"record":{"id":"f4790d98fcbcf442","repo":"phalcon/cphalcon","slug":"no-default-guard-registered","errorCode":null,"errorMessage":"No default guard registered","messagePattern":"No default guard registered","errorType":"exception","errorClass":"Phalcon\\Auth\\Exceptions\\DefaultGuardNotRegistered","httpStatus":null,"severity":"error","filePath":"phalcon/Auth/Manager.zep","lineNumber":153,"sourceCode":"        return this->defaultGuard;\n    }\n\n    /**\n     * @return array<string, Guard>\n     */\n    public function getGuards() -> array\n    {\n        return this->guards;\n    }\n\n    /**\n     * @throws Exception\n     */\n    public function guard(string name = null) -> <Guard>\n    {\n        if (name === null) {\n            if (this->defaultGuard === null) {\n                throw new DefaultGuardNotRegistered();\n            }\n\n            return this->defaultGuard;\n        }\n\n        if (!isset(this->guards[name])) {\n            throw new GuardNotDefined(name);\n        }\n\n        return this->guards[name];\n    }\n\n    public function id() -> int | string | null\n    {\n        return this->guard()->id();\n    }\n\n    public function logout() -> void","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Auth/Manager.zep#L135-L171","documentation":"Manager::guard() with no argument returns the default guard. The default is only set when a guard is registered with default: true (ManagerFactory::load() passes the 'default' flag from config to Manager::addGuard(), which stores it as defaultGuard). If no guard was marked default - typically because the 'default' key is missing or false on every guard in config - calling guard(), or anything that delegates to it (id(), logout(), user()...), throws DefaultGuardNotRegistered.","triggerScenarios":"Auth config with guards: {web: {...}, api: {...}} but neither entry has default: true; calling $auth->user() / $auth->logout() / $auth->id() on a manager built from such config; constructing Manager manually and only calling addGuard() without ever calling setDefaultGuard().","commonSituations":"Multi-guard configs (web + api) where the author forgets to elect a default; config refactor that drops the 'default' flag; code written against a single guard assuming it is implicitly default.","solutions":["Mark exactly one guard as default in config: guards: {web: {default: true, ...}, api: {...}}","Or set it programmatically: $manager->setDefaultGuard($manager->guard('web'));","Or always pass the guard name explicitly: $auth->guard('web')->user() instead of relying on a default"],"exampleFix":"// before\n'guards' => [\n    'web' => ['adapter' => [...], 'type' => 'session'],\n],\n\n// after\n'guards' => [\n    'web' => ['adapter' => [...], 'type' => 'session', 'default' => true],\n],","handlingStrategy":"validation","validationCode":"$hasDefault = false;\nforeach ($config['guards'] ?? [] as $guard) {\n    $hasDefault = $hasDefault || (bool) ($guard['default'] ?? false);\n}\nif (!$hasDefault) {\n    throw new InvalidArgumentException('Exactly one guard must be marked default: true');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $user = $auth->user();\n} catch (\\Phalcon\\Auth\\Exceptions\\DefaultGuardNotRegistered $e) {\n    // fall back to an explicit guard name instead of relying on a default\n    $user = $auth->guard('web')->user();\n}","preventionTips":["Always mark exactly one guard default: true in multi-guard configs","Prefer explicit guard('name') calls in code so a missing default is not fatal","Assert at boot that $manager->guard() (no args) returns without throwing"],"tags":["auth","guard","configuration","default"],"backgroundTag":"default-guard-not-set","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}