├── .circleci └── config.yml ├── .gitignore ├── LICENSE ├── README.md ├── example ├── .gitignore ├── index.html ├── index.tsx ├── package.json ├── tsconfig.json └── yarn.lock ├── package.json ├── src ├── AuthContext.tsx ├── AuthProvider.tsx ├── index.tsx └── usePrevious.ts ├── test └── AuthProvider.test.tsx ├── tsconfig.json └── yarn.lock /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | jobs: 4 | install: 5 | docker: 6 | - image: circleci/node:lts 7 | steps: 8 | - checkout 9 | - restore_cache: 10 | keys: 11 | - yarn-v1-dependencies-{{ checksum "yarn.lock" }} 12 | - run: 13 | name: Install Deps 14 | command: yarn install --frozen-lockfile 15 | - save_cache: 16 | key: yarn-v1-dependencies-{{ checksum "yarn.lock" }} 17 | paths: 18 | - node_modules 19 | 20 | test: 21 | docker: 22 | - image: circleci/node:lts 23 | steps: 24 | - checkout 25 | - restore_cache: 26 | keys: 27 | - yarn-v1-dependencies-{{ checksum "yarn.lock" }} 28 | - run: 29 | name: Test 30 | command: CI=true yarn test --coverage 31 | - run: bash <(curl -s https://codecov.io/bash) -t $CODECOV_TOKEN 32 | 33 | lint: 34 | docker: 35 | - image: circleci/node:lts 36 | steps: 37 | - checkout 38 | - restore_cache: 39 | keys: 40 | - yarn-v1-dependencies-{{ checksum "yarn.lock" }} 41 | - run: 42 | name: Lint 43 | command: yarn lint 44 | 45 | workflows: 46 | version: 2 47 | build_and_test: 48 | jobs: 49 | - install 50 | 51 | - test: 52 | requires: 53 | - install 54 | 55 | - lint: 56 | requires: 57 | - install 58 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | .DS_Store 3 | node_modules 4 | .cache 5 | .rts2_cache_cjs 6 | .rts2_cache_esm 7 | .rts2_cache_umd 8 | .rts2_cache_system 9 | dist 10 | coverage 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ryan Castner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Auth Provider 2 | 3 | > Easy to integrate react authentication management through context. 4 | 5 | [![codecov](https://codecov.io/gh/audiolion/react-auth-provider/branch/master/graph/badge.svg)](https://codecov.io/gh/audiolion/react-auth-provider) [![CircleCI](https://circleci.com/gh/audiolion/react-auth-provider/tree/master.svg?style=svg)](https://circleci.com/gh/audiolion/react-auth-provider/tree/master) 6 | 7 | ## Features 8 | 9 | - Dead simple to integrate 10 | - Properly memoized authenticated attribute that can be consumed by your app 11 | - Hooks into `onLogin` and `onLogout` 12 | 13 | ## Install 14 | 15 | ```shell 16 | $ yarn add @ryanar/react-auth-provider 17 | ``` 18 | 19 | ## Usage 20 | 21 | Add `AuthProvider` somewhere towards the top of your tree. You likely want to do some routing on login or logout so it should be below your routing context (e.g. react router's `Router` component). 22 | 23 | ```tsx 24 | // App.tsx 25 | import React from 'react'; 26 | import { useHistory } from 'react-router-dom'; 27 | import { AuthProvider } from '@ryanar/react-auth-provider'; 28 | 29 | function App() { 30 | const history = useHistory(); 31 | 32 | const handleLogin = () => { 33 | history.push('/'); 34 | }; 35 | 36 | const handleLogout = () => { 37 | history.push('/login'); 38 | }; 39 | 40 | return ( 41 | 42 | 43 | 44 | 45 | ); 46 | } 47 | ``` 48 | 49 | Integrate `setAuthenticated` into your login and logout flow, here is an example of a login flow: 50 | 51 | ```tsx 52 | // Login.tsx 53 | import React from 'react'; 54 | import { AuthContext } from '@ryanar/react-auth-provider'; 55 | 56 | function Login(props: RouteComponentProps) { 57 | const { setAuthenticated } = React.useContext(AuthContext); 58 | return ( 59 | 62 | ); 63 | } 64 | ``` 65 | 66 | Optionally configure a route that checks for authenticated, here is an example using react router: 67 | 68 | ```tsx 69 | // AuthRoute.tsx 70 | import React from 'react'; 71 | import { AuthContext } from '@ryanar/react-auth-provider'; 72 | 73 | function AuthRoute(props: RouteComponentProps) { 74 | const { authenticated } = React.useContext(AuthContext); 75 | 76 | if (!authenticated) { 77 | return ; 78 | } 79 | 80 | const { component: Component, ...rest } = props; 81 | 82 | return ; 83 | } 84 | ``` 85 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .cache 3 | dist -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Playground 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /example/index.tsx: -------------------------------------------------------------------------------- 1 | import 'react-app-polyfill/ie11'; 2 | import * as React from 'react'; 3 | import * as ReactDOM from 'react-dom'; 4 | import { AuthContext, AuthProvider, AuthProviderProps } from '../.'; 5 | 6 | function DisplayAuthenticated() { 7 | const { authenticated } = React.useContext(AuthContext); 8 | return

{authenticated ? 'true' : 'false'}

; 9 | } 10 | 11 | function Button() { 12 | const { authenticated, setAuthenticated } = React.useContext(AuthContext); 13 | return ( 14 | 21 | ); 22 | } 23 | 24 | function App({ onLogin, onLogout, defaultAuthenticated }: AuthProviderProps) { 25 | return ( 26 | 31 | 32 | 20 | ); 21 | } 22 | 23 | function App({ onLogin, onLogout, defaultAuthenticated }: AuthProviderProps) { 24 | return ( 25 | 30 | 31 |