oidc-spa 0.1.0 → 0.1.1

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.
Files changed (2) hide show
  1. package/README.md +39 -21
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <p align="center">
2
- <img src="https://user-images.githubusercontent.com/6702424/80216211-00ef5280-863e-11ea-81de-59f3a3d4b8e4.png">
2
+ <img src="https://github.com/garronej/oidc-spa/assets/6702424/6adde1f7-b7b6-4b1a-b48f-bd02095b99ea">
3
3
  </p>
4
4
  <p align="center">
5
5
  <i>Openidconnect client for Single Page Applications</i>
@@ -24,22 +24,19 @@
24
24
  <a href="https://github.com/garronej/oidc-spa">Documentation</a>
25
25
  </p>
26
26
 
27
- An OIDC client for Single Page Applications that comes with an optional adapter for React.
28
- Very minimal API surface, you don't need to know the in and out of oidc or OAuth to use this.
27
+ An OIDC client designed for Single Page Applications, typically [Vite](https://vitejs.dev/) projects.
28
+ With a streamlined API, you can easily integrate OIDC without needing to understand every detail of the protocol.
29
29
 
30
- Why not oidc-client-ts ?
30
+ ### Comparison with Existing Libraries
31
31
 
32
- - `oidc-client-ts` more a toolkit than a ready to use adapter. This lib use it internally but abstract away most of it's complexity.
33
- - It's used under the hood by this lib but it's hard to setup directly in a SPA setup especially the silent SSO.
34
- - It's more resilient do misconfigured server.
35
- - It restrict what params can be passed on the url when redirecting to the login page.
32
+ #### [oidc-client-ts](https://github.com/authts/oidc-client-ts)
36
33
 
37
- Why not react-oidc-context?
34
+ While `oidc-client-ts` serves as a comprehensive toolkit, our library aims to provide a simplified, ready-to-use adapter. We utilize `oidc-client-ts` internally but abstract away most of its intricacies.
38
35
 
39
- - There's no overlap between OIDC and React. Not everything should be though as a React problem. Oidc and React are
40
- completely different problem space they have no business being intertwined.
41
- This library provide an optional React adapter for convenience in the spirit of being truly ready to use but don't
42
- get mistaken, it's trivial you could as well do without it.
36
+ #### [react-oidc-context](https://github.com/authts/react-oidc-context)
37
+
38
+ Our library takes a modular approach to OIDC and React, treating them as separate concerns that don't necessarily have to be intertwined.
39
+ We offer an optional React adapter for added convenience, but it's not a requirement to use it and [it's really trivial anyway](https://github.com/garronej/oidc-spa/blob/main/src/react.tsx).
43
40
 
44
41
  # Install / Import
45
42
 
@@ -59,9 +56,7 @@ Create a `silent-sso.html` file and put it in your public directory.
59
56
  </html>
60
57
  ```
61
58
 
62
- # Usage
63
-
64
- ## Isolated from the UI library
59
+ ## Basic usage (without involving React)
65
60
 
66
61
  ```ts
67
62
  import { createOidc, decodeJwt } from "oidc-spa";
@@ -91,12 +86,14 @@ import { createOidc, decodeJwt } from "oidc-spa";
91
86
  idToken
92
87
  } = oidc.getTokens();
93
88
 
94
- const { sub, preferred_username } = decodeJwt<{
89
+ const user = decodeJwt<{
95
90
  // Use https://jwt.io/ to tell what's in your idToken
96
91
  sub: string;
97
92
  preferred_username: string;
98
93
  }>(idToken);
99
94
 
95
+ console.log(`Hello ${user.preferred_username}`);
96
+
100
97
  // To call when the user click on logout.
101
98
  oidc.logout();
102
99
  }
@@ -139,17 +136,38 @@ function App() {
139
136
  );
140
137
  }
141
138
 
142
- const { preferred_username } = decodeJwt<{
143
- preferred_username: string;
144
- }>(oidc.getTokens().idToken);
139
+ const { user } = useUser();
145
140
 
146
141
  return (
147
142
  <>
148
- <h1>Hello {preferred_username}</h1>
143
+ <h1>Hello {user.preferred_username}</h1>
149
144
  <button onClick={() => oidc.logout()}>Log out</button>
150
145
  </>
151
146
  );
152
147
  }
148
+
149
+ // Convenience hook to get the parsed idToken
150
+ // To call only when the user is logged in
151
+ function useUser() {
152
+ const { oidc } = useOidc();
153
+
154
+ if (!oidc.isUserLoggedIn) {
155
+ throw new Error("This hook should be used only on authenticated route");
156
+ }
157
+
158
+ const { idToken } = oidc.getTokens();
159
+
160
+ const user = useMemo(
161
+ () =>
162
+ decodeJwt<{
163
+ sub: string;
164
+ preferred_username: string;
165
+ }>(idToken),
166
+ idToken
167
+ );
168
+
169
+ return { user };
170
+ }
153
171
  ```
154
172
 
155
173
  # Setup example
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oidc-spa",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Openidconnect client for Single Page Applications",
5
5
  "repository": {
6
6
  "type": "git",