{"id":14955625,"url":"https://github.com/ilsonlasmar/react-native-action-cable-jwt","last_synced_at":"2025-10-06T09:31:13.716Z","repository":{"id":143803737,"uuid":"145705778","full_name":"ilsonlasmar/react-native-action-cable-jwt","owner":"ilsonlasmar","description":"React Native + Action Cable with Authetication JWT","archived":false,"fork":false,"pushed_at":"2020-02-25T09:53:36.000Z","size":12,"stargazers_count":6,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-14T11:21:52.557Z","etag":null,"topics":["action-cable","graphql-ruby","jwt","rails5","react-native"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/ilsonlasmar.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":"2018-08-22T12:25:28.000Z","updated_at":"2018-11-20T15:42:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"3b65135d-836f-4e5b-87cb-daa5aef4f787","html_url":"https://github.com/ilsonlasmar/react-native-action-cable-jwt","commit_stats":{"total_commits":15,"total_committers":1,"mean_commits":15.0,"dds":0.0,"last_synced_commit":"a42f4c752540ae9bc43721b26cda0ddf72266f05"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilsonlasmar%2Freact-native-action-cable-jwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilsonlasmar%2Freact-native-action-cable-jwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilsonlasmar%2Freact-native-action-cable-jwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilsonlasmar%2Freact-native-action-cable-jwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ilsonlasmar","download_url":"https://codeload.github.com/ilsonlasmar/react-native-action-cable-jwt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235515427,"owners_count":19002481,"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":["action-cable","graphql-ruby","jwt","rails5","react-native"],"created_at":"2024-09-24T13:11:27.911Z","updated_at":"2025-10-06T09:31:08.429Z","avatar_url":"https://github.com/ilsonlasmar.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-native-action-cable-jwt\nCompatible 100% Rails 5 + GRAPHQL RUBY ~\u003e '1.8.5' :smiley:\n\nMake your mobile application safer by avoiding to send tokens via url. Submit your jwt token from your application via the Action Cable header. \nWhy not send JWT tokens via URL? read more: https://bit.ly/2nZ4GbS\n\n## INSTALL\n\n```\nnpm install --save react-native-action-cable-jwt\n```\n\n## USAGE\n#### React Native\n```jsx\nimport ActionCableJwt from 'react-native-action-cable-jwt';\nimport ActionCableLink from 'graphql-ruby-client/subscriptions/ActionCableLink';\n\nconst url = 'http://YOUR_URL';\n\nconst wsUri = `ws://${url}/cable`;\nconst actionCableJwt = ActionCableJwt.createConnection(async () =\u003e {\n  const token = await AsyncStorage.getItem('@token');\n  return token;\n});\nconst cable = actionCableJwt.createConsumer(wsUri);\nconst webSocketLink = new ActionCableLink({ cable });\n\n```\n\n#### Rails + GRAPHQL RUBY\napp/channels/graphql_channel.rb\n```ruby\nclass GraphqlChannel \u003c ApplicationCable::Channel\n  def subscribed    \n    @subscription_ids = []\n  end\n\n  def execute(data)\n    query = data['query']\n    variables = ensure_hash(data['variables'])\n    operation_name = data['operationName']\n    context = {\n      current_user: current_user,\n      channel: self,\n    }\n\n    result = YOURSCHEMA.execute(query: query,\n      context: context,\n      variables: variables,\n      operation_name: operation_name)\n\n    payload = {\n      result: result.subscription? ? {data: nil} : result.to_h,\n      more: result.subscription?\n    }\n\n    if result.context[:subscription_id]\n      @subscription_ids \u003c\u003c context[:subscription_id]\n    end\n\n    transmit(payload)\n  end\n\n  def unsubscribed\n    @subscription_ids.each { |sid|\n      YOURSCHEMA.subscriptions.delete_subscription(sid)\n    }\n  end\n\n  private\n\n  def ensure_hash(ambiguous_param)\n    case ambiguous_param\n    when String\n      ambiguous_param.present? ? ensure_hash(JSON.parse(ambiguous_param)) : {}\n    when Hash, ActionController::Parameters\n      ambiguous_param\n    when nil\n      {}\n    else\n      raise ArgumentError, \"Unexpected parameter: #{ambiguous_param}\"\n    end\n  end  \nend\n\n```\napp/channels/application_cable/connection.rb\n```ruby\nmodule ApplicationCable\n  class Connection \u003c ActionCable::Connection::Base\n    identified_by :current_user    \n\n    def connect      \n      self.current_user = find_verified_user || reject_unauthorized_connection      \n    end  \n\n    def disconnect\n      # Any cleanup work needed when the cable connection is cut.\n    end    \n\n    protected\n    def find_verified_user\n      header_array = request.headers[:HTTP_SEC_WEBSOCKET_PROTOCOL].split(',')\n      token = header_array[header_array.length-1]      \n      return nil if token.blank?     \n      # Example with authetication via GEM 'json_web_token' \n      current_user = AuthToken.verify(token)\n       return current_user ? current_user.id : nil\n    end      \n  end\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filsonlasmar%2Freact-native-action-cable-jwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Filsonlasmar%2Freact-native-action-cable-jwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filsonlasmar%2Freact-native-action-cable-jwt/lists"}