Skip to content

Commit

Permalink
fix: #45 switch to oidc-client-ts
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Oct 11, 2021
1 parent aba6a6a commit 5aa9ca3
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 88 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Pipeline](https://github.com/authts/react-oidc-context/workflows/Release/badge.svg)


Lightweight auth library using the [oidc-client](https://github.com/IdentityModel/oidc-client-js) library for React single page applications (SPA).
Lightweight auth library using the [oidc-client-ts](https://github.com/authts/oidc-client-ts) library for React single page applications (SPA).
Support for [hooks](https://reactjs.org/docs/hooks-intro.html) and [higher-order components (HOC)](https://reactjs.org/docs/higher-order-components.html).


Expand All @@ -18,10 +18,10 @@ Support for [hooks](https://reactjs.org/docs/hooks-intro.html) and [higher-order


## Documentation
This library implements an auth context provider by making use of the `oidc-client` library. Its configuration is
This library implements an auth context provider by making use of the `oidc-client-ts` library. Its configuration is
tight coupled to that library.

- [oidc-client](https://github.com/IdentityModel/oidc-client-js/wiki)
- [oidc-client-ts](https://github.com/authts/oidc-client-ts)

The User and UserManager is hold in this context, which is accessible from the React application. Additionally it intercepts
the auth redirects by looking at the query/fragment parameters and acts accordingly. You still need to setup a redirect uri,
Expand Down Expand Up @@ -170,7 +170,7 @@ As **not** a child of `AuthProvider` (e.g. redux slice) when using local storage
containing an access token:
```jsx
// src/slice.js
import { User } from "oidc-client"
import { User } from "oidc-client-ts"

function getUser() {
const oidcStorage = localStorage.getItem(`oidc.user:<your authority>:<your client id>`)
Expand Down
91 changes: 42 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@
"lint": "eslint ./src/**/*.ts"
},
"peerDependencies": {
"react": ">=16",
"oidc-client": "^1.11.5"
},
"dependencies": {
"oidc-client": "^1.11.5"
"react": ">=16.8.0",
"oidc-client-ts": "^2.0.0-beta.2"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.5.0",
Expand All @@ -46,6 +43,7 @@
"@typescript-eslint/parser": "^4.29.0",
"eslint": "^7.32.0",
"eslint-plugin-testing-library": "^4.10.1",
"oidc-client-ts": "^2.0.0-beta.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-test-renderer": "^17.0.2",
Expand Down
19 changes: 11 additions & 8 deletions src/AuthContext.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import React from "react";
import { UserManagerSettings, User, SessionStatus } from "oidc-client";
import {
UserManagerSettings, User, SessionStatus,
SigninPopupArgs, SigninSilentArgs, SigninRedirectArgs,
SignoutRedirectArgs, SignoutPopupArgs, QuerySessionStatusArgs } from "oidc-client-ts";

import { AuthState } from "./AuthState";

export interface AuthContextProps extends AuthState {
/**
* UserManager functions. See [UserManager](https://github.com/IdentityModel/oidc-client-js/wiki#usermanager) for more details.
* UserManager functions. See [UserManager](https://github.com/authts/oidc-client-ts) for more details.
*/
readonly settings: UserManagerSettings;
clearStaleState(): Promise<void>;
removeUser(): Promise<void>;
signinPopup(args?: any): Promise<User>;
signinSilent(args?: any): Promise<User>;
signinRedirect(args?: any): Promise<void>;
signoutRedirect(args?: any): Promise<void>;
signoutPopup(args?: any): Promise<void>;
querySessionStatus(args?: any): Promise<SessionStatus>;
signinPopup(args?: SigninPopupArgs): Promise<User>;
signinSilent(args?: SigninSilentArgs): Promise<User | null>;
signinRedirect(args?: SigninRedirectArgs): Promise<void>;
signoutRedirect(args?: SignoutRedirectArgs): Promise<void>;
signoutPopup(args?: SignoutPopupArgs): Promise<void>;
querySessionStatus(args?: QuerySessionStatusArgs): Promise<SessionStatus | null>;
revokeAccessToken(): Promise<void>;
startSilentRenew(): void;
stopSilentRenew(): void;
Expand Down
9 changes: 5 additions & 4 deletions src/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { UserManager, UserManagerSettings, User } from "oidc-client";
import { UserManager, UserManagerSettings, User } from "oidc-client-ts";
import { SignoutRedirectArgs, SignoutPopupArgs } from "oidc-client-ts";

import { AuthContext } from "./AuthContext";
import { initialAuthState } from "./AuthState";
Expand Down Expand Up @@ -106,7 +107,7 @@ export const AuthProvider = (props: AuthProviderProps): JSX.Element => {
const [state, dispatch] = React.useReducer(reducer, initialAuthState);
const userManagerContext = React.useMemo(
() => ({
settings: userManager?.settings ?? {},
settings: userManager?.settings ?? { ...userManagerProps },
...(Object.fromEntries(
userManagerContextKeys.map((key) => [
key,
Expand Down Expand Up @@ -173,14 +174,14 @@ export const AuthProvider = (props: AuthProviderProps): JSX.Element => {

const signoutRedirect = React.useMemo(
() => userManager
? (args?: any) => userManager.signoutRedirect(args).then(onSignoutRedirect)
? (args?: SignoutRedirectArgs) => userManager.signoutRedirect(args).then(onSignoutRedirect)
: unsupportedEnvironment("signoutRedirect"),
[userManager, onSignoutRedirect]
);

const signoutPopup = React.useMemo(
() => userManager
? (args?: any) => userManager.signoutPopup(args).then(onSignoutPopup)
? (args?: SignoutPopupArgs) => userManager.signoutPopup(args).then(onSignoutPopup)
: unsupportedEnvironment("signoutPopup"),
[userManager, onSignoutPopup]
);
Expand Down
2 changes: 1 addition & 1 deletion src/AuthState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User } from "oidc-client";
import { User } from "oidc-client-ts";

/**
* The auth state which, when combined with the auth methods, make up the return object of the `useAuth` hook.
Expand Down
2 changes: 1 addition & 1 deletion src/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User } from "oidc-client";
import { User } from "oidc-client-ts";

import { AuthState } from "./AuthState";

Expand Down
Loading

0 comments on commit 5aa9ca3

Please sign in to comment.