{
  "name": "Auth Service (MongoDB Production)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "GET",
        "path": "api/auth/me",
        "options": {}
      },
      "name": "Get User Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.headers[\"x-acl-auth-key\"]}}",
              "operation": "equal",
              "value2": "acl_live_sec_8f92a3b4"
            }
          ]
        }
      },
      "name": "Security Guard Me",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        200,
        0
      ]
    },
    {
      "parameters": {
        "operation": "find",
        "collection": "users",
        "query": "={ \"_id\": \"{{$json.headers.authorization.split(' ')[1]}}\" }"
      },
      "name": "Find User by ID",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        400,
        0
      ],
      "credentials": {
        "mongoDb": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{$json}}",
        "options": {}
      },
      "name": "Respond User",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        600,
        0
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\"error\": \"Unauthorized\", \"message\": \"Invalid or missing security key\"}",
        "options": {
          "responseCode": 401
        }
      },
      "name": "Reject User",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        200,
        200
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "api/auth/login",
        "options": {}
      },
      "name": "Login Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        0,
        400
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json.headers[\"x-acl-auth-key\"]}}",
              "operation": "equal",
              "value2": "acl_live_sec_8f92a3b4"
            }
          ]
        }
      },
      "name": "Security Guard Login",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        200,
        400
      ]
    },
    {
      "parameters": {
        "operation": "find",
        "collection": "users",
        "query": "={ \"email\": \"{{$json.body.email}}\" }"
      },
      "name": "Find User by Email",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        400,
        400
      ],
      "credentials": {
        "mongoDb": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{!!$json[\"_id\"]}}",
              "value2": true
            }
          ]
        }
      },
      "name": "User Exists?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        600,
        400
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n  \"success\": true,\n  \"token\": \"{{$json._id}}\",\n  \"user\": {{$json}}\n}",
        "options": {}
      },
      "name": "Respond Existing",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        800,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const email = $node[\"Login Webhook\"].json.body.email;\nconst userType = $node[\"Login Webhook\"].json.body.userType || 'student';\nlet role = userType;\nlet permissions = [];\n\nif (userType === 'institution') {\n  role = 'admin';\n  permissions = ['view_dashboard', 'bulk_issue', 'view_job_monitor', 'manage_institutions', 'manage_users', 'manage_settings'];\n} else if (userType === 'employer') {\n  permissions = ['view_dashboard', 'verify_credential', 'search_talent'];\n} else if (userType === 'creator') {\n  role = 'CREATOR';\n  permissions = ['issue_credentials', 'view_dashboard'];\n}\n\nreturn {\n  email,\n  role,\n  name: email.split('@')[0],\n  permissions,\n  createdAt: new Date().toISOString()\n};"
      },
      "name": "Prepare New User",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        800,
        500
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "collection": "users"
      },
      "name": "Create User",
      "type": "n8n-nodes-base.mongoDb",
      "typeVersion": 1,
      "position": [
        1000,
        500
      ],
      "credentials": {
        "mongoDb": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\n  \"success\": true,\n  \"token\": \"{{$json._id}}\",\n  \"user\": {{$json}}\n}",
        "options": {}
      },
      "name": "Respond New",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1200,
        500
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\"error\": \"Unauthorized\", \"message\": \"Invalid or missing security key\"}",
        "options": {
          "responseCode": 401
        }
      },
      "name": "Reject Login",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        200,
        600
      ]
    }
  ],
  "connections": {
    "Get User Webhook": {
      "main": [
        [
          {
            "node": "Security Guard Me",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Security Guard Me": {
      "main": [
        [
          {
            "node": "Find User by ID",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reject User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find User by ID": {
      "main": [
        [
          {
            "node": "Respond User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Login Webhook": {
      "main": [
        [
          {
            "node": "Security Guard Login",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Security Guard Login": {
      "main": [
        [
          {
            "node": "Find User by Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reject Login",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find User by Email": {
      "main": [
        [
          {
            "node": "User Exists?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "User Exists?": {
      "main": [
        [
          {
            "node": "Respond Existing",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Prepare New User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare New User": {
      "main": [
        [
          {
            "node": "Create User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create User": {
      "main": [
        [
          {
            "node": "Respond New",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}