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.
- package/README.md +39 -21
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="https://
|
|
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
|
|
28
|
-
|
|
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
|
-
|
|
30
|
+
### Comparison with Existing Libraries
|
|
31
31
|
|
|
32
|
-
-
|
|
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
|
-
|
|
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
|
-
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
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
|
|
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 {
|
|
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
|