@forward-software/react-auth 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 ForWarD Software
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.
package/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # React Auth
2
+
3
+ > Simplify your Auth flow when working with React apps
4
+
5
+ [![license](https://img.shields.io/github/license/Forward-Software/react-auth.svg)](https://github.com/Forward-Software/react-auth/blob/main/LICENSE) [![CI](https://github.com/Forward-Software/react-auth/actions/workflows/main.yml/badge.svg)](https://github.com/Forward-Software/react-auth/actions/workflows/main.yml) [![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/Forward-Software/react-auth.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/Forward-Software/react-auth/context:javascript) [![Coverage Status](https://coveralls.io/repos/github/Forward-Software/react-auth/badge.svg?branch=main)](https://coveralls.io/github/Forward-Software/react-auth?branch=main) [![Github Issues](https://img.shields.io/github/issues/Forward-Software/react-auth.svg)](https://github.com/Forward-Software/react-auth/issues)
6
+
7
+ [![npm](https://img.shields.io/npm/v/@forward-software/react-auth)](https://npmjs.com/package/@forward-software/react-auth) [![NPM downloads](https://img.shields.io/npm/dm/@forward-software/react-auth.svg)](https://npmjs.com/package/@forward-software/react-auth)
8
+
9
+ This React package allows you to streamline the integration of user authentication flows in any React app by providing a single unified interface
10
+
11
+ ---
12
+
13
+ ## Install
14
+
15
+ ```sh
16
+ yarn add @forward-software/react-auth
17
+ ```
18
+
19
+ ## Setup
20
+
21
+ ### Define an AuthClient class
22
+
23
+ Create a new `AuthClient` class which extends the `BaseAuthClient` provided by this library and implements the 4 required methods:
24
+
25
+ - **onInit**, called when the AuthClient gets initialized
26
+ - **onLogin**, invoked when the `login` method of the AuthClient gets called
27
+ - **onRefresh**, invoked when the `refresh` method of the AuthClient gets called
28
+ - **onLogout**, invoked when the `logout` method of the AuthClient gets called
29
+
30
+ ```ts
31
+ import { BaseAuthClient } from '@forward-software/react-auth';
32
+
33
+ // The type for your credentials
34
+ type AuthCredentials = {
35
+ username: string;
36
+
37
+ password: string;
38
+ };
39
+
40
+ // The type for your tokens
41
+ type AuthTokens = {
42
+ authToken: string;
43
+
44
+ refreshToken: string;
45
+ };
46
+
47
+ class AuthClient extends BaseAuthClient<AuthTokens, AuthCredentials> {
48
+ protected onInit(): Promise<void> {
49
+ // Implement the initialization logic for your client
50
+ }
51
+
52
+ protected onLogin(credentials?: AuthCredentials): Promise<AuthTokens> {
53
+ // Implement the logic required to exchange the provided credentials for user tokens
54
+ }
55
+
56
+ protected onRefresh(minValidity?: number): Promise<AuthTokens> {
57
+ // Implement the logic required to refresh the current user tokens
58
+ }
59
+
60
+ protected onLogout(): Promise<void> {
61
+ // Implement the logic required to invalidate the current user tokens
62
+ }
63
+ }
64
+ ```
65
+
66
+ ### Instantiate an AuthClient
67
+
68
+ Create an instance of the `AuthClient` class defined
69
+
70
+ ```ts
71
+ const authClient = new AuthClient();
72
+ ```
73
+
74
+ #### AuthClient Props
75
+
76
+ - `isInitialized`, a boolean indicating if the AuthClient has been initialized
77
+ - `isAuthenticated`, a boolean indicating if the login process has been successfull and the user is authenticated
78
+ - `tokens`, the current tokens returned by the `login` or the `refresh` process
79
+
80
+ #### AuthClient Methods
81
+
82
+ ##### Core
83
+
84
+ - `init()`, initialize the AuthClient (**N.B.** this shouldn't be called if using `AuthProvider` - see below)
85
+ - `login(credentials)`, start the login process
86
+ - `refresh()`, refresh the current tokens
87
+ - `logout()`, logout and invalidate the current tokens
88
+
89
+ ##### EventEmitter
90
+
91
+ - `on(eventName, listenerFn)`, subscribe to `eventName` events emitted by the AuthClient
92
+ - `off(eventName, listenerFn)`, unsubscribe from `eventName` events emitted by the AuthClient
93
+
94
+ ##### Observable
95
+
96
+ - `subscribe(() => { })`, subscribe to AuthClient state changes
97
+ - `getSnapshot()`, returns the current state of the AuthClient
98
+
99
+ ### React components
100
+
101
+ Setup React components to interact with the AuthClient using the `createAuth` function exported by this library
102
+
103
+ ```ts
104
+ import { createAuth } from '@forward-software/react-auth';
105
+
106
+ export const { AuthProvider, useAuthClient } = createAuth(authClient);
107
+ ```
108
+
109
+ the `createAuth` function returns:
110
+
111
+ - `AuthProvider`, the context Provider component that should wrap your app and provide access to your AuthClient
112
+ - `useAuthClient`, the hook to retrieve and interact with your AuthClient
113
+
114
+ #### AuthProvider
115
+
116
+ The context Provider component that should wrap your app and provide access to your AuthClient, this component also accepts 2 additional props
117
+
118
+ - `ErrorComponent`, displayed when the AuthClient initialization fails
119
+ - `LoadingComponent`, displayed while the AuthClient is being initialized
120
+
121
+ ## Examples
122
+
123
+ The `examples` folder contains some examples of how you can integrate this library in your React app.
124
+
125
+ ## Credits
126
+
127
+ This library has been inspired by [`react-keycloak`](https://github.com/react-keycloak/react-keycloak) and similar libraries.
128
+
129
+ ## License
130
+
131
+ MIT
132
+
133
+ ---
134
+
135
+ Made with ✨ & ❤️ by [ForWarD Software](https://github.com/Forward-Software) and [contributors](https://github.com/Forward-Software/react-auth/graphs/contributors)
136
+
137
+ If you found this project to be helpful, please consider contacting us to develop your React and React Native projects.
@@ -0,0 +1,20 @@
1
+ import React from 'react';
2
+ import { BaseAuthClient } from './types';
3
+ /**
4
+ * Props that can be passed to AuthProvider
5
+ */
6
+ export declare type AuthProviderProps = {
7
+ /**
8
+ * An optional component to display if AuthClient initialization failed.
9
+ */
10
+ ErrorComponent?: JSX.Element;
11
+ /**
12
+ * An optional component to display while AuthClient instance is being initialized.
13
+ */
14
+ LoadingComponent?: JSX.Element;
15
+ };
16
+ export declare function createAuth<C extends BaseAuthClient>(authClient: C): {
17
+ AuthProvider: React.FC<AuthProviderProps>;
18
+ useAuthClient: () => C;
19
+ };
20
+ export { BaseAuthClient };
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+
2
+ 'use strict'
3
+
4
+ if (process.env.NODE_ENV === 'production') {
5
+ module.exports = require('./react-auth.cjs.production.min.js')
6
+ } else {
7
+ module.exports = require('./react-auth.cjs.development.js')
8
+ }