{"id":24518250,"url":"https://github.com/flasd/fetch-auth-manager","last_synced_at":"2025-07-27T01:07:48.912Z","repository":{"id":44075253,"uuid":"206875658","full_name":"flasd/fetch-auth-manager","owner":"flasd","description":"Manage authentication in a stateless and simple manner, both in NodeJs and React, using Axios or GraphQL as transporters.","archived":false,"fork":false,"pushed_at":"2022-12-22T12:31:50.000Z","size":498,"stargazers_count":0,"open_issues_count":10,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-08T08:48:13.266Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/flasd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-06T21:19:48.000Z","updated_at":"2019-10-09T20:37:02.000Z","dependencies_parsed_at":"2023-01-30T08:46:05.122Z","dependency_job_id":null,"html_url":"https://github.com/flasd/fetch-auth-manager","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/flasd/fetch-auth-manager","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flasd%2Ffetch-auth-manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flasd%2Ffetch-auth-manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flasd%2Ffetch-auth-manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flasd%2Ffetch-auth-manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flasd","download_url":"https://codeload.github.com/flasd/fetch-auth-manager/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flasd%2Ffetch-auth-manager/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267278840,"owners_count":24063269,"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","status":"online","status_checked_at":"2025-07-26T02:00:08.937Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2025-01-22T01:41:23.778Z","updated_at":"2025-07-27T01:07:48.880Z","avatar_url":"https://github.com/flasd.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fetch-auth-manager\n\nManage authentication in a stateless and simple manner, both in NodeJs and React, using Axios or GraphQL as transporters.\n\n## Usage\n\nInstall the latest version of fetch-auth-manager:\n\n```\nyarn add fetch-auth-manager\n\n// or\nnpm install fetch-auth-manager\n```\n\n### NodeJs\n\nInside NodeJs, you have access to three functions:\n\n```javascript\nconst {\n  manageAuth,\n  authenticate,\n  deauthenticate\n} = \"fetch-auth-manager/server\";\n```\n\n#### manageAuth(options): ExpressMiddleware\n\nThe `manageAuth` function returns a middleware that verifies the authorization header using the `JWT_SECRET` environment variable and injects the decoded payload in `req.user`. If there is no token, this will simply set `req.user` to `null`. **It's up to you to disallow unauthenticated requests!**\n\nUsage:\n\n```javascript\nconst app = require(\"express\")();\nconst { manageAuth } = require(\"fetch-auth-manager/server\");\n\nconst authManager = manageAuth({ secret: process.env.JWT_SECRET });\n\napp.use(authManager);\n```\n\n#### authenticate(response, tokenData, options)\n\nThe `authenticate` function expects the Express Response object, any data you want present inside the token and some options. Call it when you want to set or update the user's JWT token.\n\n```javascript\nconst { authenticate } = require(\"fetch-auth-manager/server\");\n\nconst options = {\n  secret: process.env.JWT_SECRET, // default\n  lifespan: process.env.JWT_LIFESPAN // jwt ttl in seconds\n};\n\nfunction loginController(req, res) {\n  // your login logic\n  authenticate(res, { subject: \"userId goes here\" }, options);\n}\n```\n\n#### deauthenticate(response)\n\nThe `deauthenticate` function expects the Express Response object. It will remove the user's token.\n\n```javascript\nconst { deauthenticate } = require(\"fetch-auth-manager/server\");\n\nfunction logoutController(req, res) {\n  // your logout logic\n  deauthenticate(res);\n}\n```\n\n### Browser\n\n#### Utility methods\n\nYou can call these functions anywhere in your code to control/get auth state:\n\n```javascript\nimport { logout, hasAuth, subscribe } from \"fetch-auth-manager\";\n```\n\n##### logout()\n\nWill log the user out and update all connected providers.\n\n##### hasAuth()\n\nReturns a boolean value telling if the user has auth.\n\n##### subscribe(fn): unsubscribeFn\n\nRegisters a callback that will get called everytime the auth state changes. This function returns a unsubscribe function that cancels the subscription when called.\n\n#### React\n\nAt or near the root of your application, apply the `AuthProvider` component.\n\n```javascript\nimport { AuthProvider } from 'fetch-auth-manager';\n\ndefault function App(props) {\n\treturn (\n\t\t\u003cAuthProvider\u003e\n\t\t\t\u003cYourApp /\u003e\n\t\t\u003c/AuthProvider\u003e\n\t);\n}\n```\n\nThen, whenever you need access to auth state, decorate any component with the `withAuth` HOC.\n\n```javascript\nimport { withAuth } from \"fetch-auth-manager\";\n\nfunction AnyComponent({ hasAuth, decoded }) {\n  // hasAuth is a boolean flag\n  // decoded contains all the token payload\n  return \u003cdiv /\u003e;\n}\n\nexport default withAuth(AnyComponent);\n```\n\n### Transports\n\nTransports are interfaces that connect the frontend code with the server. There are two transports, GraphQL and Axios.\n\n#### GraphQL\n\nThe GraphQL transport has support for both `http` and `ws` links.\n\n##### Http Transport\n\n```javascript\nimport { ApolloClient } from \"apollo-client\";\nimport { ApolloLink } from \"apollo-link\";\nimport { createHttpLink } from \"apollo-link-http\";\nimport { authHttpLink } from \"fetch-auth-manager/dist/link\";\n\nconst httpLink = createHttpLink({\n  uri: \"https://backend.com/graphql\",\n  credentials: \"include\",\n  fetchOptions: {\n    credentials: \"include\"\n  }\n});\n\nexport const client = new ApolloClient({\n  link: ApolloLink.from([authHttpLink, httpLink])\n});\n```\n\n##### WS Transport\n\nTo use the WS transport, you need to prepare your backend. In your Apollo Setup:\n\n```javascript\nconst { parseWSAuth } from 'fetch-auth-manager/server';\n\nconst apolloServer = new ApolloServer({\n\t// ...your Config\n\tsubscriptions: {\n\t\tonConnect: parseWSAuth(options, (params, ws, context) =\u003e {\n\t\t\t// this function is optional\n\t\t\t// params.user has the decoded token\n\t\t})\n\t}\n})\n```\n\nThen, just decorate the WSLink options\n\n```javascript\nimport { ApolloClient } from \"apollo-client\";\nimport { WebSocketLink } from \"apollo-link-ws\";\nimport { withWSAuth } from \"fetch-auth-manager/dist/link\";\n\nconst wsLink = new WebSocketLink({\n  uri: \"ws://backend.com/graphql\",\n  options: withWSAuth({\n    /* your options */\n  })\n});\n\nexport const client = new ApolloClient({\n  link: wsLink\n});\n```\n\n#### Axios\n\nTo use the Axios transport, add the interceptors to your Axios instance.\n\n```javascript\nimport Axios from \"axios\";\nimport {\n  onRequest,\n  onResponse,\n  onResponseError\n} from \"fetch-auth-manager/interceptors\";\n\nconst axios = Axios.create();\n\naxios.interceptors.request.use(onRequest);\naxios.interceptors.response.use(onResponse, onResponseError);\n```\n\n## Copyright e Licença\n\nCopyright (c) 2019 [Marcel de Oliveira Coelho](https://github.com/husscode) sob a [Licença MIT](https://github.com/husscode/cpf-check/blob/master/LICENSE.md). Go Crazy. :rocket:\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflasd%2Ffetch-auth-manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflasd%2Ffetch-auth-manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflasd%2Ffetch-auth-manager/lists"}