{
  "name": "Obatin \u2014 Login & Auth",
  "nodes": [
    {
      "id": "ffc0fbd0-6b3c-476c-b8ab-ab6ef3959a37",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        250,
        300
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "obatin-login",
        "responseMode": "lastNode",
        "responseData": "all",
        "options": {}
      }
    },
    {
      "id": "8db09247-1e49-4287-8385-e8eab08065de",
      "name": "Get Password",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [
        550,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT value FROM settings WHERE key = 'password_hash' LIMIT 1",
        "additionalFields": {}
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "d98d6cb6-a3b6-4a25-aaa0-c45ef1066637",
      "name": "Check Password",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        850,
        300
      ],
      "parameters": {
        "language": "python",
        "code": "import bcrypt\nitems_input = items\npassword = items[0].json['body']['password']\nhash_val = items[0].json['value']\nvalid = bcrypt.checkpw(password.encode('utf-8'), hash_val.encode('utf-8'))\ntoken = __import__('secrets').token_hex(32)\nreturn [{'json': {'valid': valid, 'token': token, 'password': password, 'hash': hash_val}}]"
      }
    },
    {
      "id": "4a87afdc-d44c-4771-b0f1-89a903970feb",
      "name": "IF Valid",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1150,
        300
      ],
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.valid }}",
              "operation": "equal",
              "value2": true
            }
          ]
        }
      }
    },
    {
      "id": "8f7c0ec5-5a83-48b1-a828-a53339128652",
      "name": "Save Token",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2,
      "position": [
        1450,
        150
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE settings SET value = '{{ $json.token }}', updated_at = NOW() WHERE key = 'token'",
        "additionalFields": {}
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "fd9580ac-f163-47b6-8250-bc71cffc9fd7",
      "name": "Resp OK",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 2,
      "position": [
        1750,
        150
      ],
      "parameters": {
        "respondWith": "json",
        "responseBodyType": "json",
        "responseBodyJson": "={{\"success\": true, \"token\": $json.token}}"
      }
    },
    {
      "id": "2eba7a98-2ae5-448b-82a7-2e3a679d6db8",
      "name": "Resp Fail",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 2,
      "position": [
        1450,
        450
      ],
      "parameters": {
        "respondWith": "json",
        "responseBodyType": "json",
        "responseBodyJson": "={{\"error\": \"Password salah\"}}"
      }
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Get Password",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Password": {
      "main": [
        [
          {
            "node": "Check Password",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Password": {
      "main": [
        [
          {
            "node": "IF Valid",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Valid": {
      "main": [
        [
          {
            "node": "Save Token",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Resp Fail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Token": {
      "main": [
        [
          {
            "node": "Resp OK",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": []
}