@tasoskakour/react-use-oauth2 1.0.7

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/README.md ADDED
@@ -0,0 +1,178 @@
1
+ # @tasoskakour/react-use-oauth2
2
+
3
+ ![gh workflow](https://github.com/tasoskakour/react-use-oauth2/actions/workflows/ci-cd.yml/badge.svg) [![npm](https://img.shields.io/npm/v/@tasoskakour/react-use-oauth2.svg?style=svg&logo=npm&label=)](https://www.npmjs.com/package/@tasoskakour/react-use-oauth2)
4
+
5
+ > 💎 A custom React hook that makes OAuth2 authorization simple. Both for **Implicit Grant** and **Authorization Code** flows.
6
+
7
+ ## Features
8
+
9
+ - Usage with both `Implicit` and `Authorization Code` grant flows.
10
+ - Seamlessly **exchanges code for token** via your backend API URL, for authorization code grant flows.
11
+ - Works with **Popup** authorization.
12
+ - Provides data and loading/error states via a hook.
13
+ - **Persists data** to localStorage and automatically syncs auth state between tabs and/or browser windows.
14
+
15
+ ## Install
16
+
17
+ _Requires `react@16.8.0` or higher that includes hooks._
18
+
19
+ ```console
20
+ yarn add @tasoskakour/react-use-oauth2
21
+ ```
22
+
23
+ or
24
+
25
+ ```console
26
+ npm i @tasoskakour/react-use-oauth2
27
+ ```
28
+
29
+ ## Usage example
30
+
31
+ *For authorization code flow:*
32
+
33
+ ```js
34
+ import { OAuth2Popup, useOAuth2 } from "@tasoskakour/react-use-oauth2";
35
+ import { BrowserRouter, Routes, Route } from "react-router-dom";
36
+
37
+ const Home = () => {
38
+ const { data, loading, error, getAuth } = useOAuth2({
39
+ authorizeUrl: "https://example.com/auth",
40
+ clientId: "YOUR_CLIENT_ID",
41
+ redirectUri: `${document.location.origin}/callback`,
42
+ scope: "YOUR_SCOPES",
43
+ responseType: "code",
44
+ exchangeCodeForTokenServerURL: "https://your-backend/token",
45
+ exchangeCodeForTokenMethod: "POST",
46
+ onSuccess: (payload) => console.log("Success", payload),
47
+ onError: (error_) => console.log("Error", error_)
48
+ });
49
+
50
+ const isLoggedIn = Boolean(data?.access_token); // or whatever...
51
+
52
+ if (error) {
53
+ return <div>Error</div>;
54
+ }
55
+
56
+ if (loading) {
57
+ return <div>Loading...</div>;
58
+ }
59
+
60
+ if (isLoggedIn) {
61
+ return <pre>{JSON.stringify(data)}</pre>;
62
+ }
63
+
64
+ return (
65
+ <button style={{ margin: "24px" }} type="button" onClick={() => getAuth()}>
66
+ Login
67
+ </button>
68
+ );
69
+ };
70
+
71
+ const App = () => {
72
+ return (
73
+ <BrowserRouter>
74
+ <Routes>
75
+ <Route element={<OAuthPopup />} path="/callback" />
76
+ <Route element={<Home />} path="/" />
77
+ </Routes>
78
+ </BrowserRouter>
79
+ );
80
+ };
81
+ ```
82
+
83
+ ### What is the purpose of `exchangeCodeForTokenServerURL` for Authorization Code flows?
84
+
85
+ Generally when we're working with authorization code flows, we need to *immediately* **exchange** the retrieved *code* with an actual *access token*, after a successful authorization. Most of the times this is needed for back-end apps, but there are many use cases this is useful for front-end apps as well.
86
+
87
+ In order for the flow to be accomplished, the 3rd party provider we're authorizing against (e.g Google, Facebook etc), will provide an API call (e.g for Google is `https://oauth2.googleapis.com/token`) that we need to hit in order to exchange the code for an access token. However, this call requires the `client_secret` of your 3rd party app as a parameter to work - a secret that you cannot expose to your front-end app.
88
+
89
+ That's why you need to proxy this call to your back-end and `exchangeCodeForTokenServerURL` is the API URL of your back-end route that will take care of this. The request parameters that will get passed along as **query parameters** are `{ code, client_id, grant_type, redirect_uri }`. By default this will be a **POST** request but you can change it with the `exchangeCodeForTokenMethod` property.
90
+
91
+
92
+ You can read more about "Exchanging authorization code for refresh and access tokens" in [Google OAuth2 documentation](https://developers.google.com/identity/protocols/oauth2/web-server#exchange-authorization-code).
93
+
94
+ ### What's the case with Implicit Grant flows?
95
+
96
+ With an implicit grant flow things are much simpler as the 3rd-party provider immediately returns the `access_token` to the callback request so there's no need to make any action after that. Just set `responseType=token` to use this flow.
97
+
98
+ ### Data persistence
99
+
100
+ After a successful authorization, data will get persisted to **localStorage** and the state will automatically sync to all tabs/pages of the browser. The storage key the data will be written to will be: `{responseType}-{authorizeUrl}-{clientId}-{scope}`.
101
+
102
+ If you want to re-trigger the authorization flow just call `getAuth()` function again.
103
+
104
+ ## API
105
+
106
+ - `function useOAuth2(options): {data, loading, error, getAuth}`
107
+
108
+ This is the hook that makes this package to work. `Options` is an object that contains the properties below
109
+
110
+ - **authorizeUrl** (string): The 3rd party authorization URL (e.g https://accounts.google.com/o/oauth2/v2/auth).
111
+ - **clientId** (string): The OAuth2 client id of your application.
112
+ - **redirectUri** (string): Determines where the 3rd party API server redirects the user after the user completes the authorization flow. In our [example](#usage-example) the Popup is rendered on that redirectUri.
113
+ - **scope** (string - _optional_): A list of scopes depending on your application needs.
114
+ - **responseType** (string): Can be either **code** for _code authorization grant_ or **token** for _implicit grant_ .
115
+ - **exchangeCodeForTokenServerURL** (string): This property is only used when using _code authorization grant_ flow (responseType = code). It specifies the API URL of your server that will get called immediately after the user completes the authorization flow. Read more [here](#what-is-the-purpose-of-exchangecodefortokenserverurl-for-authorization-code-flows).
116
+ - **exchangeCodeForTokenMethod** (string - _optional_): Specifies the HTTP method that will be used for the code-for-token exchange to your server. Defaults to **POST**.
117
+ - **onSuccess** (function): Called after a complete successful authorization flow.
118
+ - **onError** (function): Called when an error occurs.
119
+
120
+ **Returns**:
121
+
122
+ - **data** (object): Consists of the retrieved auth data and generally will have the shape of `{access_token, token_type, expires_in}` (check [Typescript](#typescript) usage for providing custom shape).
123
+ - **loading** (boolean): Is set to true while the authorization is taking place.
124
+ - **error** (string): Is set when an error occurs.
125
+ - **getAuth** (function): Call this function to trigger the authorization flow.
126
+
127
+ ---
128
+
129
+ - `function OAuthPopup(props)`
130
+
131
+ This is the component that will be rendered as a window Popup for as long as the authorization is taking place. You need to render this in a place where it does not disrupt the user flow. An ideal place is inside a `Route` component of `react-router-dom` as seen in the [usage example](#usage-example).
132
+
133
+ Props consists of:
134
+
135
+ - **Component** (ReactElement - _optional_): You can optionally set a custom component to be rendered inside the Popup. By default it just displays a "Loading..." message.
136
+
137
+ ### Typescript
138
+
139
+ The `useOAuth2` function identity is:
140
+
141
+ ```
142
+ const useOAuth2: <TData = AuthTokenPayload>(props: Oauth2Props<TData>) => {
143
+ data: State<AuthTokenPayload>;
144
+ loading: boolean;
145
+ error: null;
146
+ getAuth: () => () => void;
147
+ }
148
+ ```
149
+
150
+ That means that generally the data will have the shape of `AuthTokenPayload` which consists of:
151
+
152
+ ```
153
+ token_type: string;
154
+ expires_in: number;
155
+ access_token: string;
156
+ scope: string;
157
+ refresh_token: string;
158
+ ```
159
+
160
+ And that also means that you can set the data type by using the hook like this:
161
+
162
+ ```
163
+ type MyCustomShapeData = {
164
+ ...
165
+ }
166
+
167
+ const {data, ...} = useOAuth2<MyCustomShapeData>({...});
168
+ ```
169
+
170
+ ### Tests
171
+
172
+ You can run tests by calling
173
+
174
+ ```console
175
+ npm test
176
+ ```
177
+
178
+ It will start a react-app (:3000) and back-end server (:3001) and then it will run the tests with jest & puppeteer.