{"id":16389572,"url":"https://github.com/mnishiguchi/weather_api","last_synced_at":"2026-05-16T15:34:38.590Z","repository":{"id":84540914,"uuid":"79662134","full_name":"mnishiguchi/weather_api","owner":"mnishiguchi","description":null,"archived":false,"fork":false,"pushed_at":"2017-01-25T23:07:55.000Z","size":134,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-11-13T15:31:55.590Z","etag":null,"topics":["api","jwt","rails"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mnishiguchi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-21T17:49:37.000Z","updated_at":"2017-01-21T17:49:57.000Z","dependencies_parsed_at":"2023-03-05T14:30:40.694Z","dependency_job_id":null,"html_url":"https://github.com/mnishiguchi/weather_api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mnishiguchi/weather_api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnishiguchi%2Fweather_api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnishiguchi%2Fweather_api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnishiguchi%2Fweather_api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnishiguchi%2Fweather_api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mnishiguchi","download_url":"https://codeload.github.com/mnishiguchi/weather_api/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnishiguchi%2Fweather_api/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33108191,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-16T04:41:52.686Z","status":"ssl_error","status_checked_at":"2026-05-16T04:41:52.009Z","response_time":115,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","jwt","rails"],"created_at":"2024-10-11T04:33:39.868Z","updated_at":"2026-05-16T15:34:38.569Z","avatar_url":"https://github.com/mnishiguchi.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Weather Api\n\nIn this repository, I will learn about building Rails api apps.\n\n---\n\n## Enforcing a default format\n\n```rb\nclass ApiController \u003c ApplicationController\n  before_action :set_default_format\n\n  private\n    def set_default_format\n      request.format = :json\n    end\nend\n```\n\n```rb\nclass Api::V1::LocationsController \u003c ApiController\n  # ...\nend\n```\n\n---\n\n## JWTs vs session cookies\n- Both JWTs and session cookies are stateless. If we really want to keep track of\nwho is logged in; who logged out, we will need to implement a whitelist and blacklist\nusing database or in-memory state cache.\n\n#### JWT\n- Header + Payload + Signature ([docs](https://jwt.io/))\n- Encoded with [Base64](https://www.base64decode.org/), anyone can decode a JWT\n- Readable by Javascript\n- Only the server knows the secret key, and using that key, it verifies that the information is valid\n- Must manually add a JWT to a request header on every request\n- Need extra caution for XSS and XSRF\n\n#### Rails session cookies\n- Encrypted with secret key, only the server can decode a cookie\n- HTTP-ONLY - not accessible from Javascript\n- Cookies are automatically sent on every request\n- Rails provides countermeasures against XSS and CSRF\n\n---\n\n## Knock gem\n- Like JWT version of Devise\n- Very easy to set up. Read [docs](https://github.com/nsarno/knock).\n- User model must have an `authenticate` method, similar to the one added by `has_secure_password`.\n\n---\n\n## ruby-jwt gem\n- [docs](https://github.com/jwt/ruby-jwt)\n\n---\n\n## Simulating log in form submission from a single page app\n\n### using cUrl\n\nLog in with password\n\n```bash\n$ curl --data \"auth[email]=user@example.com\u0026auth[password]=password\" http://localhost:3000/api/v1/user_token\n{\"jwt\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0ODUxMTQ1ODMsInN1YiI6MX0.RRkN-X3EGUHhMM3wTfvWujupnmNwXxP9k3bma5SkBYw\"}\n```\n\nTry to access our api without a JWT\n\n```bash\n$ curl -v http://localhost:3000/api/v1/locations/1\n*   Trying ::1...\n* Connected to localhost (::1) port 3000 (#0)\n\u003e GET /api/v1/locations/1 HTTP/1.1\n\u003e Host: localhost:3000\n\u003e User-Agent: curl/7.43.0\n\n\u003e Accept: */*\n\u003e\n\u003c HTTP/1.1 401 Unauthorized\n\u003c X-Frame-Options: SAMEORIGIN\n\u003c X-XSS-Protection: 1; mode=block\n\u003c X-Content-Type-Options: nosniff\n\u003c Content-Type: text/html\n\u003c Cache-Control: no-cache\n\u003c X-Request-Id: 943ae954-54ba-4197-a61d-92d27b488e76\n\u003c X-Runtime: 0.041746\n\u003c Transfer-Encoding: chunked\n\u003c\n* Connection #0 to host localhost left intact\n```\n\nTry to access our api with a JWT\n\n```bash\n$ curl -v -H \"Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0ODUxMTQ1ODMsInN1YiI6MX0.RRkN-X3EGUHhMM3wTfvWujupnmNwXxP9k3bma5SkBYw\" http://localhost:3000/api/v1/locations/1\n*   Trying ::1...\n* Connected to localhost (::1) port 3000 (#0)\n\u003e GET /api/v1/locations/1 HTTP/1.1\n\u003e Host: localhost:3000\n\u003e User-Agent: curl/7.43.0\n\u003e Accept: */*\n\u003e Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0ODUxMTQ1ODMsInN1YiI6MX0.RRkN-X3EGUHhMM3wTfvWujupnmNwXxP9k3bma5SkBYw\n\n\u003e\n\u003c HTTP/1.1 200 OK\n\u003c X-Frame-Options: SAMEORIGIN\n\u003c X-XSS-Protection: 1; mode=block\n\u003c X-Content-Type-Options: nosniff\n\u003c Content-Type: application/json; charset=utf-8\n\u003c ETag: W/\"f6e19543159be2645d531f42e8a400e9\"\n\u003c Cache-Control: max-age=0, private, must-revalidate\n\u003c X-Request-Id: 9ad32672-8c29-4533-98be-bdcf10fb9343\n\u003c X-Runtime: 0.102464\n\u003c Transfer-Encoding: chunked\n\u003c\n* Connection #0 to host localhost left intact\n{\"id\":1,\"name\":\"Washington\",\"curent\":{\"temp\":\"snowy\"}}\n```\n\n### pasting a Javascript snippet into a browser's console\n\n```js\nconst http = new XMLHttpRequest()\nconst url    = '/api/v1/user_token'\nconst params = 'auth[email]=user@example.com\u0026auth[password]=password'\n\nhttp.open('POST', url, true)\n\n// Send the proper header information along with the request\nhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')\n\nhttp.onreadystatechange = () =\u003e {\n  if (http.readyState == 4 \u0026\u0026 http.status == 201) {\n    console.log(http.responseText)\n  }\n}\n\nhttp.send(params)\n```\n\n```\n{\"jwt\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0ODUxMTcyOTYsInN1YiI6MSwiZW1haWwiOiJ1c2VyQGV4YW1wbGUuY29tIiwiYWRtaW4iOnRydWV9.mIj6ni4a2o_6rItFYWgEmCFG6wKBMsgpyVcpP1VxDWs\"}\n```\n\n---\n\n## Decoding the token on the browser\n\n```js\nwindow.atob('eyJleHAiOjE0ODUxMTcyOTYsInN1YiI6MSwiZW1haWwiOiJ1c2VyQGV4YW1wbGUuY29tIiwiYWRtaW4iOnRydWV9')\n\"{\"exp\":1485117296,\"sub\":1,\"email\":\"user@example.com\",\"admin\":true}\"\n```\n\n---\n\n## Models\n\n![](erd/erd.jpg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmnishiguchi%2Fweather_api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmnishiguchi%2Fweather_api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmnishiguchi%2Fweather_api/lists"}