Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new API for react-router 6 #274

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e821364
wip
denysoblohin-okta Feb 14, 2024
8e64efd
use Secure for reach sample
denysoblohin-okta Feb 14, 2024
71bdf3e
refactoring
denysoblohin-okta Feb 15, 2024
3ef0c8a
5 hash
denysoblohin-okta Feb 15, 2024
58a72b9
utils
denysoblohin-okta Feb 15, 2024
6e54e63
react-router-5
denysoblohin-okta Feb 16, 2024
4b8a1d1
.
denysoblohin-okta Feb 16, 2024
c317f49
rr 5
denysoblohin-okta Feb 16, 2024
c720629
rr 6
denysoblohin-okta Feb 16, 2024
8914d49
rr 6 hash
denysoblohin-okta Feb 19, 2024
a308d9d
nit
denysoblohin-okta Feb 19, 2024
6960502
upd other test apps
denysoblohin-okta Feb 19, 2024
0633a46
add sample rr6-data
denysoblohin-okta Feb 19, 2024
37ace82
.
denysoblohin-okta Feb 19, 2024
572cd1f
test fixes
denysoblohin-okta Feb 19, 2024
32e0102
.
denysoblohin-okta Feb 21, 2024
f39e794
fix
denysoblohin-okta Feb 21, 2024
b50a031
nits
denysoblohin-okta Feb 21, 2024
7e886a1
.
denysoblohin-okta Feb 21, 2024
952a2b7
.
denysoblohin-okta Feb 21, 2024
b80dd17
.
denysoblohin-okta Feb 21, 2024
ae234ca
.
denysoblohin-okta Feb 21, 2024
86ddb97
chlog
denysoblohin-okta Feb 21, 2024
978ba66
optional dep
denysoblohin-okta Feb 21, 2024
ccfd34b
added tests
denysoblohin-okta Feb 22, 2024
4093629
nit
denysoblohin-okta Feb 22, 2024
7ec4bcc
fix - revert Can't handle redirect
denysoblohin-okta Feb 23, 2024
f6da2cb
rename useHashPathForLoginCalback -> usingFragmentForLoginCalback
denysoblohin-okta Feb 23, 2024
c486651
SecureLayout
denysoblohin-okta Feb 23, 2024
4034d2c
rename: Secure -> AuthRequired
denysoblohin-okta Feb 23, 2024
2b5d981
Removed withLoginCallback
denysoblohin-okta Feb 23, 2024
e6bb144
getComponents is not a hook
denysoblohin-okta Feb 23, 2024
3d27da2
fix getComponents
denysoblohin-okta Feb 23, 2024
fa8779b
remove test/jest/package.json
denysoblohin-okta Feb 23, 2024
cadc88d
remove useLoginCallback
denysoblohin-okta Feb 26, 2024
d811d3a
options -> props
denysoblohin-okta Feb 26, 2024
7dc3b97
remove requiresAuth. don't use hook in SecureRoute
denysoblohin-okta Feb 26, 2024
15c7c5b
remove useLoginCallbackCheck
denysoblohin-okta Feb 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# 7.0.0

### Breaking Changes

- [#274](https://github.com/okta/okta-react/pull/274)
- `SecureRoute` should be imported from `@okta/okta-react/react-router-5`
- `react-router-dom` is optional dependency

### Features

- [#274](https://github.com/okta/okta-react/pull/274) Adds new API for integration with `react-router 6.x`
- Adds new `<Secure>` container
- `<LoginCallback>` acts like a container, can render children on a page that is not a login callback
- Adds new hooks: `useAuthRequired`, `useLoginCallback`
- Adds new HOCs: `withAuthRequired`, `withLoginCallback`
- Adds new utils: `getRelativeUri`, `waitForAuthenticated`
- `<Security>` accepts props `errorComponent` and `loadingElement`
- `<Security>` checks the login callback to be called on the login redirect page
- Updated routing samples. Adds new sample `react-router-dom-v6-data` for new routers with Data APIs

# 6.8.0

### Bug Fixes
Expand Down
592 changes: 504 additions & 88 deletions README.md

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ delete packageJSON.scripts; // remove all scripts
delete packageJSON.jest; // remove jest section
delete packageJSON['jest-junit']; // remove jest-junit section
delete packageJSON.workspaces; // remove yarn workspace section
delete packageJSON.devDependencies;

// Remove "build/" from the entrypoint paths.
['main', 'module', 'types'].forEach(function(key) {
if (packageJSON[key]) {
packageJSON[key] = packageJSON[key].replace(`${NPM_DIR}/`, '');
}
});
['.', './react-router-5'].forEach(function(name) {
['types', 'import', 'require', 'default'].forEach(function(key) {
if (packageJSON['exports'][name][key]) {
packageJSON['exports'][name][key] = packageJSON['exports'][name][key].replace(`${NPM_DIR}/`, '');
}
});
});

fs.writeFileSync(`./${NPM_DIR}/package.json`, JSON.stringify(packageJSON, null, 4));

Expand Down
4 changes: 2 additions & 2 deletions generator/_templates/samples/config/new/package.json.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ force: true
"preview": "vite preview"
},
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
<% if (reactRouterDomVersion !== 'false') { -%>
"react-router-dom": "<%= reactRouterDomVersion %>",
<% } -%>
Expand Down
18 changes: 15 additions & 3 deletions generator/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = [
useSiw: false,
useSemanticUi: false,
usePolyfill: false,
reactRouterDomVersion: '6.2.1',
reactRouterDomVersion: '6.22.0',
reachRouterVersion: false,
specs: [
'router-sample'
Expand All @@ -73,7 +73,19 @@ module.exports = [
useSiw: false,
useSemanticUi: false,
usePolyfill: false,
reactRouterDomVersion: '6.2.1',
reactRouterDomVersion: '6.22.0',
reachRouterVersion: false,
specs: [
'router-sample'
]
},
{
name: '@okta/samples.react.react-router-dom-v6-data',
nested: 'routing/',
useSiw: false,
useSemanticUi: false,
usePolyfill: false,
reactRouterDomVersion: '6.22.0',
reachRouterVersion: false,
specs: [
'router-sample'
Expand Down Expand Up @@ -109,7 +121,7 @@ module.exports = [
useSiw: false,
useSemanticUi: false,
usePolyfill: false,
reactRouterDomVersion: '5.3.0',
reactRouterDomVersion: false,
reachRouterVersion: "^1.3.4",
specs: [
'router-sample'
Expand Down
41 changes: 0 additions & 41 deletions jest.config.js

This file was deleted.

31 changes: 24 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@
"start": "yarn workspace @okta/test.app.test-harness-app start",
"test": "yarn lint && yarn test:unit && yarn test:e2e",
"test:e2e": "yarn workspace @okta/test.e2e test",
"test:unit": "jest",
"test:unit": "yarn workspace @okta/test.unit test",
"bundle": "rollup -c",
"dev": "yarn bundle --watch",
"generate": "yarn --cwd generator install && yarn --cwd generator generate"
},
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/bundles/types/index.d.ts",
"import": "./dist/bundles/okta-react.esm.js",
"require": "./dist/bundles/okta-react.cjs.js",
"default": "./dist/bundles/okta-react.umd.js"
},
"./react-router-5": {
"types": "./dist/bundles/types/react-router-5.d.ts",
"import": "./dist/bundles/okta-react-router-5.esm.js",
"require": "./dist/bundles/okta-react-router-5.cjs.js"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we export a react-router-6 bundle as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could export a react-router-6 bundle with one component called like SecureOutlet that just renders <AuthRequired><Outlet /></AuthRequired>
Probably we should not name it SecureRoute as in RR5, because it's not a route.

},
"repository": {
"type": "git",
"url": "git+https://github.com/okta/okta-react.git"
Expand Down Expand Up @@ -52,7 +66,9 @@
"peerDependencies": {
"@okta/okta-auth-js": "^5.3.1 || ^6.0.0 || ^7.0.0",
"react": ">=16.8.0",
"react-dom": ">=16.8.0",
"react-dom": ">=16.8.0"
},
"optionalDependencies": {
"react-router-dom": ">=5.1.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think react-router-dom is actually an "optional peer dependency" (not an optional dependency)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a minimum version of react-router-dom 6.x we should specify here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to decouple okta-react from react-router.
See my comment: #274 (comment)
What if developer wants to use @remix-run/react router, or Next.js router, reach-router?
Then he would not want a react-router to be installed in node_modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left react-router-dom in optionalDependencies just for legacy support of react-router 5

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, but a optional dependencies and a peer dependencies and a optional peer dependencies are not the same thing.

Optional dependency: A dependency that is may or may not be included (optional) to use a package. In the SIW we used to use a async library called fibers to improved the compile time of sass (https://github.com/okta/okta-signin-widget/pull/2863/files)

Peer dependency: A dependency which the application and library need a common version/instance of. For example okta-react need a common instance of React (and authjs). The application consuming okta-react needs React and okta-react needs React AND it needs to be the same "instance" of React

Optional Peer Dependency: Same as a peer dependency, however including the peer dependency is optional

Optional Peer Dependencies are defined as so:

"peerDependencies": {
    "@okta/okta-auth-js": "^7.x"
  },
  "peerDependenciesMeta": {
    "@okta/okta-auth-js": {
      "optional": true
    }
  },

},
"devDependencies": {
Expand All @@ -71,7 +87,6 @@
"@types/jest": "^29.5.4",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/react-router-dom": "^5.1.6",
"@typescript-eslint/eslint-plugin": "^4.8.1",
"@typescript-eslint/parser": "^4.8.1",
"axios": "^0.27.2",
Expand All @@ -87,7 +102,7 @@
"eslint-plugin-jsx-a11y": "^6.0.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-react": "^7.3.0",
"eslint-plugin-react-hooks": "^2.5.0",
"eslint-plugin-react-hooks": "^4.6.0",
"globby": "^11.0.1",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
Expand All @@ -98,7 +113,6 @@
"prop-types": "^15.5.10",
"react": "^16.9.0",
"react-dom": "^16.9.0",
"react-router-dom": "5.2.0",
"rimraf": "^2.6.2",
"rollup": "^2.33.1",
"rollup-plugin-cleanup": "^3.2.1",
Expand All @@ -119,7 +133,8 @@
"samples/**/*",
"samples/routing/**/*",
"test/apps/*",
"test/e2e"
"test/e2e",
"test/jest"
],
"nohoist": [
"**/@wdio/**",
Expand All @@ -132,7 +147,9 @@
"**/@types/react/**",
"**/@types/react-dom",
"**/@types/react-dom/**",
"**/@types/react-router-dom"
"**/@types/react-router-dom",
"**/@reach/router/",
"**/@reach/router/**"
]
}
}
38 changes: 37 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const makeExternalPredicate = () => {
const externalArr = [
...Object.keys(pkg.peerDependencies || {}),
...Object.keys(pkg.dependencies || {}),
// for SecureRoute:
...Object.keys(pkg.optionalDependencies || {}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed (see package.json comment)

'@okta/okta-react',
];

if (externalArr.length === 0) {
Expand Down Expand Up @@ -72,7 +75,8 @@ export default [
globals: {
'react': 'React',
'react-router-dom': 'ReactRouterDOM',
'@okta/okta-auth-js': 'OktaAuth'
'@okta/okta-auth-js': 'OktaAuth',
'compare-versions': 'compareVersions',
}
}
},
Expand Down Expand Up @@ -107,5 +111,37 @@ export default [
sourcemap: true
}
]
},
{
input: 'src/react-router-5.ts',
external,
plugins: [
...commonPlugins,
babel({
babelHelpers: 'runtime',
presets: [
'@babel/preset-env',
'@babel/preset-react'
],
plugins: [
'@babel/plugin-transform-runtime'
],
extensions
}),
],
output: [
{
format: 'cjs',
file: 'dist/bundles/okta-react-router-5.cjs.js',
exports: 'named',
sourcemap: true
},
{
format: 'esm',
file: 'dist/bundles/okta-react-router-5.esm.js',
exports: 'named',
sourcemap: true
}
]
}
];
3 changes: 2 additions & 1 deletion samples/custom-login/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import React from 'react';
import { Route, useHistory, Switch } from 'react-router-dom';
import { OktaAuth, toRelativeUrl } from '@okta/okta-auth-js';
import { Security, SecureRoute, LoginCallback } from '@okta/okta-react';
import { Security, LoginCallback } from '@okta/okta-react';
import { SecureRoute } from '@okta/okta-react/react-router-5';
import { Container } from 'semantic-ui-react';
import config from './config';
import Home from './Home';
Expand Down
3 changes: 2 additions & 1 deletion samples/doc-direct-auth/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import React from 'react';
import { Route, useHistory } from 'react-router-dom';
import { Security, SecureRoute, LoginCallback } from '@okta/okta-react';
import { Security, LoginCallback } from '@okta/okta-react';
import { SecureRoute } from '@okta/okta-react/react-router-5';
import { OktaAuth, toRelativeUrl } from '@okta/okta-auth-js';
import Home from './Home';
import Login from './Login';
Expand Down
3 changes: 2 additions & 1 deletion samples/doc-embedded-widget/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import React from 'react';
import { Route, useHistory } from 'react-router-dom';
import { Security, SecureRoute, LoginCallback } from '@okta/okta-react';
import { Security, LoginCallback } from '@okta/okta-react';
import { SecureRoute } from '@okta/okta-react/react-router-5';
import { OktaAuth, toRelativeUrl } from '@okta/okta-auth-js';
import Home from './Home';
import Login from './Login';
Expand Down
3 changes: 2 additions & 1 deletion samples/okta-hosted-login/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import React from 'react';
import { Route, useHistory, Switch } from 'react-router-dom';
import { OktaAuth, toRelativeUrl } from '@okta/okta-auth-js';
import { Security, SecureRoute, LoginCallback } from '@okta/okta-react';
import { Security, LoginCallback } from '@okta/okta-react';
import { SecureRoute } from '@okta/okta-react/react-router-5';
import { Container } from 'semantic-ui-react';
import config from './config';
import Home from './Home';
Expand Down
5 changes: 2 additions & 3 deletions samples/routing/reach-router/package.json

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

13 changes: 7 additions & 6 deletions samples/routing/reach-router/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@
import React from 'react';

import { navigate } from '@reach/router';
import { Security } from '@okta/okta-react';
import { OktaAuth, toRelativeUrl } from '@okta/okta-auth-js';
import { Security, getRelativeUri } from '@okta/okta-react';
import { OktaAuth } from '@okta/okta-auth-js';
import config from './config';

import Footer from './components/Footer';
import Nav from './components/Nav';
import Routes from './components/Routes';
import Loading from './components/Loading';

const oktaAuth = new OktaAuth(config.oidc);

function App() {
const restoreOriginalUri = (_oktaAuth: any, originalUri: string) => {
navigate(toRelativeUrl(originalUri || '/', window.location.origin), { replace: true });
};
const restoreOriginalUri = React.useCallback((_oktaAuth: any, originalUri: string) => {
navigate(getRelativeUri(originalUri), { replace: true });
}, [navigate]);

return (
<Security oktaAuth={oktaAuth} restoreOriginalUri={restoreOriginalUri}>
<Security oktaAuth={oktaAuth} restoreOriginalUri={restoreOriginalUri} loadingElement={<Loading />}>
<div className="App">
<header className="App-header">
<Nav />
Expand Down
14 changes: 5 additions & 9 deletions samples/routing/reach-router/src/components/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@
*/

import React from 'react';
import { Router, RouteComponentProps } from '@reach/router';
import { LoginCallback } from '@okta/okta-react';
import { SecureRoutes } from './SecureRoutes';
import { Router } from '@reach/router';
import { LoginCallback, Secure } from '@okta/okta-react';

import Home from '../pages/Home';
import Protected from '../pages/Protected';
import Loading from './Loading';

const OktaLoginCallback: React.FC<RouteComponentProps> = () => (<LoginCallback loadingElement={<Loading />} />);

const AppRoutes = () => {
return (
<Router>
<Home path='/' />
<OktaLoginCallback path='login/callback' />
<SecureRoutes path='protected'>
<LoginCallback path='login/callback' />
<Secure path='protected'>
<Protected path='/' />
</SecureRoutes>
</Secure>
</Router>
);
};
Expand Down
Loading