{"id":20891336,"url":"https://github.com/Ispirett/rails_jwt_api","last_synced_at":"2025-05-12T21:31:12.851Z","repository":{"id":56890602,"uuid":"414443924","full_name":"Ispirett/rails_jwt_api","owner":"Ispirett","description":"Simple jwt authentication with rails","archived":false,"fork":false,"pushed_at":"2021-10-13T20:01:31.000Z","size":75,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-26T20:17:03.609Z","etag":null,"topics":["api","authentication","jwt","phone-verfication","rails","ruby","twilio","verfication"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ispirett.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-07T03:05:29.000Z","updated_at":"2021-10-13T20:01:35.000Z","dependencies_parsed_at":"2022-08-21T00:20:31.998Z","dependency_job_id":null,"html_url":"https://github.com/Ispirett/rails_jwt_api","commit_stats":null,"previous_names":["ispirett/rails_jwt"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ispirett%2Frails_jwt_api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ispirett%2Frails_jwt_api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ispirett%2Frails_jwt_api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ispirett%2Frails_jwt_api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ispirett","download_url":"https://codeload.github.com/Ispirett/rails_jwt_api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253572851,"owners_count":21929677,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","authentication","jwt","phone-verfication","rails","ruby","twilio","verfication"],"created_at":"2024-11-18T09:11:52.196Z","updated_at":"2025-05-12T21:31:12.826Z","avatar_url":"https://github.com/Ispirett.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RailsJwtApi\n\n## Work in progress\nSimple jwt rails authentication\n\n## Features\n##### Sign Up\n##### Sign in\n##### Authenticate any Controller action\n##### Refresh Token\n##### Expiration date for token =\u003e default 2.weeks\n##### Phone verification using twilio\n\n\n\n## Installation\n```ruby\ngem 'rails_jwt_api', github: \"https://github.com/Ispirett/rails_jwt\"\nor \ngem 'rails_jwt_api'\n```\nthen execute:\n```bash\n$ bundle\n```\n```bash\nrails g rails_jwt_api:install\n```\nAnd then:\n\n```bash\nrails db:migrate\n```\n## Usage\n* Use authorize_user! helper for authenticating user with token \n```ruby\n:authorize_user!\n```\n```ruby\nclass TomController \u003c ApplicationController\nbefore_action :authorize_user!, only: %w(create update)\nend \n```\n* Use current_user to access the current logged in user\n```ruby\nclass TomController \u003c ApplicationController\n  def create\n    current_user.email \n  end\nend \n```\n## User model \n* This gem creates a user model and handle securing password with bcrypt.\n* After running the gem's install command you will see the code below.\n* The details method is important to the gem.\n* You can add or remove attributes which will affect the response.\n\n```ruby\ndef details\n    as_json(only: [:id, :email,:created_at])\nend\n```\n\n## Routes\n* This gem adds routes to your routes file like so.\n* Sign Up /rails_jwt_api/auth/sign_up\n* Sign In /rails_jwt_api/auth/sign_in\n\n```ruby\n  mount RailsJwtApi::Engine =\u003e \"/rails_jwt_api\", as: :rails_jwt\n```\n\n\n\n\n\n## Making  requests\n##### Sign Up\n\n```html\nrails_jwt_api/auth/sign_up\n```\n\n```json\n{ \"user\":{ \"email\":\"hit@gmail.com\", \"password\": \"foobar\", \"password_confirmation\": \"foobar\"}}\n```\n##### Response\n```json\n{\n    \"status\": \"success\",\n    \"token\": \"eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJleHAiOjE2MzQxNzg2MDd9.eJmaV2_fP8P52LiI9tJx_UTI8nTPxepuADz6KYK_Pew\",\n    \"user\": {\n        \"id\": 1,\n        \"email\": \"hit@gmail.com\",\n        \"created_at\": \"2021-10-07T02:30:07.894Z\",\n    },\n    \"exp\": \"07 20 73 01:00\"\n}\n```\n\n## Sign In\n```html\nrails_jwt_api/auth/sign_in\n\n```\n\n```json\n{ \"user\":{ \"email\":\"hit@gmail.com\", \"password\": \"foobar\"}}\n```\n##### Response\n```json\n{\n    \"status\": \"success\",\n    \"user\": {\n        \"id\": 4,\n        \"email\": \"test@gmail.com\",\n        \"created_at\": \"2021-10-07T17:20:28.592Z\"\n    },\n    \"token\": \"eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo0LCJleHAiOjE2MzM2Mjc2MjJ9.HjMN61WlujV9YYLZAQ3Xog53jbPTugwMrq0rxdNL_Qk\",\n    \"exp\": \"07-21-73 06:52\"\n}\n```\n\n## Request with token\n* Add token user \"AuthToken\": '.........' in  headers\n```js\n      const api_add_error = async (data) =\u003e {\n        const token = 'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2l.................'\n          try {\n              const response = await fetch('http://example.com/resource',{\n                  method: 'POST',\n                  headers: {\n                      'Content-Type': 'application/json',\n                      'AuthToken': token\n                  },\n                  body: JSON.stringify(data)\n              });\n              return  await response.json()\n          }\n          catch (e) {\n                 console.log(e)\n          }\n      }\n```\n\n##Phone Verification\n##### Phone verification sends a 5 digit number to the number of the user.\n* To enable phone verification edit the rails_jwt_api.rb in the initializers folder.\n  \n```ruby\n# Twilio configuration\n  config.phone_verification = true # default: false\n  # config.account_sid # default: Rails.application.credentials.dig(:twilio, :account_sid)\n  # config.auth_token #  Rails.application.credentials.dig(:twilio, :auth_token)\n  config.magic_number   # Rails.application.credentials.dig(:twilio, :magic_number)\n  # config.verification_length = 5  # The length of the verification code\n```\n* Configure twilio configuration in you credentials file like so\n```bash\nEDITOR=\"code --wait\" rails:credentials edit --environment development\n*Note you can also use ENV instead by replacing the configs in the inintialzer file.\n```\n```yaml\ntwilio:\n  account_sid: rerreqereqreqfdafdfd\n  auth_token:  fdfadffdfjereqwrq\n  magic_number: +2132545454\n```\n\n## Request and Response phone verification\n* Sign Up\n```url\n    http://localhost:3000/rails_jwt_api/auth/sign_up\n```\n*POST\n```json\n{ \"user\":{ \"phone\": \"18683292490\",\"email\":\"le@gmail.com\", \n  \"password\": \"12345678\", \"password_confirmation\": \"12345678\"}\n}\n```\n* User receives code\n```json\n{\n    \"status\": \"success\",\n    \"user\": {\n        \"id\": 1,\n        \"email\": \"boo@gmail.com\",\n        \"created_at\": \"2021-10-13T19:49:25.178Z\"\n    },\n    \"msg\": \"Please verify phone number\"}\n```\n\n* Verify Phone\n\n```url\nhttp://localhost:3000/rails_jwt_api/auth/verify_phone\n```\n*POST\n```json\n{\"user_id\":\"32\", \"verification_number\": \"70840\"}\n```\n* Return user and token\n```json\n    {\n  \"status\": \"success\",\n  \"token\": \"eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjozNCwiZXhwIjoxNjM1MzY0MTE0fQ.D6NGW88tR6TlVz587Dbw9uFUHktX2HCFCtKCccb27i8\",\n  \"user\": {\n    \"id\": 1,\n    \"email\": \"boo@gmail.com\",\n    \"created_at\": \"2021-10-13T19:49:25.178Z\"\n  },\n  \"exp\": \"08 09 73 11:46\"\n}\n```\n\n\n\n\n\n\n## Contributing\n\n\n## License\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FIspirett%2Frails_jwt_api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FIspirett%2Frails_jwt_api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FIspirett%2Frails_jwt_api/lists"}