├── .eslintignore ├── .eslintrc.json ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md └── workflows │ ├── CI.js.yml │ └── npm-publish.yml ├── .gitignore ├── .npmignore ├── .prettierignore ├── .prettierrc ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.MD ├── LICENSE ├── README.md ├── __tests__ └── unit │ ├── getData.test.js │ ├── login.test.js │ ├── logout.test.js │ └── verify.test.js ├── example ├── package-lock.json ├── package.json ├── public │ ├── GentySans-Regular.woff │ ├── GentySans-Regular.woff2 │ ├── bg.svg │ ├── hero.png │ ├── icon.png │ └── index.html ├── src │ ├── app.js │ ├── components │ │ ├── Input.js │ │ ├── Input.module.css │ │ ├── NavBar.js │ │ └── NavBar.module.css │ ├── context │ │ ├── authContext.js │ │ └── navContext.js │ ├── global.css │ ├── hooks │ │ └── useInput.js │ ├── index.js │ └── pages │ │ ├── Home.js │ │ ├── Home.module.css │ │ ├── Login.js │ │ └── Login.module.css └── webpack.config.js ├── index.js ├── lib ├── getData.js ├── login.js ├── logout.js └── verify.js ├── package-lock.json └── package.json /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | example/ -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "commonjs": true, 5 | "es2021": true 6 | }, 7 | "extends": "eslint:recommended", 8 | "parserOptions": { 9 | "ecmaVersion": 12 10 | }, 11 | "rules": { 12 | "indent": ["error", 4], 13 | "linebreak-style": "off", 14 | "quotes": ["error", "single"], 15 | "semi": ["error", "always"], 16 | "no-unused-vars": ["warn", { "args": "none" }] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/workflows/CI.js.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 3 | 4 | name: Node.js CI 5 | 6 | on: 7 | push: 8 | branches: [ main ] 9 | pull_request: 10 | branches: [ main ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | strategy: 18 | matrix: 19 | node-version: [14.x] 20 | # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ 21 | 22 | steps: 23 | - uses: actions/checkout@v2 24 | - name: Use Node.js ${{ matrix.node-version }} 25 | uses: actions/setup-node@v2 26 | with: 27 | node-version: ${{ matrix.node-version }} 28 | cache: 'npm' 29 | - name: 'Install dependencies' 30 | run: npm ci 31 | - name: 'Run tests' 32 | run: npm test 33 | - name: 'Check linting' 34 | run: npm run lint 35 | -------------------------------------------------------------------------------- /.github/workflows/npm-publish.yml: -------------------------------------------------------------------------------- 1 | # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages 3 | 4 | name: AuthBee CI 5 | 6 | on: 7 | release: 8 | types: [created] 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v2 15 | - uses: actions/setup-node@v2 16 | with: 17 | node-version: 16 18 | - run: npm i 19 | - run: npm test 20 | 21 | publish-npm: 22 | needs: build 23 | runs-on: ubuntu-latest 24 | steps: 25 | - uses: actions/checkout@v2 26 | - uses: actions/setup-node@v2 27 | with: 28 | node-version: 16 29 | registry-url: https://registry.npmjs.org/ 30 | - run: npm i 31 | - run: npm publish 32 | env: 33 | NODE_AUTH_TOKEN: ${{secrets.npm_token}} 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | example/dist 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # TypeScript v1 declaration files 46 | typings/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Microbundle cache 58 | .rpt2_cache/ 59 | .rts2_cache_cjs/ 60 | .rts2_cache_es/ 61 | .rts2_cache_umd/ 62 | 63 | # Optional REPL history 64 | .node_repl_history 65 | 66 | # Output of 'npm pack' 67 | *.tgz 68 | 69 | # Yarn Integrity file 70 | .yarn-integrity 71 | 72 | # dotenv environment variables file 73 | .env 74 | .env.test 75 | 76 | # parcel-bundler cache (https://parceljs.org/) 77 | .cache 78 | 79 | # Next.js build output 80 | .next 81 | 82 | # Nuxt.js build / generate output 83 | .nuxt 84 | dist 85 | 86 | # Gatsby files 87 | .cache/ 88 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 89 | # https://nextjs.org/blog/next-9-1#public-directory-support 90 | # public 91 | 92 | # vuepress build output 93 | .vuepress/dist 94 | 95 | # Serverless directories 96 | .serverless/ 97 | 98 | # FuseBox cache 99 | .fusebox/ 100 | 101 | # DynamoDB Local files 102 | .dynamodb/ 103 | 104 | # TernJS port file 105 | .tern-port 106 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | example 2 | .eslintignore 3 | .eslintrc.json 4 | .gitignore 5 | .prettierignore 6 | .prettierrc 7 | LICENSE 8 | README.md 9 | node_modules 10 | __tests__ 11 | .github -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | example/node_modules/ -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "semi": true, 4 | "tabWidth": 4 5 | } 6 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | - Using welcoming and inclusive language 12 | - Being respectful of differing viewpoints and experiences 13 | - Gracefully accepting constructive criticism 14 | - Focusing on what is best for the community 15 | - Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | - The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | - Trolling, insulting/derogatory comments, and personal or political attacks 21 | - Public or private harassment 22 | - Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | - Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [shakyaimanjith32@gmail.com](mailto:shakyaimanjith32@gmail.com). All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [https://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: https://contributor-covenant.org 46 | [version]: https://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /CONTRIBUTING.MD: -------------------------------------------------------------------------------- 1 | # Contributing To Auth Bee 2 | 3 | Hi, first of all I'd like to thank you for contributing AuthBee. It's people like you who makes AuthBee like tools great tools for developers. 4 | 5 | Following these guidelines helps to communicate that you respect the time of the fellow developers managing and developing this project. In return, the also should hel you in addressing your issue, assessing changes, and helping to finalize your pull request. 6 | 7 | ## What are the ways you can contribute to AuthBee? 8 | 9 | There are many ways you can contrbute to thi project such as, 10 | 11 | - Improving Documentations 12 | - Bug triaging 13 | - Writing tutorials about Authbee 14 | - Feature requests, etc... 15 | 16 | ## Ground Rules 17 | 18 | - Ensure cross-platform compaitibility for every change you make. 19 | - Create issues for any major changes and enhancements that you wish to make. Discuss things transparently and get community feedback. 20 | - Keep feature versions as small as possible, preferably one new feature per version. 21 | 22 | ## Is this your first contribution? 23 | 24 | No problems. Everyone one was once a beginner. First you can start by checking issues with `good first issue` which are specifically kept for beginners. Also, we recommend you to refer this [article](https://app.egghead.io/playlists/how-to-contribute-to-an-open-source-project-on-github) before making your pull request. 25 | 26 | ## Getting started 27 | 28 | 1. If you havent installed, please install `Node.Js` and `git` 29 | 2. First make a fork of the repo and clone the forked repo into your machine. 30 | 3. Then install all the dependencies by running `npm install` 31 | 4. Next make your changes. 32 | 5. Run `npm run lint` & `npm run test` to check whether there's no any issue in linting or testing. 33 | 6. Next push changes in to your forked repo. 34 | 7. Then create a pull request to the main repo from you forked repo. 35 | 36 | ## How to report a bug? 37 | 38 | When reporting a bug we highly encourage you to use the provided template. Else, you can create a custom issue but we'll not consider it, if it does not has following points. 39 | 40 | - Expected behaviour 41 | - Your environment details (browser version, etc...) 42 | - Your node version 43 | - Steps to reproduce the behaviour 44 | 45 | ## How to suggest a feature? 46 | 47 | If you find yourself wishing for a feature that doesn't exist in AuthBee, you are probably not alone. There are bound to be others out there with similar needs. Many of the features that AuthBee has today have been added because our users saw the need. Open an issue on our issues list on GitHub which describes the feature you would like to see, why you need it, and how it should work. 48 | 49 | ## So that's it and Thank You again for your support in improving AuthBee 🐝 50 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 | # AuthBee 6 | 7 | AuthBee is a NPM package to implement authentication in your JavaScript project. If you are using JWT in your project, this is the best option for you since this tracks expirationDate with the help of localStorage and logouts the usert if token is expired. 8 | 9 | ## Features 10 | 11 | - Token Verification 12 | - Automatic Logout when token expires 13 | 14 | ## Why you should use AuthBee? 15 | 16 | When I was a begineer I felt real difficult to implement client side authentication with lots of complex stuff such as automatic logout, etc... So, no matter whether you are a beginner, intermidiate or an expert. You can use AuthBee to implement client side authentication in you JavaScript application. 17 | 18 | ## How to install AuthBee? 19 | 20 | ``` 21 | npm i authbee 22 | ``` 23 | 24 | ## How to use AuthBee? 25 | 26 | Basicall there are four functions exported from the package and you can use it in any place of your application to retrieve, validate, login or logout user from your application. Check out the code snippets down below and also I recommend you to checkout the React [example](https://github.com/shakyapeiris/AuthBee/tree/main/example) developed using AuthBee. 27 | 28 | ### [1. Login](https://github.com/shakyapeiris/AuthBee/blob/main/lib/login.js) 29 | 30 | Login is a promise and you must take `async` or `then` approach to use it. Also, you must send an object as a parameter of the function which contains the **_expiresIn_** property and **_token_** property. 31 | 32 | ```javascript 33 | import { login } from 'authbee' 34 | ... 35 | const submiLoginData = async(e) => { 36 | try{ 37 | const url = 'https://yourapi.com/login'; 38 | const response = await fetch(url, { 39 | method: 'POST', 40 | body: JSON.stringify({ 41 | email: 'user@abc.com', 42 | password: '123', 43 | }), 44 | headers: { 45 | 'Content-Type': 'application/json' 46 | } 47 | }) 48 | 49 | // most of the time an object 50 | const data = await response.json(); 51 | 52 | // expiresIn must be an integer 53 | await login({ token: data.token, expiresIn: data.expiresIn }) 54 | }catch(console.log) 55 | } 56 | ``` 57 | 58 | ### [2. Logout](https://github.com/shakyapeiris/AuthBee/blob/main/lib/logout.js) 59 | 60 | Logout is also a promise and since I use `async/await` in the previous example, I'll use `then` for this one. Also, there must be a token in the local storage aleady and if not the function will throw an error. 61 | 62 | ```javascript 63 | import { logout } from 'authbee' 64 | ... 65 | const logoutHandler = () => { 66 | logout() 67 | .then(() => { 68 | ... 69 | }) 70 | .catch(console.log) 71 | } 72 | ``` 73 | 74 | ### [3. Verify](https://github.com/shakyapeiris/AuthBee/blob/main/lib/verify.js) 75 | 76 | Verify is the main function of the package since it is the function responsible for token validation and automatic logout functionalities. Most suitable place to use this function is in app wide state like contextAPI, redux, etc... 77 | 78 | ```javascript 79 | import { verify } from 'authbee' 80 | ... 81 | const authContext = () => { 82 | // basically verify returns a boolean 83 | if (verify()){ 84 | ... 85 | } 86 | else { 87 | ... 88 | } 89 | } 90 | ``` 91 | 92 | ### [4. Get Data](https://github.com/shakyapeiris/AuthBee/blob/main/lib/getData.js) 93 | 94 | Using get data function, you can retrieve the data stored in local storage when you logged in to the system and this is useful if you want to get the token in order to use it as an authentication header when send a request to the backend. 95 | 96 | getData is also a promise and if the user is not logged in to the system (no token is stored in localStorage), it will send an error. 97 | 98 | ```javascript 99 | import { getData } from 'authbee' 100 | ... 101 | const fetchData = () => { 102 | getData() 103 | .then(({ token, expiresIn }) => { 104 | ... 105 | }) 106 | .catch(console.log) 107 | } 108 | ``` 109 | 110 | ## Future of AuthBee 111 | 112 | I'm planning to implement following features into AuthBee inorder to make it more efficient. 113 | 114 | - Add Type definitions 115 | - Make auth bee usable for Server Side Rendered applications 116 | - Etc... 117 | 118 | I highly appreciate you contribution in improving AuthBee. Please refer following resources if you are willing to contribute to AuthBee. 119 | 120 | - [Contribution Guidlines]() 121 | - [Code of Conduct]() 122 | 123 | ## How to get more help? 124 | 125 | - [Twitter](https://twitter.com/Shakya55007271) 126 | - [Email](shakyaimanjith32@gmail.com) 127 | - [Instagram](https://www.instagram.com/thep33ra/) 128 | - Discord: Shakya Peiris#9502 129 | 130 | ## Thank You 🐝! 131 | -------------------------------------------------------------------------------- /__tests__/unit/getData.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @jest-environment jsdom 3 | */ 4 | /*eslint-env jest*/ 5 | const getData = require('../../lib/getData'); 6 | 7 | let values = {}; 8 | window.localStorage.setItem = jest.fn((key, value) => { 9 | values[key] = value; 10 | }); 11 | window.localStorage.removeItem = jest.fn((key) => { 12 | values[key] = null; 13 | }); 14 | window.localStorage.getItem = jest.fn((key) => values[key]); 15 | 16 | let dummyData = { 17 | token: '1234', 18 | expiresIn: 24, 19 | }; 20 | 21 | describe('Unit tests of getData function', () => { 22 | test('Should send an error if not logged in', (done) => { 23 | expect.assertions(1); 24 | expect(getData()).rejects.toEqual('Must be logged in order to retrieve data'); 25 | done(); 26 | }); 27 | 28 | test('Should send data if user is already logged in', (done) => { 29 | localStorage.setItem('token', dummyData.token); 30 | localStorage.setItem('expiresIn', dummyData.expiresIn); 31 | dummyData.expiresIn = dummyData.expiresIn.toString(); 32 | getData().then((data) => { 33 | expect(data).toStrictEqual(dummyData); 34 | done(); 35 | }).catch(done); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /__tests__/unit/login.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @jest-environment jsdom 3 | */ 4 | /*eslint-env jest*/ 5 | const login = require('../../lib/login'); 6 | 7 | let values = {}; 8 | window.localStorage.setItem = jest.fn((key, value) => { 9 | values[key] = value; 10 | }); 11 | window.localStorage.getItem = jest.fn((key) => values[key]); 12 | 13 | let dummyData = { 14 | token: '1234', 15 | expiresIn: 24, 16 | }; 17 | 18 | describe('Unit tests of login module', () => { 19 | test('Should set data in local storage after logging', (done) => { 20 | login(dummyData) 21 | .then(() => { 22 | expect( 23 | localStorage.getItem('token') 24 | ).toBe(dummyData.token); 25 | done(); 26 | }) 27 | .catch(done); 28 | }); 29 | 30 | test('Should send a error if token or expiresIn is missing', (done) => { 31 | expect.assertions(1); 32 | expect(login()).rejects.toEqual('Missing the token or expiresIn'); 33 | done(); 34 | }); 35 | 36 | test('Should send an error when expiration date is not a number', (done) => { 37 | dummyData.expiresIn = 'someText'; 38 | expect.assertions(1); 39 | expect(login(dummyData)).rejects.toEqual('Expiration date must be an integer'); 40 | done(); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /__tests__/unit/logout.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @jest-environment jsdom 3 | */ 4 | /*eslint-env jest*/ 5 | const logout = require('../../lib/logout'); 6 | 7 | let values = {}; 8 | window.localStorage.setItem = jest.fn((key, value) => { 9 | values[key] = value; 10 | }); 11 | window.localStorage.removeItem = jest.fn((key) => { 12 | values[key] = null; 13 | }); 14 | window.localStorage.getItem = jest.fn((key) => values[key]); 15 | 16 | let dummyData = { 17 | token: '1234', 18 | expiresIn: 24, 19 | }; 20 | 21 | describe('Unit tests of logout function', () => { 22 | beforeAll(() => { 23 | localStorage.setItem('token', dummyData.token); 24 | localStorage.setItem('expiresIn', dummyData.expiresIn); 25 | }); 26 | 27 | test('Data should not be shown when logout function is called', (done) => { 28 | logout().then(() => { 29 | const token = localStorage.getItem('token'); 30 | const expiresIn = localStorage.getItem('expiresIn'); 31 | expect(token).toBeNull; 32 | expect(expiresIn).toBeNull; 33 | done(); 34 | }).catch(done); 35 | }); 36 | 37 | test('Should send an error if not logged in already', (done) => { 38 | localStorage.removeItem('token'); 39 | expect.assertions(1); 40 | expect(logout()).rejects.toEqual('Must be logged in order to logout'); 41 | done(); 42 | }); 43 | 44 | }); 45 | -------------------------------------------------------------------------------- /__tests__/unit/verify.test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @jest-environment jsdom 3 | */ 4 | /*eslint-env jest*/ 5 | const verify = require('../../lib/verify'); 6 | 7 | let values = {}; 8 | window.localStorage.setItem = jest.fn((key, value) => { 9 | values[key] = value; 10 | }); 11 | window.localStorage.removeItem = jest.fn((key) => { 12 | values[key] = null; 13 | }); 14 | window.localStorage.getItem = jest.fn((key) => values[key]); 15 | 16 | let dummyData = { 17 | token: '1234', 18 | expiresIn: 24, 19 | }; 20 | 21 | describe('Unit tests of verify function', () => { 22 | test('Should send false if expiration time is not set', (done) => { 23 | expect(verify()).toBe(false); 24 | done(); 25 | }); 26 | 27 | test('Should send true if expiration time is not reached', (done) => { 28 | const expirationDate = new Date().getTime() + dummyData.expiresIn * 3600000; 29 | localStorage.setItem('token', dummyData.token); 30 | localStorage.setItem('expiresIn', expirationDate); 31 | 32 | expect(verify()).toBe(true); 33 | done(); 34 | }); 35 | 36 | test('Should send false if expired', (done) => { 37 | const expirationDate = new Date().getTime() + dummyData.expiresIn * 3600000; 38 | localStorage.setItem('token', dummyData.token); 39 | localStorage.setItem('expiresIn', expirationDate); 40 | 41 | jest 42 | .useFakeTimers() 43 | .setSystemTime(expirationDate + 500); 44 | 45 | expect(verify()).toBe(false); 46 | done(); 47 | }); 48 | }); 49 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "rimraf dist && webpack", 8 | "start": "webpack serve", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "keywords": [], 12 | "author": "", 13 | "license": "ISC", 14 | "dependencies": { 15 | "@tabler/icons": "^1.104.0", 16 | "authbee": "^1.0.2", 17 | "dotenv": "^10.0.0", 18 | "react": "^17.0.2", 19 | "react-dom": "^17.0.2" 20 | }, 21 | "devDependencies": { 22 | "@babel/core": "^7.16.0", 23 | "@babel/preset-env": "^7.16.0", 24 | "@babel/preset-react": "^7.16.0", 25 | "babel-loader": "^8.2.3", 26 | "css-loader": "^6.5.0", 27 | "html-webpack-plugin": "^5.5.0", 28 | "rimraf": "^3.0.2", 29 | "style-loader": "^3.3.1", 30 | "webpack": "^5.61.0", 31 | "webpack-cli": "^4.9.1", 32 | "webpack-dev-server": "^4.11.1" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /example/public/GentySans-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shakyapeiris/AuthBee/c675622418d8f5891189d6093a72b504630cdac2/example/public/GentySans-Regular.woff -------------------------------------------------------------------------------- /example/public/GentySans-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shakyapeiris/AuthBee/c675622418d8f5891189d6093a72b504630cdac2/example/public/GentySans-Regular.woff2 -------------------------------------------------------------------------------- /example/public/bg.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /example/public/hero.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shakyapeiris/AuthBee/c675622418d8f5891189d6093a72b504630cdac2/example/public/hero.png -------------------------------------------------------------------------------- /example/public/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shakyapeiris/AuthBee/c675622418d8f5891189d6093a72b504630cdac2/example/public/icon.png -------------------------------------------------------------------------------- /example/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | AuthBee example 8 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | -------------------------------------------------------------------------------- /example/src/app.js: -------------------------------------------------------------------------------- 1 | import React, { useContext } from 'react'; 2 | import NavBar from './components/NavBar'; 3 | import Home from './pages/Home'; 4 | import Login from './pages/Login'; 5 | import { navContext } from './context/navContext'; 6 | 7 | const app = () => { 8 | const navCtx = useContext(navContext); 9 | return ( 10 | <> 11 | 12 | {navCtx.route === '/' ? ( 13 | 14 | ) : navCtx.route === '/login' ? ( 15 | 16 | ) : ( 17 |
404!
18 | )} 19 |
20 | 21 | ); 22 | }; 23 | 24 | export default app; 25 | -------------------------------------------------------------------------------- /example/src/components/Input.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import classes from './Input.module.css' 3 | 4 | const Input = ({ 5 | label, 6 | id, 7 | placeholder, 8 | onChange, 9 | value, 10 | onBlur, 11 | error, 12 | errorMessage, 13 | }) => { 14 | return ( 15 |
16 | {label && } 17 | 24 | {error && {errorMessage}} 25 |
26 | ); 27 | }; 28 | 29 | export default Input; 30 | -------------------------------------------------------------------------------- /example/src/components/Input.module.css: -------------------------------------------------------------------------------- 1 | .InputContainer { 2 | display: block; 3 | padding: 10px; 4 | width: 30%; 5 | min-width: 300px; 6 | margin-top: 15px; 7 | } 8 | 9 | .InputContainer label{ 10 | display: block; 11 | font-weight: 600; 12 | text-transform: uppercase; 13 | letter-spacing: 1.5px; 14 | font-size: 12px; 15 | margin-left: 15px; 16 | } 17 | 18 | .InputContainer input { 19 | width: 100%; 20 | display: block; 21 | outline: none; 22 | border: 2px solid #563e33; 23 | color: #563e33; 24 | padding: 15px; 25 | border-radius: 3px; 26 | font-family: 'Raleway', sans-serif; 27 | border-radius: 50px; 28 | } -------------------------------------------------------------------------------- /example/src/components/NavBar.js: -------------------------------------------------------------------------------- 1 | import React, { useContext } from 'react'; 2 | import { authContext } from '../context/authContext'; 3 | import { navContext } from '../context/navContext'; 4 | import classes from './NavBar.module.css'; 5 | import { IconUserCircle, IconLogout } from "@tabler/icons" 6 | 7 | 8 | const NavBar = ({ children }) => { 9 | const authCtx = useContext(authContext); 10 | const navCtx = useContext(navContext); 11 | return ( 12 |
13 |
14 |

AuthBee

15 | 66 |
67 |
{children}
68 |
69 | ); 70 | }; 71 | 72 | export default NavBar; 73 | -------------------------------------------------------------------------------- /example/src/components/NavBar.module.css: -------------------------------------------------------------------------------- 1 | .Main{ 2 | width: 100%; 3 | height: 100%; 4 | display: flex; 5 | flex-direction: column; 6 | } 7 | 8 | .Header { 9 | display: flex; 10 | padding: 50px 50px 20px 50px; 11 | align-items: center; 12 | justify-content: space-between; 13 | width: 100%; 14 | /* background: #fdf9f1; */ 15 | } 16 | 17 | .Header h1 { 18 | font-weight: 800; 19 | } 20 | 21 | .Nav ul { 22 | list-style: none; 23 | display: flex; 24 | align-items: center; 25 | } 26 | 27 | .Nav ul li { 28 | margin-right: 15px; 29 | margin-left: 15px; 30 | cursor: pointer; 31 | transition: 0.1s border-color ease; 32 | font-size: 18px; 33 | font-weight: 500; 34 | height: 100%; 35 | transition: 0.1s ease-in-out; 36 | } 37 | 38 | .testBtn{ 39 | background-color: #ffd740; 40 | padding: 9px 17.5px; 41 | border-radius: 50px; 42 | font-size: 16px !important; 43 | font-weight: 600 !important; 44 | border: 2px solid #563e33; 45 | white-space: nowrap; 46 | } 47 | 48 | .account{ 49 | background-color: #ffd740; 50 | padding: 9px; 51 | border-radius: 50px; 52 | font-size: 16px !important; 53 | font-weight: 600 !important; 54 | border: 2px solid #563e33; 55 | white-space: nowrap; 56 | aspect-ratio: 1/1; 57 | display: grid; 58 | place-items: center; 59 | } 60 | 61 | .logout{ 62 | background-color: #563e33 !important; 63 | color: #ffd740 !important; 64 | padding: 9px 17.5px; 65 | border-radius: 50px; 66 | font-size: 16px !important; 67 | font-weight: 600 !important; 68 | border: 2px solid #563e33; 69 | white-space: nowrap; 70 | display: flex; 71 | align-items: center; 72 | gap: 10px; 73 | } 74 | 75 | .Nav ul li:not(.testBtn,.account,.logout):hover { 76 | padding-bottom: 3px; 77 | border-bottom: 3px solid #563e33; 78 | } 79 | 80 | .Nav ul li:last-child { 81 | margin-right: none; 82 | } 83 | 84 | .webPage{ 85 | flex: 1; 86 | } 87 | 88 | -------------------------------------------------------------------------------- /example/src/context/authContext.js: -------------------------------------------------------------------------------- 1 | import React, { createContext, useState, useEffect } from 'react'; 2 | import { login, logout, verify, getData } from 'authbee'; 3 | 4 | export const authContext = createContext({ 5 | login: () => {}, 6 | logout: () => {}, 7 | token: null, 8 | }); 9 | 10 | const AuthProvider = ({ children }) => { 11 | const [token, setToken] = useState(null); 12 | 13 | useEffect(() => { 14 | const verified = verify(); 15 | 16 | if (verified) { 17 | getData() 18 | .then(({ token }) => { 19 | setToken(token); 20 | console.log(token); 21 | }) 22 | .catch(console.log); 23 | } else { 24 | console.log('Unverified!'); 25 | } 26 | }, []); 27 | 28 | const authLogin = (loginData, callback) => { 29 | login(loginData) 30 | .then(() => { 31 | setToken(loginData.token) 32 | callback(); 33 | }).catch(console.log) 34 | } 35 | 36 | const authLogout = (callback) => { 37 | logout() 38 | .then(() => { 39 | setToken(null); 40 | callback(); 41 | }).catch(console.log) 42 | } 43 | 44 | const value = { 45 | login: authLogin, 46 | logout: authLogout, 47 | token, 48 | }; 49 | 50 | return ( 51 | {children} 52 | ); 53 | }; 54 | 55 | export default AuthProvider; 56 | -------------------------------------------------------------------------------- /example/src/context/navContext.js: -------------------------------------------------------------------------------- 1 | import React, { createContext, useState } from 'react'; 2 | import { authContext } from './authContext'; 3 | 4 | export const navContext = createContext({ 5 | navigate: (route) => {}, 6 | route: '/', 7 | }); 8 | 9 | const NavProvider = ({ routes, children }) => { 10 | const [route, setRoute] = useState('/'); 11 | const navigate = (route) => { 12 | if (!routes.includes(route)) { 13 | setRoute('*'); 14 | } else { 15 | setRoute(route); 16 | } 17 | }; 18 | 19 | const value = { 20 | route, 21 | navigate, 22 | }; 23 | return {children}; 24 | }; 25 | 26 | export default NavProvider; 27 | -------------------------------------------------------------------------------- /example/src/global.css: -------------------------------------------------------------------------------- 1 | @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap'); 2 | 3 | * { 4 | box-sizing: border-box; 5 | margin: 0; 6 | padding: 0; 7 | } 8 | 9 | html,body,#root{ 10 | width: 100%; 11 | height: 100%; 12 | } 13 | 14 | body { 15 | font-family: 'Roboto', sans-serif; 16 | color: #563e33; 17 | /* background: #ffd740; */ 18 | background: #fdf9f1; 19 | } 20 | 21 | h1,h2,h3,h4,h5,h6{ 22 | font-family: 'Genty'; 23 | } 24 | 25 | a{ 26 | color: #563e33 27 | } 28 | 29 | .error{ 30 | color: red; 31 | } 32 | 33 | .notFound{ 34 | font-family: 'Genty'; 35 | font-size: 1.2em; 36 | text-align: center; 37 | margin-top: 50px; 38 | width: 100%; 39 | } -------------------------------------------------------------------------------- /example/src/hooks/useInput.js: -------------------------------------------------------------------------------- 1 | import { useRef, useState } from 'react'; 2 | 3 | const useInput = (validator) => { 4 | const [inputValue, setInputValue] = useState(''); 5 | const [inputTouched, setInputTouched] = useState(false); 6 | const inputRef = useRef(null); 7 | const isInputValid = validator(inputValue); 8 | const hasError = !isInputValid && inputTouched; 9 | 10 | const valueChangeHandler = (event) => { 11 | setInputValue(event.target.value); 12 | }; 13 | 14 | const inputBlurHandler = () => { 15 | setInputTouched(true); 16 | }; 17 | 18 | const focusHandler = () => { 19 | setInputTouched(true); 20 | if (inputRef.current) inputRef.current.focus(); 21 | }; 22 | 23 | const reset = () => { 24 | setInputValue(''); 25 | setInputTouched(false); 26 | }; 27 | 28 | return { 29 | inputValue, 30 | isInputValid, 31 | hasError, 32 | valueChangeHandler, 33 | inputBlurHandler, 34 | reset, 35 | inputRef, 36 | focusHandler, 37 | setInputValue, 38 | }; 39 | }; 40 | 41 | export default useInput; 42 | -------------------------------------------------------------------------------- /example/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './app'; 4 | import AuthProvider from './context/authContext'; 5 | import NavProvider from './context/navContext'; 6 | import './global.css'; 7 | 8 | ReactDOM.render( 9 | 10 | 11 | 12 | 13 | , 14 | document.getElementById('root') 15 | ); 16 | -------------------------------------------------------------------------------- /example/src/pages/Home.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import classes from './Home.module.css' 3 | import {IconDownload,IconStar} from "@tabler/icons" 4 | 5 | const Home = () => { 6 | return <> 7 |
8 |
9 | 10 |

Welcome To The Bee Hive!

11 |

No more stings!

12 |
13 | Install 14 | Star Me On Github 15 |
16 |
17 |
18 | ; 19 | }; 20 | 21 | export default Home; 22 | -------------------------------------------------------------------------------- /example/src/pages/Home.module.css: -------------------------------------------------------------------------------- 1 | .Container{ 2 | width: 100%; 3 | height: 100%; 4 | display: flex; 5 | flex-direction: column; 6 | align-items: center; 7 | justify-content: center; 8 | text-align: center; 9 | position: relative; 10 | } 11 | 12 | .wrapper{ 13 | width: 100%; 14 | height: 100%; 15 | display: flex; 16 | flex-direction: column; 17 | align-items: center; 18 | text-align: center; 19 | z-index: 2; 20 | padding-top: 10px; 21 | } 22 | 23 | .Container::before { 24 | content: ""; 25 | background-image: url('../../public/bg.svg'); 26 | background-size: cover; 27 | position: absolute; 28 | top: 0px; 29 | right: 0px; 30 | bottom: 0px; 31 | left: 0px; 32 | opacity: 0.03; 33 | z-index: 1; 34 | } 35 | 36 | img{ 37 | width: 250px; 38 | height: auto; 39 | margin-bottom: 25px; 40 | } 41 | 42 | .Container h1{ 43 | font-size: 3rem; 44 | } 45 | 46 | .Container p{ 47 | font-size: 20px; 48 | margin: 25px 0; 49 | } 50 | 51 | .btnGroup{ 52 | display: flex; 53 | align-items: center; 54 | gap: 10px; 55 | } 56 | 57 | .Container a{ 58 | background-color: #ffd740; 59 | padding: 12.5px 22.5px; 60 | border-radius: 50px; 61 | font-size: 16px !important; 62 | font-weight: 600 !important; 63 | border: 2px solid #563e33; 64 | white-space: nowrap; 65 | margin: 25px 0; 66 | text-decoration: none; 67 | display: flex; 68 | align-items: center; 69 | gap: 10px; 70 | } 71 | 72 | .Container a.install{ 73 | background-color: #563e33 !important; 74 | color: #ffd740 !important; 75 | } -------------------------------------------------------------------------------- /example/src/pages/Login.js: -------------------------------------------------------------------------------- 1 | import React, { useContext, useState } from 'react'; 2 | import Input from '../components/Input'; 3 | import { authContext } from '../context/authContext'; 4 | import { navContext } from '../context/navContext'; 5 | import useInput from '../hooks/useInput' 6 | import classes from './Login.module.css' 7 | import {IconLogin} from "@tabler/icons" 8 | 9 | 10 | const Login = () => { 11 | const usernameValidator = useInput((inputVal) => inputVal.trim() != ""); 12 | const passwordValidator = useInput((inputVal) => inputVal.trim().length > 6); 13 | 14 | const [isLogin, setIsLogin] = useState(true) 15 | 16 | const authCtx = useContext(authContext); 17 | const navCtx = useContext(navContext); 18 | 19 | const submiFormHandler = (e) => { 20 | e.preventDefault(); 21 | 22 | if (!usernameValidator.isInputValid) return usernameValidator.inputBlurHandler(); 23 | if (!passwordValidator.isInputValid) return passwordValidator.inputBlurHandler(); 24 | 25 | const body = { 26 | email: usernameValidator.inputValue, 27 | password: passwordValidator.inputValue, 28 | } 29 | 30 | usernameValidator.reset(); 31 | passwordValidator.reset(); 32 | 33 | if (isLogin){ 34 | authCtx.login({ token: 'dummyToken', expiresIn: 24 }, () => { 35 | navCtx.navigate('/') 36 | }) 37 | } 38 | else { 39 | setIsLogin(true); 40 | } 41 | 42 | 43 | console.log(body) 44 | } 45 | 46 | return ( 47 | <> 48 |
49 |

{isLogin ? 'Login' : 'Register'}

50 | 60 | 70 | 71 |

{isLogin ? 'Dont Have An Account?' : 'Already Have An Account?'}

72 |

{ setIsLogin(curr => !curr) }}>{isLogin ? 'Register' : 'Login'}

73 |
74 | 75 | ); 76 | }; 77 | 78 | export default Login; 79 | -------------------------------------------------------------------------------- /example/src/pages/Login.module.css: -------------------------------------------------------------------------------- 1 | .Form{ 2 | width: 100%; 3 | height: 100%; 4 | display: flex; 5 | flex-direction: column; 6 | align-items: center; 7 | padding-top: 50px; 8 | } 9 | 10 | .Form h1{ 11 | text-align: center; 12 | } 13 | 14 | .Form button{ 15 | background-color: #563e33 !important; 16 | color: #ffd740 !important; 17 | padding: 12.5px 22.5px; 18 | border-radius: 50px; 19 | font-size: 16px !important; 20 | font-weight: 600 !important; 21 | border: 2px solid #563e33; 22 | white-space: nowrap; 23 | margin: 25px 0 15px 0; 24 | text-decoration: none; 25 | display: flex; 26 | align-items: center; 27 | gap: 10px; 28 | transition: 0.1s ease-in-out; 29 | cursor: pointer; 30 | } 31 | 32 | .Form button:hover{ 33 | transform: transitionY(5px); 34 | } 35 | 36 | .MainText, .SubText { 37 | text-align: center; 38 | font-size: 14px; 39 | } 40 | 41 | .MainText { 42 | margin-top: 10px; 43 | } 44 | 45 | .SubText { 46 | font-weight: 600; 47 | cursor: pointer; 48 | } -------------------------------------------------------------------------------- /example/webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const HTMLWebpackPlugin = require('html-webpack-plugin'); 3 | 4 | module.exports = { 5 | entry: path.resolve(__dirname, 'src', 'index.js'), 6 | output: { 7 | path: path.resolve(__dirname, 'dist'), 8 | filename: '[name].[contenthash].js', 9 | }, 10 | mode: 'production', 11 | module: { 12 | rules: [ 13 | { 14 | test: /\.js$/, 15 | exclude: /node_modules/, 16 | use: { 17 | loader: 'babel-loader', 18 | options: { 19 | presets: ['@babel/preset-env', '@babel/preset-react'], 20 | }, 21 | }, 22 | }, 23 | { 24 | test: /\.css$/, 25 | use: ['style-loader', 'css-loader'], 26 | }, 27 | ], 28 | }, 29 | plugins: [ 30 | new HTMLWebpackPlugin({ 31 | template: path.resolve(__dirname, 'public', 'index.html'), 32 | }), 33 | ], 34 | }; 35 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const login = require('./lib/login'); 2 | const logout = require('./lib/logout'); 3 | const getData = require('./lib/getData'); 4 | const verify = require('./lib/verify'); 5 | 6 | module.exports = { 7 | login, 8 | logout, 9 | getData, 10 | verify, 11 | }; 12 | -------------------------------------------------------------------------------- /lib/getData.js: -------------------------------------------------------------------------------- 1 | const getData = () => { 2 | return new Promise((resolve, reject) => { 3 | const expiresIn = localStorage.getItem('expiresIn'); 4 | const token = localStorage.getItem('token'); 5 | if (!token) { 6 | const errorMessage = 'Must be logged in order to retrieve data'; 7 | reject(errorMessage); 8 | } 9 | resolve({ expiresIn, token }); 10 | }); 11 | }; 12 | 13 | module.exports = getData; 14 | -------------------------------------------------------------------------------- /lib/login.js: -------------------------------------------------------------------------------- 1 | const login = (loginData) => { 2 | return new Promise((resolve, reject) => { 3 | if (!loginData || !(loginData.token && loginData.expiresIn)) { 4 | const errorMessage = 'Missing the token or expiresIn'; 5 | reject(errorMessage); 6 | } 7 | const expirationTime = loginData.expiresIn; 8 | if (isNaN(expirationTime)) { 9 | const errorMessage = 'Expiration date must be an integer'; 10 | reject(errorMessage); 11 | } 12 | const expirationDate = new Date().getTime() + expirationTime * 3600000; 13 | 14 | loginData.expiresIn = expirationDate; 15 | localStorage.setItem('expiresIn', expirationDate); 16 | localStorage.setItem('token', loginData.token); 17 | resolve(); 18 | }); 19 | }; 20 | 21 | module.exports = login; 22 | -------------------------------------------------------------------------------- /lib/logout.js: -------------------------------------------------------------------------------- 1 | const logout = () => { 2 | return new Promise((resolve, reject) => { 3 | const token = localStorage.getItem('token'); 4 | if (!token) { 5 | const errorMessage = 'Must be logged in order to logout'; 6 | reject(errorMessage); 7 | } 8 | 9 | localStorage.removeItem('token'); 10 | localStorage.removeItem('expiresIn'); 11 | resolve(); 12 | }); 13 | }; 14 | 15 | module.exports = logout; 16 | -------------------------------------------------------------------------------- /lib/verify.js: -------------------------------------------------------------------------------- 1 | const verify = () => { 2 | const expiresIn = localStorage.getItem('expiresIn'); 3 | 4 | let timer; 5 | 6 | if (expiresIn == null || expiresIn == undefined) { 7 | localStorage.removeItem('expiresIn'); 8 | localStorage.removeItem('token'); 9 | return false; 10 | } 11 | 12 | const remainingTime = parseInt(expiresIn) - new Date().getTime(); 13 | 14 | timer = setTimeout(() => { 15 | if (remainingTime <= 1000) { 16 | localStorage.removeItem('expiresIn'); 17 | localStorage.removeItem('token'); 18 | return false; 19 | } 20 | }, remainingTime - 1000); 21 | 22 | if (remainingTime <= 0) { 23 | localStorage.removeItem('expiresIn'); 24 | localStorage.removeItem('token'); 25 | clearTimeout(timer); 26 | return false; 27 | } 28 | 29 | return true; 30 | }; 31 | 32 | module.exports = verify; 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "authbee", 3 | "version": "1.0.2", 4 | "description": "AuthBee is a NPM package to implement authentication in your JavaScript project", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "jest", 8 | "lint": "npx eslint ." 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/shakyapeiris/AuthBee.git" 13 | }, 14 | "keywords": [ 15 | "javascript", 16 | "authentication", 17 | "authorization", 18 | "jwt", 19 | "authbee" 20 | ], 21 | "author": "", 22 | "license": "ISC", 23 | "bugs": { 24 | "url": "https://github.com/shakyapeiris/AuthBee/issues" 25 | }, 26 | "homepage": "https://github.com/shakyapeiris/AuthBee#readme", 27 | "devDependencies": { 28 | "eslint": "^8.1.0", 29 | "jest": "^27.3.1", 30 | "prettier": "^2.4.1" 31 | } 32 | } 33 | --------------------------------------------------------------------------------