@opencampus/ocid-connect-js 1.2.2 → 1.2.5

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 CHANGED
@@ -104,15 +104,21 @@ export default function CustomLoadingComponent ()
104
104
  />
105
105
  ```
106
106
 
107
- Use useOCAuth hook to read credentials info
107
+ 1. useOCAuth is a hook that provides credentials (authentication information)
108
+ 2. Before using any information from this hook, we need to check isInitialized flag
109
+ 3. This is because there's a setup/initialization period where the SDK (software development kit) is getting ready
110
+
111
+ Here's an example of how it would work:
108
112
 
109
113
  ```js
110
114
  import { useOCAuth } from '@opencampus/ocid-connect-js';
111
115
 
112
116
  const UserTokenPage = (props) => {
113
- const { authState, ocAuth, OCId, ethAddress } = useOCAuth();
117
+ const { isInitialized, authState, ocAuth, OCId, ethAddress } = useOCAuth();
114
118
 
115
- if (authState.error !== undefined) {
119
+ if (!isInitialized) {
120
+ return <div>Loading...</div>;
121
+ } else if (authState.error !== undefined) {
116
122
  return <div>Error: {authState.error.message}</div>;
117
123
  } else {
118
124
  return (
@@ -283,10 +289,6 @@ import { useOCAuth } from '@opencampus/ocid-connect-js';
283
289
  export default function Home() {
284
290
  const { authState, ocAuth } = useOCAuth();
285
291
 
286
- useEffect(() => {
287
- console.log(authState);
288
- }, [authState]); // Now it will log whenever authState changes
289
-
290
292
  if (authState.error) {
291
293
  return <div>Error: {authState.error.message}</div>;
292
294
  }
@@ -358,6 +358,7 @@ var LoginCallBack = function LoginCallBack(_ref) {
358
358
  customErrorComponent = _ref.customErrorComponent,
359
359
  customLoadingComponent = _ref.customLoadingComponent;
360
360
  var _useOCAuth = Object(_OCContext__WEBPACK_IMPORTED_MODULE_1__["useOCAuth"])(),
361
+ isInitialized = _useOCAuth.isInitialized,
361
362
  ocAuth = _useOCAuth.ocAuth,
362
363
  authState = _useOCAuth.authState,
363
364
  setAuthError = _useOCAuth.setAuthError;
@@ -399,7 +400,7 @@ var LoginCallBack = function LoginCallBack(_ref) {
399
400
  handleLogin();
400
401
  }
401
402
  }, [ocAuth]);
402
- if (authState.error !== undefined && !errorCallback) {
403
+ if (isInitialized && authState.error !== undefined && !errorCallback) {
403
404
  return customErrorComponent ? customErrorComponent : /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("div", null, "Error Logging in: ", authState.error.message);
404
405
  } else {
405
406
  return customLoadingComponent ? customLoadingComponent : /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("div", null, /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("h3", null, "Loading ......"));
@@ -439,7 +440,7 @@ var OCConnect = function OCConnect(_ref) {
439
440
  var children = _ref.children,
440
441
  opts = _ref.opts,
441
442
  sandboxMode = _ref.sandboxMode;
442
- var _useState = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])({}),
443
+ var _useState = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])(),
443
444
  _useState2 = _slicedToArray(_useState, 2),
444
445
  ocAuth = _useState2[0],
445
446
  setOcAuth = _useState2[1];
@@ -451,16 +452,18 @@ var OCConnect = function OCConnect(_ref) {
451
452
  _useState6 = _slicedToArray(_useState5, 2),
452
453
  ethAddress = _useState6[0],
453
454
  setEthAddress = _useState6[1];
454
- var _useState7 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])({
455
- isAuthenticated: false
456
- }),
455
+ var _useState7 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])(),
457
456
  _useState8 = _slicedToArray(_useState7, 2),
458
457
  authState = _useState8[0],
459
458
  setAuthState = _useState8[1];
460
- var _useState9 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])(null),
459
+ var _useState9 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])(false),
461
460
  _useState10 = _slicedToArray(_useState9, 2),
462
- authError = _useState10[0],
463
- setAuthError = _useState10[1];
461
+ isInitialized = _useState10[0],
462
+ setIsInitialized = _useState10[1];
463
+ var _useState11 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])(null),
464
+ _useState12 = _slicedToArray(_useState11, 2),
465
+ authError = _useState12[0],
466
+ setAuthError = _useState12[1];
464
467
  var updateAuthState = function updateAuthState(authState) {
465
468
  setAuthState(authState);
466
469
  setOCId(authState.OCId);
@@ -472,9 +475,10 @@ var OCConnect = function OCConnect(_ref) {
472
475
  var authSdk = sandboxMode ? new _sdk_auth__WEBPACK_IMPORTED_MODULE_2__["OCAuthSandbox"](opts) : new _sdk_auth__WEBPACK_IMPORTED_MODULE_2__["OCAuthLive"](opts);
473
476
  updateAuthState(authSdk.getAuthState());
474
477
  setOcAuth(authSdk);
478
+ setIsInitialized(true);
475
479
  }, []);
476
480
  Object(react__WEBPACK_IMPORTED_MODULE_0__["useEffect"])(function () {
477
- if (ocAuth.authInfoManager) {
481
+ if (ocAuth && ocAuth.authInfoManager) {
478
482
  // reactively recieve update on the authstate change
479
483
  ocAuth.authInfoManager.subscribe(updateAuthState);
480
484
  return function () {
@@ -489,6 +493,7 @@ var OCConnect = function OCConnect(_ref) {
489
493
  ocAuth: ocAuth,
490
494
  authState: authState,
491
495
  authError: authError,
496
+ isInitialized: isInitialized,
492
497
  setAuthError: setAuthError
493
498
  }
494
499
  }, children);
@@ -572,21 +577,24 @@ var OCAuthCore = /*#__PURE__*/function () {
572
577
  }, {
573
578
  key: "logout",
574
579
  value: function () {
575
- var _logout = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
576
- var requestUrl;
580
+ var _logout = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(logoutReturnTo) {
581
+ var url;
577
582
  return _regeneratorRuntime().wrap(function _callee$(_context) {
578
583
  while (1) switch (_context.prev = _context.next) {
579
584
  case 0:
580
585
  this.clearStorage();
581
- requestUrl = this.logoutEndPoint;
582
- window.location.assign(requestUrl);
583
- case 3:
586
+ url = new URL(this.logoutEndPoint);
587
+ if (logoutReturnTo) {
588
+ url.searchParams.append('returnTo', logoutReturnTo);
589
+ }
590
+ window.location.assign(url.toString());
591
+ case 4:
584
592
  case "end":
585
593
  return _context.stop();
586
594
  }
587
595
  }, _callee, this);
588
596
  }));
589
- function logout() {
597
+ function logout(_x) {
590
598
  return _logout.apply(this, arguments);
591
599
  }
592
600
  return logout;
@@ -618,7 +626,7 @@ var OCAuthCore = /*#__PURE__*/function () {
618
626
  }
619
627
  }, _callee2, this);
620
628
  }));
621
- function signInWithRedirect(_x) {
629
+ function signInWithRedirect(_x2) {
622
630
  return _signInWithRedirect.apply(this, arguments);
623
631
  }
624
632
  return signInWithRedirect;
@@ -679,7 +687,7 @@ var OCAuthCore = /*#__PURE__*/function () {
679
687
  var _this$getParsedIdToke = this.getParsedIdToken(),
680
688
  edu_username = _this$getParsedIdToke.edu_username,
681
689
  eth_address = _this$getParsedIdToke.eth_address;
682
- this.authInfoManager.setAuthState(this.getAccessToken(), this.getIdToken(), edu_username, eth_address, this.isAuthenticated());
690
+ this.authInfoManager.setAuthState(this.getAccessToken(), this.getIdToken(), edu_username, eth_address, true);
683
691
  }
684
692
  }
685
693
  }, {
@@ -757,7 +765,7 @@ var OCAuthLive = /*#__PURE__*/function (_OCAuthCore2) {
757
765
  var storageClass = Object(_lib_StorageManager__WEBPACK_IMPORTED_MODULE_3__["getStorageClass"])(opts);
758
766
  var pkceTransactionManager = new _lib_TransactionManager__WEBPACK_IMPORTED_MODULE_2__["default"](storageClass);
759
767
  var tokenManager = new _lib_TokenManager__WEBPACK_IMPORTED_MODULE_1__["default"](storageClass, tokenEndpoint, publicKey);
760
- return _callSuper(this, OCAuthLive, [loginEndpoint, redirectUri, pkceTransactionManager, tokenManager, referralCode, logoutEndpoint]);
768
+ return _callSuper(this, OCAuthLive, [loginEndpoint, redirectUri, pkceTransactionManager, tokenManager, referralCode, logoutEndpoint, false]);
761
769
  }
762
770
  _inherits(OCAuthLive, _OCAuthCore2);
763
771
  return _createClass(OCAuthLive);
@@ -771,7 +779,8 @@ var OCAuthSandbox = /*#__PURE__*/function (_OCAuthCore3) {
771
779
  overrideLogoutEndpoint = opts.logoutEndPoint,
772
780
  overridePublicKey = opts.publicKey,
773
781
  redirectUri = opts.redirectUri,
774
- referralCode = opts.referralCode;
782
+ referralCode = opts.referralCode,
783
+ mode = opts.mode;
775
784
  var tokenEndpoint = overrideTokenEndpoint || 'https://api.login.sandbox.opencampus.xyz/auth/token';
776
785
  var loginEndpoint = overrideLoginEndpoint || 'https://api.login.sandbox.opencampus.xyz/auth/login';
777
786
  var logoutEndpoint = overrideLogoutEndpoint || 'https://api.login.sandbox.opencampus.xyz/auth/logout';
@@ -779,7 +788,7 @@ var OCAuthSandbox = /*#__PURE__*/function (_OCAuthCore3) {
779
788
  var storageClass = Object(_lib_StorageManager__WEBPACK_IMPORTED_MODULE_3__["getStorageClass"])(opts);
780
789
  var pkceTransactionManager = new _lib_TransactionManager__WEBPACK_IMPORTED_MODULE_2__["default"](storageClass);
781
790
  var tokenManager = new _lib_TokenManager__WEBPACK_IMPORTED_MODULE_1__["default"](storageClass, tokenEndpoint, publicKey);
782
- return _callSuper(this, OCAuthSandbox, [loginEndpoint, redirectUri, pkceTransactionManager, tokenManager, referralCode, logoutEndpoint]);
791
+ return _callSuper(this, OCAuthSandbox, [loginEndpoint, redirectUri, pkceTransactionManager, tokenManager, referralCode, logoutEndpoint, true]);
783
792
  }
784
793
  _inherits(OCAuthSandbox, _OCAuthCore3);
785
794
  return _createClass(OCAuthSandbox);
@@ -839,7 +848,10 @@ var AuthInfoManager = /*#__PURE__*/function () {
839
848
  }, {
840
849
  key: "clear",
841
850
  value: function clear() {
842
- this._authState = null;
851
+ this._authState = {
852
+ isAuthenticated: false
853
+ };
854
+ this._emitter.emit(EVENT_AUTH_STATE_CHANGE, this._authState);
843
855
  }
844
856
  }, {
845
857
  key: "subscribe",