@newskit-render/auth 0.0.0-014af081

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 (45) hide show
  1. package/README.md +157 -0
  2. package/dist/cjs/__tests__/CookieView.d.ts +1 -0
  3. package/dist/cjs/__tests__/CookieView.js +44 -0
  4. package/dist/cjs/__tests__/CookieView.js.map +1 -0
  5. package/dist/cjs/client/CookieView.d.ts +5 -0
  6. package/dist/cjs/client/CookieView.js +17 -0
  7. package/dist/cjs/client/CookieView.js.map +1 -0
  8. package/dist/cjs/client/PageWithAuth.d.ts +6 -0
  9. package/dist/cjs/client/PageWithAuth.js +18 -0
  10. package/dist/cjs/client/PageWithAuth.js.map +1 -0
  11. package/dist/cjs/client/SessionProvider.d.ts +10 -0
  12. package/dist/cjs/client/SessionProvider.js +11 -0
  13. package/dist/cjs/client/SessionProvider.js.map +1 -0
  14. package/dist/cjs/client/index.d.ts +3 -0
  15. package/dist/cjs/client/index.js +20 -0
  16. package/dist/cjs/client/index.js.map +1 -0
  17. package/dist/cjs/index.d.ts +2 -0
  18. package/dist/cjs/index.js +23 -0
  19. package/dist/cjs/index.js.map +1 -0
  20. package/dist/cjs/providers/index.d.ts +7 -0
  21. package/dist/cjs/providers/index.js +15 -0
  22. package/dist/cjs/providers/index.js.map +1 -0
  23. package/dist/esm/__tests__/CookieView.d.ts +1 -0
  24. package/dist/esm/__tests__/CookieView.js +39 -0
  25. package/dist/esm/__tests__/CookieView.js.map +1 -0
  26. package/dist/esm/client/CookieView.d.ts +5 -0
  27. package/dist/esm/client/CookieView.js +10 -0
  28. package/dist/esm/client/CookieView.js.map +1 -0
  29. package/dist/esm/client/PageWithAuth.d.ts +6 -0
  30. package/dist/esm/client/PageWithAuth.js +14 -0
  31. package/dist/esm/client/PageWithAuth.js.map +1 -0
  32. package/dist/esm/client/SessionProvider.d.ts +10 -0
  33. package/dist/esm/client/SessionProvider.js +7 -0
  34. package/dist/esm/client/SessionProvider.js.map +1 -0
  35. package/dist/esm/client/index.d.ts +3 -0
  36. package/dist/esm/client/index.js +4 -0
  37. package/dist/esm/client/index.js.map +1 -0
  38. package/dist/esm/index.d.ts +2 -0
  39. package/dist/esm/index.js +3 -0
  40. package/dist/esm/index.js.map +1 -0
  41. package/dist/esm/providers/index.d.ts +7 -0
  42. package/dist/esm/providers/index.js +10 -0
  43. package/dist/esm/providers/index.js.map +1 -0
  44. package/package.json +69 -0
  45. package/providers.js +2 -0
package/README.md ADDED
@@ -0,0 +1,157 @@
1
+ # @newskit-render/auth
2
+
3
+ A package for the authentication of protected pages in your Next.js application. We are currently supporting Okta as a provider but there are also other options for authentication. You can see example usage with a preview article here: [@newskit-render/core](https://www.npmjs.com/package/@newskit-render/core)
4
+
5
+ ## Getting started
6
+
7
+ Before starting, either locally or on deployed enviroments, you will need the following in your `.env.local` file / CircleCi Config:
8
+
9
+ - **OKTA_CLIENT_ID** - client id for **OKTA** app
10
+ - **OKTA_CLIENT_SECRET** - client secret for **OKTA** app
11
+ - **OKTA_DOMAIN** - domain of **OKTA** app i.e `https://newscorp.okta.com`
12
+ - **NEXTAUTH_URL** - domain of your app i.e on local `http://localhost:3000` or on some environment `https://newskit-render.prod.ceng.newsuk.tech`
13
+ - **NEXTAUTH_SECRET** - A random string is used to hash tokens, sign/encrypt cookies and generate cryptographic keys. You can quickly create a good value on the command line via this openssl command. `openssl rand -base64 32`
14
+
15
+ To get this information you need to raise a ticket with **ServiceDesk** for the attention of **IAM and Collaboration team**. You need to request they set up **OKTA** apps for each of your environments.
16
+
17
+ **IMPORTANT** For each each environment the **OKTA** app's sign-in and sign-out urls need to be modified:
18
+
19
+ local
20
+
21
+ ```
22
+ sign-in - http://localhost:3000/api/auth/callback/okta
23
+ sign-out - http://localhost:300
24
+ ```
25
+
26
+ Other environment
27
+
28
+ ```
29
+ sign-in - {your domain / NEXTAUTH_URL}/api/auth/callback/okta
30
+ sign-out - {your domain / NEXTAUTH_URL}
31
+ ```
32
+
33
+ The **IAM and Collaboration team** team need to set all this up. They also need to add you and anyone else you need as users on the apps. They will then send you the credentials `OKTA_CLIENT_ID, OKTA_CLIENT_SECRET, OKTA_DOMAIN`. With this you can test locally (`.env.local`) and once added to your CircleCi config on environments (Render is set up to pass the credentials so you just need to add them and then rebuild).
34
+
35
+ Any issues contact the newskit-render team for help.
36
+
37
+ ### Add it as a dependency:
38
+
39
+ ```
40
+ npm install @newskit-render/auth
41
+ ```
42
+
43
+ or
44
+
45
+ ```
46
+ yarn add @newskit-render/auth
47
+ ```
48
+
49
+ ### Add the provider
50
+
51
+ In your main `_app.jsx/tsx`:
52
+
53
+ ```
54
+ import { SessionProvider } from '@newskit-render/auth'
55
+
56
+ ```
57
+
58
+ Wrap all the content and pass the pageProps as a prop to the provider:
59
+
60
+ ```
61
+ <SessionProvider pageProps={pageProps}>{Content goes here...}</SessionProvider>
62
+ ```
63
+
64
+ ### Create authentication route
65
+
66
+ Under your `/pages` folder create an `/api` folder and an `/auth` folder inside the latter. Inside the `/auth` folder create a file called `[...nextauth].ts`.
67
+ The path should be as follows: `/pages/api/auth/[...nextauth].ts`
68
+
69
+ Create the route and pass your Okta credentials:
70
+
71
+ ```
72
+ import createAuthRoute from '@newskit-render/auth/providers'
73
+ import { NextApiRequest, NextApiResponse } from 'next'
74
+
75
+ const { OKTA_CLIENT_ID, OKTA_CLIENT_SECRET, OKTA_DOMAIN } = process.env
76
+
77
+ export default (req: NextApiRequest, res: NextApiResponse) =>
78
+ createAuthRoute(req, res, {
79
+ clientId: OKTA_CLIENT_ID,
80
+ clientSecret: OKTA_CLIENT_SECRET,
81
+ issuer: OKTA_DOMAIN,
82
+ })
83
+
84
+ ```
85
+
86
+ ### Wrap your protected page with PageWithAuth component
87
+
88
+ Import the following in your page component:
89
+
90
+ ```
91
+ import { PageWithAuth, getSession, signIn, signOut } from '@newskit-render/auth'
92
+ ```
93
+
94
+ Wrap all of your page component content with PageWithAuth. It will accept `onDenied` and `isLoading` props. You can use these to show content for the user when he is not authenticated or when the content is still loading.
95
+
96
+ ```
97
+ <PageWithAuth onDenied={accessDeniedPage} isLoading={showOnLoading}></PageWithAuth>
98
+ ```
99
+
100
+ You can call the signIn and signOut functions to initiate or terminate the authentication for the user. When using singIn set the provider so you don't have an extra login page signIn('okta').
101
+
102
+ The last step is to get the session using the getSession hook.
103
+ Add the hook under the `getServerSideProps` function and then include the result as a prop inside the props that will be returned.
104
+
105
+ ```
106
+ export async function getServerSideProps(context) {
107
+ const session = await getSession(context)
108
+
109
+ return {
110
+ props: {
111
+ ...some props
112
+ session,
113
+ },
114
+ }
115
+ }
116
+ ```
117
+
118
+ You can add some additional logic here if you want to prevent unnecessary requests or any other logic execution if the user is not authenticated:
119
+
120
+ ```
121
+ export async function getServerSideProps(context) {
122
+ const session = await getSession(context)
123
+
124
+ if (!session) {
125
+ return {
126
+ props: {},
127
+ }
128
+ }
129
+
130
+ const { data } = await apolloClient.query({
131
+ query: GET_DATA,
132
+ })
133
+
134
+ return {
135
+ props: {
136
+ data: data,
137
+ session,
138
+ },
139
+ }
140
+ }
141
+ ```
142
+
143
+ signOut should really be dealt with by your CMS but for testing purposes you will want a sign out button. We have added a CookieView component to the the package that only shows children if you set a cookie with the following:
144
+
145
+ ```
146
+ document.cookie = `nextAuthShowLogOut=true; Path=/`; location.reload();
147
+ ```
148
+
149
+ You can change the cookie name if you want.
150
+
151
+ For the full page example see [preview page](https://github.com/newscorp-ghfb/ncu-newskit-render/blob/master/packages/core/pages/preview/%5BarticleId%5D/version/%5BversionId%5D/index.tsx)
152
+
153
+ ## Preview Pages
154
+
155
+ To see Preview pages uses the following pattern:
156
+
157
+ `{site-domain}/preview/{article-id}/version/{version-id}`
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var react_1 = __importDefault(require("react"));
7
+ var react_dom_1 = require("react-dom");
8
+ var CookieView_1 = require("../client/CookieView");
9
+ describe('CookieView', function () {
10
+ var container;
11
+ beforeEach(function () {
12
+ container = document.createElement('div');
13
+ document.body.appendChild(container);
14
+ });
15
+ afterEach(function () {
16
+ (0, react_dom_1.unmountComponentAtNode)(container);
17
+ container.remove();
18
+ container = { textContent: '' };
19
+ });
20
+ test('Not visible, no cookie set', function () {
21
+ (0, react_dom_1.render)(react_1.default.createElement(CookieView_1.CookieView, null,
22
+ react_1.default.createElement("div", null, "Not visible")), container);
23
+ expect(container.textContent).toBe('');
24
+ });
25
+ test('Not visible, cookie set set to false', function () {
26
+ Object.defineProperty(document, 'cookie', {
27
+ writable: true,
28
+ value: 'nextAuthShowLogOut=false',
29
+ });
30
+ (0, react_dom_1.render)(react_1.default.createElement(CookieView_1.CookieView, null,
31
+ react_1.default.createElement("div", null, "visible")), container);
32
+ expect(container.textContent).toBe('');
33
+ });
34
+ test('Visible, cookie set set to true', function () {
35
+ Object.defineProperty(document, 'cookie', {
36
+ writable: true,
37
+ value: 'nextAuthShowLogOut=true',
38
+ });
39
+ (0, react_dom_1.render)(react_1.default.createElement(CookieView_1.CookieView, null,
40
+ react_1.default.createElement("div", null, "visible")), container);
41
+ expect(container.textContent).toBe('visible');
42
+ });
43
+ });
44
+ //# sourceMappingURL=CookieView.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CookieView.js","sourceRoot":"","sources":["../../../src/__tests__/CookieView.tsx"],"names":[],"mappings":";;;;;AAAA,gDAAyB;AACzB,uCAA0D;AAC1D,mDAAiD;AAEjD,QAAQ,CAAC,YAAY,EAAE;IACrB,IAAI,SAAS,CAAA;IACb,UAAU,CAAC;QACT,SAAS,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAA;QACzC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAA;IACtC,CAAC,CAAC,CAAA;IAEF,SAAS,CAAC;QACR,IAAA,kCAAsB,EAAC,SAAS,CAAC,CAAA;QACjC,SAAS,CAAC,MAAM,EAAE,CAAA;QAClB,SAAS,GAAG,EAAE,WAAW,EAAE,EAAE,EAAE,CAAA;IACjC,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,4BAA4B,EAAE;QACjC,IAAA,kBAAM,EACJ,8BAAC,uBAAU;YACT,yDAAsB,CACX,EACb,SAAS,CACV,CAAA;QACD,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,sCAAsC,EAAE;QAC3C,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,QAAQ,EAAE;YACxC,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE,0BAA0B;SAClC,CAAC,CAAA;QACF,IAAA,kBAAM,EACJ,8BAAC,uBAAU;YACT,qDAAkB,CACP,EACb,SAAS,CACV,CAAA;QACD,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,iCAAiC,EAAE;QACtC,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,QAAQ,EAAE;YACxC,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE,yBAAyB;SACjC,CAAC,CAAA;QACF,IAAA,kBAAM,EACJ,8BAAC,uBAAU;YACT,qDAAkB,CACP,EACb,SAAS,CACV,CAAA;QACD,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;IAC/C,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ export declare const CookieView: ({ cookieName, children, }: {
3
+ cookieName?: string | undefined;
4
+ children: JSX.Element;
5
+ }) => JSX.Element | null;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.CookieView = void 0;
7
+ var cookie_1 = __importDefault(require("cookie"));
8
+ var CookieView = function (_a) {
9
+ var _b = _a.cookieName, cookieName = _b === void 0 ? 'nextAuthShowLogOut' : _b, children = _a.children;
10
+ return typeof document !== 'undefined'
11
+ ? cookie_1.default.parse(document.cookie)[cookieName] === 'true'
12
+ ? children
13
+ : null
14
+ : null;
15
+ };
16
+ exports.CookieView = CookieView;
17
+ //# sourceMappingURL=CookieView.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CookieView.js","sourceRoot":"","sources":["../../../src/client/CookieView.tsx"],"names":[],"mappings":";;;;;;AAAA,kDAA2B;AACpB,IAAM,UAAU,GAAG,UAAC,EAM1B;QALC,kBAAiC,EAAjC,UAAU,mBAAG,oBAAoB,KAAA,EACjC,QAAQ,cAAA;IAKR,OAAA,OAAO,QAAQ,KAAK,WAAW;QAC7B,CAAC,CAAC,gBAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,UAAU,CAAC,KAAK,MAAM;YACpD,CAAC,CAAC,QAAQ;YACV,CAAC,CAAC,IAAI;QACR,CAAC,CAAC,IAAI;AAJR,CAIQ,CAAA;AAXG,QAAA,UAAU,cAWb"}
@@ -0,0 +1,6 @@
1
+ /// <reference types="react" />
2
+ export declare const PageWithAuth: ({ children, isLoading, onDenied, }: {
3
+ children: JSX.Element;
4
+ isLoading: JSX.Element;
5
+ onDenied: JSX.Element;
6
+ }) => JSX.Element;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PageWithAuth = void 0;
4
+ var react_1 = require("next-auth/react");
5
+ var PageWithAuth = function (_a) {
6
+ var children = _a.children, isLoading = _a.isLoading, onDenied = _a.onDenied;
7
+ var _b = (0, react_1.useSession)(), session = _b.data, status = _b.status;
8
+ if (status === 'loading')
9
+ return isLoading;
10
+ if (!session && status === 'unauthenticated') {
11
+ return onDenied;
12
+ }
13
+ else {
14
+ return children;
15
+ }
16
+ };
17
+ exports.PageWithAuth = PageWithAuth;
18
+ //# sourceMappingURL=PageWithAuth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PageWithAuth.js","sourceRoot":"","sources":["../../../src/client/PageWithAuth.tsx"],"names":[],"mappings":";;;AAAA,yCAA4C;AAErC,IAAM,YAAY,GAAG,UAAC,EAQ5B;QAPC,QAAQ,cAAA,EACR,SAAS,eAAA,EACT,QAAQ,cAAA;IAMF,IAAA,KAA4B,IAAA,kBAAU,GAAE,EAAhC,OAAO,UAAA,EAAE,MAAM,YAAiB,CAAA;IAE9C,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAA;IAE1C,IAAI,CAAC,OAAO,IAAI,MAAM,KAAK,iBAAiB,EAAE;QAC5C,OAAO,QAAQ,CAAA;KAChB;SAAM;QACL,OAAO,QAAQ,CAAA;KAChB;AACH,CAAC,CAAA;AAlBY,QAAA,YAAY,gBAkBxB"}
@@ -0,0 +1,10 @@
1
+ /// <reference types="react" />
2
+ import { Session } from 'next-auth';
3
+ export declare const SessionProvider: ({ pageProps, children, }: {
4
+ pageProps: {
5
+ session: Session;
6
+ } & {
7
+ [key: string]: unknown;
8
+ };
9
+ children: JSX.Element;
10
+ }) => JSX.Element;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SessionProvider = void 0;
4
+ var react_1 = require("react");
5
+ var react_2 = require("next-auth/react");
6
+ var SessionProvider = function (_a) {
7
+ var pageProps = _a.pageProps, children = _a.children;
8
+ return (0, react_1.createElement)(react_2.SessionProvider, { session: pageProps.session, children: children }, children);
9
+ };
10
+ exports.SessionProvider = SessionProvider;
11
+ //# sourceMappingURL=SessionProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SessionProvider.js","sourceRoot":"","sources":["../../../src/client/SessionProvider.tsx"],"names":[],"mappings":";;;AAAA,+BAAqC;AAErC,yCAA6D;AAEtD,IAAM,eAAe,GAAG,UAAC,EAM/B;QALC,SAAS,eAAA,EACT,QAAQ,cAAA;IAKR,OAAO,IAAA,qBAAa,EAClB,uBAAQ,EACR,EAAE,OAAO,EAAE,SAAS,CAAC,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAClD,QAAQ,CACT,CAAA;AACH,CAAC,CAAA;AAZY,QAAA,eAAe,mBAY3B"}
@@ -0,0 +1,3 @@
1
+ export * from './PageWithAuth';
2
+ export * from './CookieView';
3
+ export * from './SessionProvider';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./PageWithAuth"), exports);
18
+ __exportStar(require("./CookieView"), exports);
19
+ __exportStar(require("./SessionProvider"), exports);
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/client/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,iDAA8B;AAC9B,+CAA4B;AAC5B,oDAAiC"}
@@ -0,0 +1,2 @@
1
+ export { getSession, signIn, signOut } from 'next-auth/react';
2
+ export * from './client';
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.signOut = exports.signIn = exports.getSession = void 0;
18
+ var react_1 = require("next-auth/react");
19
+ Object.defineProperty(exports, "getSession", { enumerable: true, get: function () { return react_1.getSession; } });
20
+ Object.defineProperty(exports, "signIn", { enumerable: true, get: function () { return react_1.signIn; } });
21
+ Object.defineProperty(exports, "signOut", { enumerable: true, get: function () { return react_1.signOut; } });
22
+ __exportStar(require("./client"), exports);
23
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,yCAA6D;AAApD,mGAAA,UAAU,OAAA;AAAE,+FAAA,MAAM,OAAA;AAAE,gGAAA,OAAO,OAAA;AAEpC,2CAAwB"}
@@ -0,0 +1,7 @@
1
+ import { NextApiRequest, NextApiResponse } from 'next';
2
+ declare const createAuthRoute: (req: NextApiRequest, res: NextApiResponse, providerOptions: {
3
+ clientId: string;
4
+ clientSecret: string;
5
+ issuer: string;
6
+ }) => Promise<void>;
7
+ export default createAuthRoute;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var next_auth_1 = __importDefault(require("next-auth"));
7
+ var okta_1 = __importDefault(require("next-auth/providers/okta"));
8
+ var createAuthRoute = function (req, res, providerOptions) {
9
+ var options = {
10
+ providers: [(0, okta_1.default)(providerOptions)],
11
+ };
12
+ return (0, next_auth_1.default)(req, res, options);
13
+ };
14
+ exports.default = createAuthRoute;
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/providers/index.ts"],"names":[],"mappings":";;;;;AAAA,wDAAgC;AAEhC,kEAAmD;AACnD,IAAM,eAAe,GAAG,UACtB,GAAmB,EACnB,GAAoB,EACpB,eAIC;IAED,IAAM,OAAO,GAAG;QACd,SAAS,EAAE,CAAC,IAAA,cAAY,EAAC,eAAe,CAAC,CAAC;KAC3C,CAAA;IAED,OAAO,IAAA,mBAAQ,EAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,CAAA;AACpC,CAAC,CAAA;AAED,kBAAe,eAAe,CAAA"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,39 @@
1
+ import React from 'react';
2
+ import { render, unmountComponentAtNode } from 'react-dom';
3
+ import { CookieView } from '../client/CookieView';
4
+ describe('CookieView', function () {
5
+ var container;
6
+ beforeEach(function () {
7
+ container = document.createElement('div');
8
+ document.body.appendChild(container);
9
+ });
10
+ afterEach(function () {
11
+ unmountComponentAtNode(container);
12
+ container.remove();
13
+ container = { textContent: '' };
14
+ });
15
+ test('Not visible, no cookie set', function () {
16
+ render(React.createElement(CookieView, null,
17
+ React.createElement("div", null, "Not visible")), container);
18
+ expect(container.textContent).toBe('');
19
+ });
20
+ test('Not visible, cookie set set to false', function () {
21
+ Object.defineProperty(document, 'cookie', {
22
+ writable: true,
23
+ value: 'nextAuthShowLogOut=false',
24
+ });
25
+ render(React.createElement(CookieView, null,
26
+ React.createElement("div", null, "visible")), container);
27
+ expect(container.textContent).toBe('');
28
+ });
29
+ test('Visible, cookie set set to true', function () {
30
+ Object.defineProperty(document, 'cookie', {
31
+ writable: true,
32
+ value: 'nextAuthShowLogOut=true',
33
+ });
34
+ render(React.createElement(CookieView, null,
35
+ React.createElement("div", null, "visible")), container);
36
+ expect(container.textContent).toBe('visible');
37
+ });
38
+ });
39
+ //# sourceMappingURL=CookieView.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CookieView.js","sourceRoot":"","sources":["../../../src/__tests__/CookieView.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,MAAM,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAA;AAC1D,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAEjD,QAAQ,CAAC,YAAY,EAAE;IACrB,IAAI,SAAS,CAAA;IACb,UAAU,CAAC;QACT,SAAS,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAA;QACzC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,CAAA;IACtC,CAAC,CAAC,CAAA;IAEF,SAAS,CAAC;QACR,sBAAsB,CAAC,SAAS,CAAC,CAAA;QACjC,SAAS,CAAC,MAAM,EAAE,CAAA;QAClB,SAAS,GAAG,EAAE,WAAW,EAAE,EAAE,EAAE,CAAA;IACjC,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,4BAA4B,EAAE;QACjC,MAAM,CACJ,oBAAC,UAAU;YACT,+CAAsB,CACX,EACb,SAAS,CACV,CAAA;QACD,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,sCAAsC,EAAE;QAC3C,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,QAAQ,EAAE;YACxC,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE,0BAA0B;SAClC,CAAC,CAAA;QACF,MAAM,CACJ,oBAAC,UAAU;YACT,2CAAkB,CACP,EACb,SAAS,CACV,CAAA;QACD,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IACxC,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,iCAAiC,EAAE;QACtC,MAAM,CAAC,cAAc,CAAC,QAAQ,EAAE,QAAQ,EAAE;YACxC,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE,yBAAyB;SACjC,CAAC,CAAA;QACF,MAAM,CACJ,oBAAC,UAAU;YACT,2CAAkB,CACP,EACb,SAAS,CACV,CAAA;QACD,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;IAC/C,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
@@ -0,0 +1,5 @@
1
+ /// <reference types="react" />
2
+ export declare const CookieView: ({ cookieName, children, }: {
3
+ cookieName?: string | undefined;
4
+ children: JSX.Element;
5
+ }) => JSX.Element | null;
@@ -0,0 +1,10 @@
1
+ import cookie from 'cookie';
2
+ export var CookieView = function (_a) {
3
+ var _b = _a.cookieName, cookieName = _b === void 0 ? 'nextAuthShowLogOut' : _b, children = _a.children;
4
+ return typeof document !== 'undefined'
5
+ ? cookie.parse(document.cookie)[cookieName] === 'true'
6
+ ? children
7
+ : null
8
+ : null;
9
+ };
10
+ //# sourceMappingURL=CookieView.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CookieView.js","sourceRoot":"","sources":["../../../src/client/CookieView.tsx"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAC3B,MAAM,CAAC,IAAM,UAAU,GAAG,UAAC,EAM1B;QALC,kBAAiC,EAAjC,UAAU,mBAAG,oBAAoB,KAAA,EACjC,QAAQ,cAAA;IAKR,OAAA,OAAO,QAAQ,KAAK,WAAW;QAC7B,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,UAAU,CAAC,KAAK,MAAM;YACpD,CAAC,CAAC,QAAQ;YACV,CAAC,CAAC,IAAI;QACR,CAAC,CAAC,IAAI;AAJR,CAIQ,CAAA"}
@@ -0,0 +1,6 @@
1
+ /// <reference types="react" />
2
+ export declare const PageWithAuth: ({ children, isLoading, onDenied, }: {
3
+ children: JSX.Element;
4
+ isLoading: JSX.Element;
5
+ onDenied: JSX.Element;
6
+ }) => JSX.Element;
@@ -0,0 +1,14 @@
1
+ import { useSession } from 'next-auth/react';
2
+ export var PageWithAuth = function (_a) {
3
+ var children = _a.children, isLoading = _a.isLoading, onDenied = _a.onDenied;
4
+ var _b = useSession(), session = _b.data, status = _b.status;
5
+ if (status === 'loading')
6
+ return isLoading;
7
+ if (!session && status === 'unauthenticated') {
8
+ return onDenied;
9
+ }
10
+ else {
11
+ return children;
12
+ }
13
+ };
14
+ //# sourceMappingURL=PageWithAuth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PageWithAuth.js","sourceRoot":"","sources":["../../../src/client/PageWithAuth.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5C,MAAM,CAAC,IAAM,YAAY,GAAG,UAAC,EAQ5B;QAPC,QAAQ,cAAA,EACR,SAAS,eAAA,EACT,QAAQ,cAAA;IAMF,IAAA,KAA4B,UAAU,EAAE,EAAhC,OAAO,UAAA,EAAE,MAAM,YAAiB,CAAA;IAE9C,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAA;IAE1C,IAAI,CAAC,OAAO,IAAI,MAAM,KAAK,iBAAiB,EAAE;QAC5C,OAAO,QAAQ,CAAA;KAChB;SAAM;QACL,OAAO,QAAQ,CAAA;KAChB;AACH,CAAC,CAAA"}
@@ -0,0 +1,10 @@
1
+ /// <reference types="react" />
2
+ import { Session } from 'next-auth';
3
+ export declare const SessionProvider: ({ pageProps, children, }: {
4
+ pageProps: {
5
+ session: Session;
6
+ } & {
7
+ [key: string]: unknown;
8
+ };
9
+ children: JSX.Element;
10
+ }) => JSX.Element;
@@ -0,0 +1,7 @@
1
+ import { createElement } from 'react';
2
+ import { SessionProvider as Provider } from 'next-auth/react';
3
+ export var SessionProvider = function (_a) {
4
+ var pageProps = _a.pageProps, children = _a.children;
5
+ return createElement(Provider, { session: pageProps.session, children: children }, children);
6
+ };
7
+ //# sourceMappingURL=SessionProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SessionProvider.js","sourceRoot":"","sources":["../../../src/client/SessionProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAA;AAErC,OAAO,EAAE,eAAe,IAAI,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAE7D,MAAM,CAAC,IAAM,eAAe,GAAG,UAAC,EAM/B;QALC,SAAS,eAAA,EACT,QAAQ,cAAA;IAKR,OAAO,aAAa,CAClB,QAAQ,EACR,EAAE,OAAO,EAAE,SAAS,CAAC,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAClD,QAAQ,CACT,CAAA;AACH,CAAC,CAAA"}
@@ -0,0 +1,3 @@
1
+ export * from './PageWithAuth';
2
+ export * from './CookieView';
3
+ export * from './SessionProvider';
@@ -0,0 +1,4 @@
1
+ export * from './PageWithAuth';
2
+ export * from './CookieView';
3
+ export * from './SessionProvider';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/client/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA;AAC9B,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA"}
@@ -0,0 +1,2 @@
1
+ export { getSession, signIn, signOut } from 'next-auth/react';
2
+ export * from './client';
@@ -0,0 +1,3 @@
1
+ export { getSession, signIn, signOut } from 'next-auth/react';
2
+ export * from './client';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAE7D,cAAc,UAAU,CAAA"}
@@ -0,0 +1,7 @@
1
+ import { NextApiRequest, NextApiResponse } from 'next';
2
+ declare const createAuthRoute: (req: NextApiRequest, res: NextApiResponse, providerOptions: {
3
+ clientId: string;
4
+ clientSecret: string;
5
+ issuer: string;
6
+ }) => Promise<void>;
7
+ export default createAuthRoute;
@@ -0,0 +1,10 @@
1
+ import NextAuth from 'next-auth';
2
+ import OktaProvider from 'next-auth/providers/okta';
3
+ var createAuthRoute = function (req, res, providerOptions) {
4
+ var options = {
5
+ providers: [OktaProvider(providerOptions)],
6
+ };
7
+ return NextAuth(req, res, options);
8
+ };
9
+ export default createAuthRoute;
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/providers/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,WAAW,CAAA;AAEhC,OAAO,YAAY,MAAM,0BAA0B,CAAA;AACnD,IAAM,eAAe,GAAG,UACtB,GAAmB,EACnB,GAAoB,EACpB,eAIC;IAED,IAAM,OAAO,GAAG;QACd,SAAS,EAAE,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;KAC3C,CAAA;IAED,OAAO,QAAQ,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,CAAA;AACpC,CAAC,CAAA;AAED,eAAe,eAAe,CAAA"}
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@newskit-render/auth",
3
+ "version": "0.0.0-014af081",
4
+ "description": "Newskit Render",
5
+ "author": "",
6
+ "license": "UNLICENSED",
7
+ "keywords": [],
8
+ "main": "dist/cjs/index.js",
9
+ "module": "dist/esm/index.js",
10
+ "types": "dist/esm/index.d.ts",
11
+ "engines": {
12
+ "node": ">=16.13"
13
+ },
14
+ "scripts": {
15
+ "build": "rm -rf dist && tsc -b tsconfig.build.json && tsc -p tsconfig.build.json --module CommonJS --outDir './dist/cjs'",
16
+ "build:watch": "tsc -b tsconfig.build.json -w",
17
+ "test:unit": "jest --coverage --verbose",
18
+ "test:unit:watch": "jest --watch",
19
+ "test:unit:ci": "JEST_JUNIT_OUTPUT_NAME=create-render-app.xml jest --ci --coverage --reporters=default --reporters=jest-junit",
20
+ "lint": "eslint --ext .js,.jsx,.ts,.tsx . --color",
21
+ "lint:fix": "eslint --ext .js,.jsx,.ts,.tsx . --color --fix && prettier --write './**/*.{js,jsx,ts,tsx,json}'",
22
+ "clean": "rm -rf node_modules & rm -f package-lock.json & rm -f yarn.lock"
23
+ },
24
+ "dependencies": {
25
+ "cookie": "0.5.0",
26
+ "next-auth": "4.22.3"
27
+ },
28
+ "devDependencies": {
29
+ "@babel/polyfill": "7.12.1",
30
+ "@babel/register": "7.18.9",
31
+ "@types/cookie": "^0.5.1",
32
+ "@types/jest": "29.5.5",
33
+ "@types/react": "18.0.26",
34
+ "@types/react-dom": "18.0.10",
35
+ "@typescript-eslint/eslint-plugin": "5.51.0",
36
+ "@typescript-eslint/parser": "5.51.0",
37
+ "eslint": "8.34.0",
38
+ "eslint-config-prettier": "8.6.0",
39
+ "eslint-plugin-prettier": "4.2.1",
40
+ "eslint-plugin-react": "7.31.11",
41
+ "jest": "29.7.0",
42
+ "jest-environment-jsdom": "29.7.0",
43
+ "jest-junit": "15.0.0",
44
+ "next": "13.4.9",
45
+ "preact": "10.6.4",
46
+ "prettier": "2.8.1",
47
+ "react": "18.2.0",
48
+ "react-dom": "18.2.0",
49
+ "ts-jest": "29.1.1",
50
+ "typescript": "4.9.4"
51
+ },
52
+ "peerDependencies": {
53
+ "next": ">= 12.1.0 <= 13.4.9",
54
+ "react": ">= 17.0.2 <= 18.2.0",
55
+ "react-dom": ">= 17.0.2 <= 18.2.0"
56
+ },
57
+ "resolutions": {
58
+ "@types/react": "18.0.26",
59
+ "@types/react-dom": "18.0.10"
60
+ },
61
+ "files": [
62
+ "dist",
63
+ "providers.js",
64
+ "index.js"
65
+ ],
66
+ "publishConfig": {
67
+ "access": "public"
68
+ }
69
+ }
package/providers.js ADDED
@@ -0,0 +1,2 @@
1
+ // file is related to server side
2
+ module.exports = require('./dist/cjs/providers').default