🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

react-auth-kit

Package Overview
Dependencies
Maintainers
1
Versions
200
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-auth-kit - npm Package Compare versions

Comparing version

to
1.5.0

dist/__tests__/utils/hooks.test.d.ts

@@ -7,2 +7,23 @@ # Changelog

## [1.5.0] - 2021-10-18
## Added:
- Refresh Token feature (#603)
- Added Documentation for Refresh Token (#629)
- Unit test for actions/reducers.ts (#587)
- Unit test for TokenObject.ts (#592)
- Unit test for PrivateRoute.tsx (#606)
- Unit test for utils/hooks.ts (#618)
- Unit test for AuthProvider.tsx(#617)
## Thanks to
- [@atrievel](https://github.com/atrievel)
- [@tumit](https://github.com/tumit)
- [@sitek94](https://github.com/sitek94)
- [@DarrylBrooks97](https://github.com/DarrylBrooks97)
- [@jason1985](https://github.com/jason1985)
- [@katieraby](https://github.com/katieraby)
- [@ok524](https://github.com/ok524)
## [1.4.18] - 2021-09-10

@@ -9,0 +30,0 @@ ## Changed:

5

dist/index.d.ts
import AuthProvider from './AuthProvider';
import createRefresh from './createRefresh';
import PrivateRoute from './PrivateRoute';

@@ -7,3 +8,2 @@ import useSignIn from './hooks/useSignIn';

import useAuthHeader from './hooks/useAuthHeader';
import useRefreshToken from './hooks/useRefreshToken';
import useIsAuthenticated from './hooks/useIsAuthenticated';

@@ -14,4 +14,3 @@ import withSignIn from './higherOrderComponents/withSignIn';

import withAuthHeader from './higherOrderComponents/withAuthHeader';
import withRefreshToken from './higherOrderComponents/withRefreshToken';
import withIsAuthenticated from './higherOrderComponents/withIsAuthenticated';
export { AuthProvider, PrivateRoute, useSignIn, useSignOut, useAuthUser, useAuthHeader, useRefreshToken, useIsAuthenticated, withSignIn, withSignOut, withAuthUser, withAuthHeader, withRefreshToken, withIsAuthenticated, };
export { AuthProvider, PrivateRoute, createRefresh, useSignIn, useSignOut, useAuthUser, useAuthHeader, useIsAuthenticated, withSignIn, withSignOut, withAuthUser, withAuthHeader, withIsAuthenticated, };

280

dist/index.js

@@ -20,5 +20,3 @@ 'use strict';

enumerable: true,
get: function () {
return e[k];
}
get: function () { return e[k]; }
});

@@ -28,3 +26,3 @@ }

}
n['default'] = e;
n["default"] = e;
return Object.freeze(n);

@@ -155,11 +153,11 @@ }

TokenObject.prototype.initialCookieToken_ = function () {
var authToken = Cookies__default['default'].get(this.authStorageName);
var authTokenType = Cookies__default['default'].get(this.authStorageTypeName);
var authTokenTime = Cookies__default['default'].get(this.authTimeStorageName);
var stateCookie = Cookies__default['default'].get(this.stateStorageName);
var authToken = Cookies__default["default"].get(this.authStorageName);
var authTokenType = Cookies__default["default"].get(this.authStorageTypeName);
var authTokenTime = Cookies__default["default"].get(this.authTimeStorageName);
var stateCookie = Cookies__default["default"].get(this.stateStorageName);
var refreshToken = this.isUsingRefreshToken &&
this.refreshTokenName != null ? Cookies__default['default'].get(this.refreshTokenName) : null;
this.refreshTokenName != null ? Cookies__default["default"].get(this.refreshTokenName) : null;
var refreshTokenTime = this.isUsingRefreshToken &&
this.refreshTokenTimeName != null ?
Cookies__default['default'].get(this.refreshTokenTimeName) : null;
Cookies__default["default"].get(this.refreshTokenTimeName) : null;
return this.checkTokenExist(authToken, authTokenType, authTokenTime, stateCookie, refreshToken, refreshTokenTime);

@@ -306,3 +304,3 @@ };

TokenObject.prototype.setCookieToken_ = function (authToken, authTokenType, refreshToken, expiresAt, refreshTokenExpiresAt, authState) {
Cookies__default['default'].set(this.authStorageName, authToken, {
Cookies__default["default"].set(this.authStorageName, authToken, {
expires: expiresAt,

@@ -312,3 +310,3 @@ domain: this.cookieDomain,

});
Cookies__default['default'].set(this.authStorageTypeName, authTokenType, {
Cookies__default["default"].set(this.authStorageTypeName, authTokenType, {
expires: expiresAt,

@@ -318,3 +316,3 @@ domain: this.cookieDomain,

});
Cookies__default['default'].set(this.authTimeStorageName, expiresAt.toISOString(), {
Cookies__default["default"].set(this.authTimeStorageName, expiresAt.toISOString(), {
expires: expiresAt,

@@ -325,3 +323,3 @@ domain: this.cookieDomain,

if (authState) {
Cookies__default['default'].set(this.stateStorageName, JSON.stringify(authState), {
Cookies__default["default"].set(this.stateStorageName, JSON.stringify(authState), {
expires: expiresAt,

@@ -334,3 +332,3 @@ domain: this.cookieDomain,

!!refreshToken) {
Cookies__default['default'].set(this.refreshTokenName, refreshToken, {
Cookies__default["default"].set(this.refreshTokenName, refreshToken, {
expires: expiresAt,

@@ -343,3 +341,3 @@ domain: this.cookieDomain,

!!refreshTokenExpiresAt) {
Cookies__default['default'].set(this.refreshTokenTimeName, refreshTokenExpiresAt.toISOString(), {
Cookies__default["default"].set(this.refreshTokenTimeName, refreshTokenExpiresAt.toISOString(), {
expires: expiresAt,

@@ -392,15 +390,15 @@ domain: this.cookieDomain,

TokenObject.prototype.removeCookieToken_ = function () {
Cookies__default['default'].remove(this.authStorageName, {
Cookies__default["default"].remove(this.authStorageName, {
domain: this.cookieDomain,
secure: this.cookieSecure,
});
Cookies__default['default'].remove(this.authTimeStorageName, {
Cookies__default["default"].remove(this.authTimeStorageName, {
domain: this.cookieDomain,
secure: this.cookieSecure,
});
Cookies__default['default'].remove(this.authStorageTypeName, {
Cookies__default["default"].remove(this.authStorageTypeName, {
domain: this.cookieDomain,
secure: this.cookieSecure,
});
Cookies__default['default'].remove(this.stateStorageName, {
Cookies__default["default"].remove(this.stateStorageName, {
domain: this.cookieDomain,

@@ -410,3 +408,3 @@ secure: this.cookieSecure,

if (this.isUsingRefreshToken && !!this.refreshTokenName) {
Cookies__default['default'].remove(this.refreshTokenName, {
Cookies__default["default"].remove(this.refreshTokenName, {
domain: this.cookieDomain,

@@ -417,3 +415,3 @@ secure: this.cookieSecure,

if (this.isUsingRefreshToken && !!this.refreshTokenTimeName) {
Cookies__default['default'].remove(this.refreshTokenTimeName, {
Cookies__default["default"].remove(this.refreshTokenTimeName, {
domain: this.cookieDomain,

@@ -465,2 +463,3 @@ secure: this.cookieSecure,

ActionType[ActionType["SignOut"] = 1] = "SignOut";
ActionType[ActionType["RefreshToken"] = 2] = "RefreshToken";
})(ActionType || (ActionType = {}));

@@ -480,4 +479,25 @@

return __assign(__assign({}, state), { auth: null, refresh: null, userState: null, isSignIn: false });
default:
return state;
case ActionType.RefreshToken:
if (state.isSignIn && state.auth && state.refresh) {
return __assign(__assign({}, state), { auth: {
token: action.payload.newAuthToken ?
action.payload.newAuthToken : state.auth.token,
type: state.auth.type,
expiresAt: action.payload.newAuthTokenExpireIn ?
new Date(new Date().getTime() +
action.payload.newAuthTokenExpireIn * 60 * 1000) :
state.auth.expiresAt,
}, refresh: {
token: action.payload.newRefreshToken ?
action.payload.newRefreshToken : state.refresh.token,
expiresAt: action.payload.newRefreshTokenExpiresIn ?
new Date(new Date().getTime() +
action.payload.newRefreshTokenExpiresIn * 60 * 1000) :
state.refresh.expiresAt,
}, userState: action.payload.newAuthUserState ?
action.payload.newAuthUserState : state.userState });
}
else {
return state;
}
}

@@ -497,2 +517,12 @@ }

/**
* used to refresh the Token
* @param refreshTokenParam
*/
function doRefresh(refreshTokenParam) {
return ({
type: ActionType.RefreshToken,
payload: refreshTokenParam,
});
}
/**
* Used to make sign out

@@ -506,3 +536,49 @@ */

/*
* Copyright 2020 Arkadip Bhattacharya
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* React useInterval Hook
* Used to integrate the power of setInterval seamlessly
*
* @param callback - The callback function
* @param delay - The amount of delay in minutes.
*
* @returns the ref of setInterval
*/
function useInterval(callback, delay) {
var savedCallback = React__namespace.useRef(callback);
var intervalRef = React__namespace.useRef(null);
// Remember the latest callback if it changes.
React__namespace.useEffect(function () {
savedCallback.current = callback;
}, [callback]);
// Set up the interval.
React__namespace.useEffect(function () {
var tick = function () { return savedCallback.current(); };
if (typeof delay === 'number') {
intervalRef.current = window.setInterval(tick, delay * 60 * 1000);
}
return function () {
if (intervalRef.current) {
window.clearTimeout(intervalRef.current);
}
};
}, [delay]);
return intervalRef;
}
/**
* AuthProvider - The Authentication Context Provider

@@ -518,3 +594,3 @@ *

var AuthProvider = function (_a) {
var children = _a.children, authType = _a.authType, authName = _a.authName, refreshToken = _a.refreshToken, cookieDomain = _a.cookieDomain, cookieSecure = _a.cookieSecure;
var children = _a.children, authType = _a.authType, authName = _a.authName, cookieDomain = _a.cookieDomain, cookieSecure = _a.cookieSecure, refresh = _a.refresh;
if (authType === 'cookie') {

@@ -527,5 +603,22 @@ if (!cookieDomain) {

}
var refreshTokenName = refreshToken ? authName + "_refresh" : null;
var refreshTokenName = refresh ? authName + "_refresh" : null;
var tokenObject = new TokenObject(authName, authType, refreshTokenName, cookieDomain, cookieSecure);
var _b = React__namespace.useReducer(authReducer, tokenObject.initialToken()), authState = _b[0], dispatch = _b[1];
if (refresh) {
useInterval(function () {
var _a, _b, _c, _d;
var r = refresh.refreshApiCallback({
authToken: (_a = authState.auth) === null || _a === void 0 ? void 0 : _a.token,
authTokenExpireAt: (_b = authState.auth) === null || _b === void 0 ? void 0 : _b.expiresAt,
authUserState: authState.userState,
refreshToken: (_c = authState.refresh) === null || _c === void 0 ? void 0 : _c.token,
refreshTokenExpiresAt: (_d = authState.refresh) === null || _d === void 0 ? void 0 : _d.expiresAt,
});
// store the new value using the state update
if (r.isSuccess) {
// IF the API call is successful then refresh the AUTH state
dispatch(doRefresh(r));
}
}, authState.isSignIn ? refresh.interval : null);
}
React__namespace.useEffect(function () {

@@ -537,3 +630,27 @@ tokenObject.syncTokens(authState);

/*
* Copyright 2020 Arkadip Bhattacharya
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This function doesn't really "do anything" at runtime,
* it's just help to organize the code base
* Use this function to create the refresh token system
*/
function createRefresh(param) {
return param;
}
/**
* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com>

@@ -811,74 +928,4 @@ * @fileoverview PrivateRoute component

/*
* Copyright 2020 Arkadip Bhattacharya
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @class RefreshToken
*
* Refreshes Token
*/
var RefreshToken = /** @class */ (function () {
/**
* @param context
* @constructor
*/
function RefreshToken(context) {
this.context = context;
if (!this.context.authState.isUsingRefreshToken) {
throw new Error('The app doesn\'t implement \'refreshToken\' feature.\n' +
'So you have to implement refresh token feature from ' +
'\'AuthProvider\' before using it.');
}
}
return RefreshToken;
}());
/**
* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com>
* @fileoverview Refresh Token <Hook>
* @copyright Arkadip Bhattacharya 2020
*
* Copyright 2020 Arkadip Bhattacharya
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*@function
*@name useRefreshToken
*@description Refresh Token Hook
*@returns - RefreshToken object
*/
function useRefreshToken() {
var _context = React__namespace.useContext(AuthContext);
if (_context === null) {
throw new Error('Auth Provider is missing. ' +
'Please add the AuthProvider before Router');
}
return new RefreshToken(_context);
}
/**
* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com>
* @fileoverview Authentication status <hook>

@@ -1103,40 +1150,2 @@ * @copyright Arkadip Bhattacharya 2020

* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com>
* @fileoverview Refresh Token <Higher Order Component>
* @copyright Arkadip Bhattacharya 2020
*
* Copyright 2020 Arkadip Bhattacharya
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @public
* @function
* @name withAuthHeader
* @description Inject Authentication Header inside the Component's Prop
* @param Component - React Component
*/
function withRefreshToken(Component) {
return function (props) {
return (React__namespace.createElement(AuthContextConsumer, null, function (c) {
if (c === null) {
throw new Error('Auth Provider is missing. ' +
'Please add the AuthProvider before Router');
}
return (React__namespace.createElement(Component, __assign({}, props, { refreshToken: new RefreshToken(c) })));
}));
};
}
/**
* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com>
* @fileoverview Authentication status <Higher Order Component>

@@ -1213,6 +1222,6 @@ * @copyright Arkadip Bhattacharya 2020

exports.PrivateRoute = PrivateRoute;
exports.createRefresh = createRefresh;
exports.useAuthHeader = useAuthHeader;
exports.useAuthUser = useAuthUser;
exports.useIsAuthenticated = useIsAuthenticated;
exports.useRefreshToken = useRefreshToken;
exports.useSignIn = useSignIn;

@@ -1223,5 +1232,4 @@ exports.useSignOut = useSignOut;

exports.withIsAuthenticated = withIsAuthenticated;
exports.withRefreshToken = withRefreshToken;
exports.withSignIn = withSignIn;
exports.withSignOut = withSignOut;
//# sourceMappingURL=index.js.map

@@ -424,2 +424,3 @@ import * as React from 'react';

ActionType[ActionType["SignOut"] = 1] = "SignOut";
ActionType[ActionType["RefreshToken"] = 2] = "RefreshToken";
})(ActionType || (ActionType = {}));

@@ -439,4 +440,25 @@

return __assign(__assign({}, state), { auth: null, refresh: null, userState: null, isSignIn: false });
default:
return state;
case ActionType.RefreshToken:
if (state.isSignIn && state.auth && state.refresh) {
return __assign(__assign({}, state), { auth: {
token: action.payload.newAuthToken ?
action.payload.newAuthToken : state.auth.token,
type: state.auth.type,
expiresAt: action.payload.newAuthTokenExpireIn ?
new Date(new Date().getTime() +
action.payload.newAuthTokenExpireIn * 60 * 1000) :
state.auth.expiresAt,
}, refresh: {
token: action.payload.newRefreshToken ?
action.payload.newRefreshToken : state.refresh.token,
expiresAt: action.payload.newRefreshTokenExpiresIn ?
new Date(new Date().getTime() +
action.payload.newRefreshTokenExpiresIn * 60 * 1000) :
state.refresh.expiresAt,
}, userState: action.payload.newAuthUserState ?
action.payload.newAuthUserState : state.userState });
}
else {
return state;
}
}

@@ -456,2 +478,12 @@ }

/**
* used to refresh the Token
* @param refreshTokenParam
*/
function doRefresh(refreshTokenParam) {
return ({
type: ActionType.RefreshToken,
payload: refreshTokenParam,
});
}
/**
* Used to make sign out

@@ -465,3 +497,49 @@ */

/*
* Copyright 2020 Arkadip Bhattacharya
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* React useInterval Hook
* Used to integrate the power of setInterval seamlessly
*
* @param callback - The callback function
* @param delay - The amount of delay in minutes.
*
* @returns the ref of setInterval
*/
function useInterval(callback, delay) {
var savedCallback = React.useRef(callback);
var intervalRef = React.useRef(null);
// Remember the latest callback if it changes.
React.useEffect(function () {
savedCallback.current = callback;
}, [callback]);
// Set up the interval.
React.useEffect(function () {
var tick = function () { return savedCallback.current(); };
if (typeof delay === 'number') {
intervalRef.current = window.setInterval(tick, delay * 60 * 1000);
}
return function () {
if (intervalRef.current) {
window.clearTimeout(intervalRef.current);
}
};
}, [delay]);
return intervalRef;
}
/**
* AuthProvider - The Authentication Context Provider

@@ -477,3 +555,3 @@ *

var AuthProvider = function (_a) {
var children = _a.children, authType = _a.authType, authName = _a.authName, refreshToken = _a.refreshToken, cookieDomain = _a.cookieDomain, cookieSecure = _a.cookieSecure;
var children = _a.children, authType = _a.authType, authName = _a.authName, cookieDomain = _a.cookieDomain, cookieSecure = _a.cookieSecure, refresh = _a.refresh;
if (authType === 'cookie') {

@@ -486,5 +564,22 @@ if (!cookieDomain) {

}
var refreshTokenName = refreshToken ? authName + "_refresh" : null;
var refreshTokenName = refresh ? authName + "_refresh" : null;
var tokenObject = new TokenObject(authName, authType, refreshTokenName, cookieDomain, cookieSecure);
var _b = React.useReducer(authReducer, tokenObject.initialToken()), authState = _b[0], dispatch = _b[1];
if (refresh) {
useInterval(function () {
var _a, _b, _c, _d;
var r = refresh.refreshApiCallback({
authToken: (_a = authState.auth) === null || _a === void 0 ? void 0 : _a.token,
authTokenExpireAt: (_b = authState.auth) === null || _b === void 0 ? void 0 : _b.expiresAt,
authUserState: authState.userState,
refreshToken: (_c = authState.refresh) === null || _c === void 0 ? void 0 : _c.token,
refreshTokenExpiresAt: (_d = authState.refresh) === null || _d === void 0 ? void 0 : _d.expiresAt,
});
// store the new value using the state update
if (r.isSuccess) {
// IF the API call is successful then refresh the AUTH state
dispatch(doRefresh(r));
}
}, authState.isSignIn ? refresh.interval : null);
}
React.useEffect(function () {

@@ -496,3 +591,27 @@ tokenObject.syncTokens(authState);

/*
* Copyright 2020 Arkadip Bhattacharya
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This function doesn't really "do anything" at runtime,
* it's just help to organize the code base
* Use this function to create the refresh token system
*/
function createRefresh(param) {
return param;
}
/**
* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com>

@@ -770,74 +889,4 @@ * @fileoverview PrivateRoute component

/*
* Copyright 2020 Arkadip Bhattacharya
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @class RefreshToken
*
* Refreshes Token
*/
var RefreshToken = /** @class */ (function () {
/**
* @param context
* @constructor
*/
function RefreshToken(context) {
this.context = context;
if (!this.context.authState.isUsingRefreshToken) {
throw new Error('The app doesn\'t implement \'refreshToken\' feature.\n' +
'So you have to implement refresh token feature from ' +
'\'AuthProvider\' before using it.');
}
}
return RefreshToken;
}());
/**
* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com>
* @fileoverview Refresh Token <Hook>
* @copyright Arkadip Bhattacharya 2020
*
* Copyright 2020 Arkadip Bhattacharya
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*@function
*@name useRefreshToken
*@description Refresh Token Hook
*@returns - RefreshToken object
*/
function useRefreshToken() {
var _context = React.useContext(AuthContext);
if (_context === null) {
throw new Error('Auth Provider is missing. ' +
'Please add the AuthProvider before Router');
}
return new RefreshToken(_context);
}
/**
* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com>
* @fileoverview Authentication status <hook>

@@ -1062,40 +1111,2 @@ * @copyright Arkadip Bhattacharya 2020

* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com>
* @fileoverview Refresh Token <Higher Order Component>
* @copyright Arkadip Bhattacharya 2020
*
* Copyright 2020 Arkadip Bhattacharya
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @public
* @function
* @name withAuthHeader
* @description Inject Authentication Header inside the Component's Prop
* @param Component - React Component
*/
function withRefreshToken(Component) {
return function (props) {
return (React.createElement(AuthContextConsumer, null, function (c) {
if (c === null) {
throw new Error('Auth Provider is missing. ' +
'Please add the AuthProvider before Router');
}
return (React.createElement(Component, __assign({}, props, { refreshToken: new RefreshToken(c) })));
}));
};
}
/**
* @author Arkadip Bhattacharya <in2arkadipb13@gmail.com>
* @fileoverview Authentication status <Higher Order Component>

@@ -1170,3 +1181,3 @@ * @copyright Arkadip Bhattacharya 2020

export { AuthProvider, PrivateRoute, useAuthHeader, useAuthUser, useIsAuthenticated, useRefreshToken, useSignIn, useSignOut, withAuthHeader, withAuthUser, withIsAuthenticated, withRefreshToken, withSignIn, withSignOut };
export { AuthProvider, PrivateRoute, createRefresh, useAuthHeader, useAuthUser, useIsAuthenticated, useSignIn, useSignOut, withAuthHeader, withAuthUser, withIsAuthenticated, withSignIn, withSignOut };
//# sourceMappingURL=index.modern.js.map

@@ -1,3 +0,3 @@

/*! react-auth-kit v1.4.18 | Apache-2.0 */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("js-cookie"),require("react-router-dom")):"function"==typeof define&&define.amd?define(["exports","react","js-cookie","react-router-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactAuthKit={},e.React,e.Cookies,e.ReactRouterDOM)}(this,(function(e,t,r,o){"use strict";function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function i(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var o=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,o.get?o:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var a,s=i(t),u=n(r),h=s.createContext(null),f=h.Consumer,c=function(){return(c=Object.assign||function(e){for(var t,r=1,o=arguments.length;r<o;r++)for(var n in t=arguments[r])Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e}).apply(this,arguments)},l=function(){function e(e,t,r,o,n){this.authStorageType=t,this.authStorageName=e,this.authTimeStorageName=e+"_storage",this.stateStorageName=e+"_state",this.refreshTokenName=r,this.cookieDomain=o,this.cookieSecure=n,this.authStorageTypeName=this.authStorageName+"_type",this.isUsingRefreshToken=!!this.refreshTokenName,this.refreshTokenTimeName=this.refreshTokenName?this.refreshTokenName+"_time":null}return e.prototype.initialToken=function(){return"cookie"===this.authStorageType?this.initialCookieToken_():this.initialLSToken_()},e.prototype.initialCookieToken_=function(){var e=u.default.get(this.authStorageName),t=u.default.get(this.authStorageTypeName),r=u.default.get(this.authTimeStorageName),o=u.default.get(this.stateStorageName),n=this.isUsingRefreshToken&&null!=this.refreshTokenName?u.default.get(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?u.default.get(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,r,o,n,i)},e.prototype.initialLSToken_=function(){var e=localStorage.getItem(this.authStorageName),t=localStorage.getItem(this.authStorageTypeName),r=localStorage.getItem(this.authTimeStorageName),o=localStorage.getItem(this.stateStorageName),n=this.isUsingRefreshToken&&null!=this.refreshTokenName?localStorage.getItem(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?localStorage.getItem(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,r,o,n,i)},e.prototype.checkTokenExist=function(e,t,r,o,n,i){if(!(e&&t&&r&&o))return{auth:null,refresh:null,userState:null,isUsingRefreshToken:this.isUsingRefreshToken,isSignIn:!1};var a=new Date(r);try{var s={auth:{token:e,type:t,expiresAt:a},userState:JSON.parse(o),isSignIn:!0,isUsingRefreshToken:this.isUsingRefreshToken,refresh:void 0};if(this.isUsingRefreshToken&&n&&i){var u=new Date(i);return c(c({},s),{refresh:{token:n,expiresAt:u}})}return c(c({},s),{refresh:null})}catch(e){return{auth:null,refresh:null,userState:null,isUsingRefreshToken:this.isUsingRefreshToken,isSignIn:!1}}},e.prototype.syncTokens=function(e){e.auth?this.isUsingRefreshToken&&e.refresh?this.setToken(e.auth.token,e.auth.type,e.refresh.token,e.refresh.expiresAt,e.auth.expiresAt,e.userState):this.setToken(e.auth.token,e.auth.type,null,null,e.auth.expiresAt,e.userState):this.removeToken()},e.prototype.setToken=function(e,t,r,o,n,i){"cookie"===this.authStorageType?this.setCookieToken_(e,t,r,n,o,i):this.setLSToken_(e,t,r,n,o,i)},e.prototype.setCookieToken_=function(e,t,r,o,n,i){u.default.set(this.authStorageName,e,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),u.default.set(this.authStorageTypeName,t,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),u.default.set(this.authTimeStorageName,o.toISOString(),{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),i&&u.default.set(this.stateStorageName,JSON.stringify(i),{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&r&&u.default.set(this.refreshTokenName,r,{expires:o,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&n&&u.default.set(this.refreshTokenTimeName,n.toISOString(),{expires:o,domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.setLSToken_=function(e,t,r,o,n,i){localStorage.setItem(this.authStorageName,e),localStorage.setItem(this.authStorageTypeName,t),localStorage.setItem(this.authTimeStorageName,o.toISOString()),i&&localStorage.setItem(this.stateStorageName,JSON.stringify(i)),this.isUsingRefreshToken&&this.refreshTokenName&&r&&localStorage.setItem(this.refreshTokenName,r),this.isUsingRefreshToken&&this.refreshTokenTimeName&&n&&localStorage.setItem(this.refreshTokenTimeName,n.toISOString())},e.prototype.removeToken=function(){"cookie"===this.authStorageType?this.removeCookieToken_():this.removeLSToken_()},e.prototype.removeCookieToken_=function(){u.default.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.authStorageTypeName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&u.default.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&u.default.remove(this.refreshTokenTimeName,{domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.removeLSToken_=function(){localStorage.removeItem(this.authStorageName),localStorage.removeItem(this.authTimeStorageName),localStorage.removeItem(this.authStorageTypeName),localStorage.removeItem(this.stateStorageName),this.isUsingRefreshToken&&this.refreshTokenName&&localStorage.removeItem(this.refreshTokenName),this.isUsingRefreshToken&&this.refreshTokenTimeName&&localStorage.removeItem(this.refreshTokenTimeName)},e}();function m(e,t){switch(t.type){case a.SignIn:return c(c({},e),{auth:t.payload.auth,refresh:t.payload.refresh,userState:t.payload.userState,isSignIn:!0});case a.SignOut:return c(c({},e),{auth:null,refresh:null,userState:null,isSignIn:!1});default:return e}}function k(e){return{type:a.SignIn,payload:e}}function d(){return{type:a.SignOut}}!function(e){e[e.SignIn=0]="SignIn",e[e.SignOut=1]="SignOut"}(a||(a={}));var g=function(e){var t=e.children,r=e.authType,o=e.authName,n=e.refreshToken,i=e.cookieDomain,a=e.cookieSecure;if("cookie"===r&&!i)throw new Error("authType 'cookie' requires 'cookieDomain' and 'cookieSecure' props in AuthProvider");var u=new l(o,r,n?o+"_refresh":null,i,a),f=s.useReducer(m,u.initialToken()),c=f[0],k=f[1];return s.useEffect((function(){u.syncTokens(c)}),[c]),s.createElement(h.Provider,{value:{authState:c,dispatch:k}},t)};var S=function(e){if(this.context=e,!this.context.authState.isUsingRefreshToken)throw new Error("The app doesn't implement 'refreshToken' feature.\nSo you have to implement refresh token feature from 'AuthProvider' before using it.")};g.defaultProps={cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol},e.AuthProvider=g,e.PrivateRoute=function(e){var t=s.useContext(h);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");var r=e.component,n=e.loginPath,i=e.strict,a=e.sensitive,u=e.exact,f=e.path,c=e.location,l=e.render;return s.createElement(o.Route,{location:c,path:f,exact:u,sensitive:a,strict:i,render:function(e){return t.authState.auth&&(new Date(t.authState.auth.expiresAt)>new Date||(t.dispatch(d()),0))?r?s.createElement(r,e):l?l(e):null:s.createElement(o.Redirect,{to:n})}})},e.useAuthHeader=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return e.authState.auth?e.authState.auth.type+" "+e.authState.auth.token:""}},e.useAuthUser=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return e.authState.auth?e.authState.userState:null}},e.useIsAuthenticated=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return!!e.authState.auth&&(new Date(e.authState.auth.expiresAt)>new Date||(e.dispatch(d()),!1))}},e.useRefreshToken=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return new S(e)},e.useSignIn=
/*! react-auth-kit v1.5.0 | Apache-2.0 */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("js-cookie"),require("react-router-dom")):"function"==typeof define&&define.amd?define(["exports","react","js-cookie","react-router-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactAuthKit={},e.React,e.Cookies,e.ReactRouterDOM)}(this,(function(e,t,r,n){"use strict";function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function i(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var a,s=i(t),u=o(r),h=s.createContext(null),f=h.Consumer,c=function(){return(c=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)},l=function(){function e(e,t,r,n,o){this.authStorageType=t,this.authStorageName=e,this.authTimeStorageName=e+"_storage",this.stateStorageName=e+"_state",this.refreshTokenName=r,this.cookieDomain=n,this.cookieSecure=o,this.authStorageTypeName=this.authStorageName+"_type",this.isUsingRefreshToken=!!this.refreshTokenName,this.refreshTokenTimeName=this.refreshTokenName?this.refreshTokenName+"_time":null}return e.prototype.initialToken=function(){return"cookie"===this.authStorageType?this.initialCookieToken_():this.initialLSToken_()},e.prototype.initialCookieToken_=function(){var e=u.default.get(this.authStorageName),t=u.default.get(this.authStorageTypeName),r=u.default.get(this.authTimeStorageName),n=u.default.get(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?u.default.get(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?u.default.get(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,r,n,o,i)},e.prototype.initialLSToken_=function(){var e=localStorage.getItem(this.authStorageName),t=localStorage.getItem(this.authStorageTypeName),r=localStorage.getItem(this.authTimeStorageName),n=localStorage.getItem(this.stateStorageName),o=this.isUsingRefreshToken&&null!=this.refreshTokenName?localStorage.getItem(this.refreshTokenName):null,i=this.isUsingRefreshToken&&null!=this.refreshTokenTimeName?localStorage.getItem(this.refreshTokenTimeName):null;return this.checkTokenExist(e,t,r,n,o,i)},e.prototype.checkTokenExist=function(e,t,r,n,o,i){if(!(e&&t&&r&&n))return{auth:null,refresh:null,userState:null,isUsingRefreshToken:this.isUsingRefreshToken,isSignIn:!1};var a=new Date(r);try{var s={auth:{token:e,type:t,expiresAt:a},userState:JSON.parse(n),isSignIn:!0,isUsingRefreshToken:this.isUsingRefreshToken,refresh:void 0};if(this.isUsingRefreshToken&&o&&i){var u=new Date(i);return c(c({},s),{refresh:{token:o,expiresAt:u}})}return c(c({},s),{refresh:null})}catch(e){return{auth:null,refresh:null,userState:null,isUsingRefreshToken:this.isUsingRefreshToken,isSignIn:!1}}},e.prototype.syncTokens=function(e){e.auth?this.isUsingRefreshToken&&e.refresh?this.setToken(e.auth.token,e.auth.type,e.refresh.token,e.refresh.expiresAt,e.auth.expiresAt,e.userState):this.setToken(e.auth.token,e.auth.type,null,null,e.auth.expiresAt,e.userState):this.removeToken()},e.prototype.setToken=function(e,t,r,n,o,i){"cookie"===this.authStorageType?this.setCookieToken_(e,t,r,o,n,i):this.setLSToken_(e,t,r,o,n,i)},e.prototype.setCookieToken_=function(e,t,r,n,o,i){u.default.set(this.authStorageName,e,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),u.default.set(this.authStorageTypeName,t,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),u.default.set(this.authTimeStorageName,n.toISOString(),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),i&&u.default.set(this.stateStorageName,JSON.stringify(i),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&r&&u.default.set(this.refreshTokenName,r,{expires:n,domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&o&&u.default.set(this.refreshTokenTimeName,o.toISOString(),{expires:n,domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.setLSToken_=function(e,t,r,n,o,i){localStorage.setItem(this.authStorageName,e),localStorage.setItem(this.authStorageTypeName,t),localStorage.setItem(this.authTimeStorageName,n.toISOString()),i&&localStorage.setItem(this.stateStorageName,JSON.stringify(i)),this.isUsingRefreshToken&&this.refreshTokenName&&r&&localStorage.setItem(this.refreshTokenName,r),this.isUsingRefreshToken&&this.refreshTokenTimeName&&o&&localStorage.setItem(this.refreshTokenTimeName,o.toISOString())},e.prototype.removeToken=function(){"cookie"===this.authStorageType?this.removeCookieToken_():this.removeLSToken_()},e.prototype.removeCookieToken_=function(){u.default.remove(this.authStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.authTimeStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.authStorageTypeName,{domain:this.cookieDomain,secure:this.cookieSecure}),u.default.remove(this.stateStorageName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenName&&u.default.remove(this.refreshTokenName,{domain:this.cookieDomain,secure:this.cookieSecure}),this.isUsingRefreshToken&&this.refreshTokenTimeName&&u.default.remove(this.refreshTokenTimeName,{domain:this.cookieDomain,secure:this.cookieSecure})},e.prototype.removeLSToken_=function(){localStorage.removeItem(this.authStorageName),localStorage.removeItem(this.authTimeStorageName),localStorage.removeItem(this.authStorageTypeName),localStorage.removeItem(this.stateStorageName),this.isUsingRefreshToken&&this.refreshTokenName&&localStorage.removeItem(this.refreshTokenName),this.isUsingRefreshToken&&this.refreshTokenTimeName&&localStorage.removeItem(this.refreshTokenTimeName)},e}();function m(e,t){switch(t.type){case a.SignIn:return c(c({},e),{auth:t.payload.auth,refresh:t.payload.refresh,userState:t.payload.userState,isSignIn:!0});case a.SignOut:return c(c({},e),{auth:null,refresh:null,userState:null,isSignIn:!1});case a.RefreshToken:return e.isSignIn&&e.auth&&e.refresh?c(c({},e),{auth:{token:t.payload.newAuthToken?t.payload.newAuthToken:e.auth.token,type:e.auth.type,expiresAt:t.payload.newAuthTokenExpireIn?new Date((new Date).getTime()+60*t.payload.newAuthTokenExpireIn*1e3):e.auth.expiresAt},refresh:{token:t.payload.newRefreshToken?t.payload.newRefreshToken:e.refresh.token,expiresAt:t.payload.newRefreshTokenExpiresIn?new Date((new Date).getTime()+60*t.payload.newRefreshTokenExpiresIn*1e3):e.refresh.expiresAt},userState:t.payload.newAuthUserState?t.payload.newAuthUserState:e.userState}):e}}function k(e){return{type:a.SignIn,payload:e}}function p(){return{type:a.SignOut}}!function(e){e[e.SignIn=0]="SignIn",e[e.SignOut=1]="SignOut",e[e.RefreshToken=2]="RefreshToken"}(a||(a={}));var d=function(e){var t=e.children,r=e.authType,n=e.authName,o=e.cookieDomain,i=e.cookieSecure,u=e.refresh;if("cookie"===r&&!o)throw new Error("authType 'cookie' requires 'cookieDomain' and 'cookieSecure' props in AuthProvider");var f,c,k,p,d=new l(n,r,u?n+"_refresh":null,o,i),S=s.useReducer(m,d.initialToken()),T=S[0],g=S[1];return u&&(f=function(){var e,t,r,n,o,i=u.refreshApiCallback({authToken:null===(e=T.auth)||void 0===e?void 0:e.token,authTokenExpireAt:null===(t=T.auth)||void 0===t?void 0:t.expiresAt,authUserState:T.userState,refreshToken:null===(r=T.refresh)||void 0===r?void 0:r.token,refreshTokenExpiresAt:null===(n=T.refresh)||void 0===n?void 0:n.expiresAt});i.isSuccess&&g((o=i,{type:a.RefreshToken,payload:o}))},c=T.isSignIn?u.interval:null,k=s.useRef(f),p=s.useRef(null),s.useEffect((function(){k.current=f}),[f]),s.useEffect((function(){return"number"==typeof c&&(p.current=window.setInterval((function(){return k.current()}),60*c*1e3)),function(){p.current&&window.clearTimeout(p.current)}}),[c])),s.useEffect((function(){d.syncTokens(T)}),[T]),s.createElement(h.Provider,{value:{authState:T,dispatch:g}},t)};d.defaultProps={cookieDomain:window.location.hostname,cookieSecure:"https:"===window.location.protocol},e.AuthProvider=d,e.PrivateRoute=function(e){var t=s.useContext(h);if(null===t)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");var r=e.component,o=e.loginPath,i=e.strict,a=e.sensitive,u=e.exact,f=e.path,c=e.location,l=e.render;return s.createElement(n.Route,{location:c,path:f,exact:u,sensitive:a,strict:i,render:function(e){return t.authState.auth&&(new Date(t.authState.auth.expiresAt)>new Date||(t.dispatch(p()),0))?r?s.createElement(r,e):l?l(e):null:s.createElement(n.Redirect,{to:o})}})},e.createRefresh=function(e){return e},e.useAuthHeader=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return e.authState.auth?e.authState.auth.type+" "+e.authState.auth.token:""}},e.useAuthUser=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return e.authState.auth?e.authState.userState:null}},e.useIsAuthenticated=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){return!!e.authState.auth&&(new Date(e.authState.auth.expiresAt)>new Date||(e.dispatch(p()),!1))}},e.useSignIn=
/**

@@ -23,3 +23,3 @@ *@author Arkadip Bhattacharya <in2arkadipb13@gmail.com>

*/
function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(t){var r=t.token,o=t.tokenType,n=t.authState,i=t.expiresIn,a=t.refreshToken,s=t.refreshTokenExpireIn,u=new Date((new Date).getTime()+60*i*1e3);if(e.authState.isUsingRefreshToken){if(a&&s){var h=new Date((new Date).getTime()+60*s*1e3);return e.dispatch(k({auth:{token:r,type:o,expiresAt:u},userState:n,refresh:{token:a,expiresAt:h}})),!0}throw new Error('Make sure you given "refreshToken" and "refreshTokenExpireIn" parameter')}if(a&&s)throw new Error("The app doesn't implement 'refreshToken' feature.\nSo you have to implement refresh token feature from 'AuthProvider' before using it.");return e.dispatch(k({auth:{token:r,type:o,expiresAt:u},userState:n,refresh:null})),!0}},e.useSignOut=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){try{return!!e&&(e.dispatch(d()),!0)}catch(e){return!1}}},e.withAuthHeader=function(e){return function(t){return s.createElement(f,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?s.createElement(e,c({},t,{authHeader:r.authState.auth.type+" "+r.authState.auth.token})):s.createElement(e,c({},t,{authHeader:""}))}))}},e.withAuthUser=function(e){return function(t){return s.createElement(f,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?s.createElement(e,c({},t,{authState:r.authState.userState})):s.createElement(e,c({},t,{authState:null}))}))}},e.withIsAuthenticated=function(e){return function(t){return s.createElement(f,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?new Date(r.authState.auth.expiresAt)>new Date?s.createElement(e,c({},t,{isAuth:!0})):(r.dispatch(d()),s.createElement(e,c({},t,{isAuth:!1}))):s.createElement(e,c({},t,{isAuth:!1}))}))}},e.withRefreshToken=function(e){return function(t){return s.createElement(f,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return s.createElement(e,c({},t,{refreshToken:new S(r)}))}))}},e.withSignIn=function(e){return function(t){return s.createElement(f,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return s.createElement(e,c({},t,{signIn:function(e){var t=e.token,o=e.tokenType,n=e.authState,i=e.expiresIn,a=e.refreshToken,s=e.refreshTokenExpireIn,u=new Date((new Date).getTime()+60*i*1e3);if(r.authState.isUsingRefreshToken){if(a&&s){var h=new Date((new Date).getTime()+60*s*1e3);return r.dispatch(k({auth:{token:t,type:o,expiresAt:u},userState:n,refresh:{token:a,expiresAt:h}})),!0}throw new Error('Make sure you given "refreshToken" and "refreshTokenExpireIn" parameter')}if(a&&s)throw new Error("The app doesn't implement 'refreshToken' feature.\n So you have to implement refresh token feature from 'AuthProvider' before using it.");return r.dispatch(k({auth:{token:t,type:o,expiresAt:u},userState:n,refresh:null})),!0}}))}))}},e.withSignOut=function(e){return function(t){return s.createElement(f,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return s.createElement(e,c({},t,{signOut:function(){try{return!!r&&(r.dispatch(d()),!0)}catch(e){return!1}}}))}))}},Object.defineProperty(e,"__esModule",{value:!0})}));
function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(t){var r=t.token,n=t.tokenType,o=t.authState,i=t.expiresIn,a=t.refreshToken,s=t.refreshTokenExpireIn,u=new Date((new Date).getTime()+60*i*1e3);if(e.authState.isUsingRefreshToken){if(a&&s){var h=new Date((new Date).getTime()+60*s*1e3);return e.dispatch(k({auth:{token:r,type:n,expiresAt:u},userState:o,refresh:{token:a,expiresAt:h}})),!0}throw new Error('Make sure you given "refreshToken" and "refreshTokenExpireIn" parameter')}if(a&&s)throw new Error("The app doesn't implement 'refreshToken' feature.\nSo you have to implement refresh token feature from 'AuthProvider' before using it.");return e.dispatch(k({auth:{token:r,type:n,expiresAt:u},userState:o,refresh:null})),!0}},e.useSignOut=function(){var e=s.useContext(h);if(null===e)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return function(){try{return!!e&&(e.dispatch(p()),!0)}catch(e){return!1}}},e.withAuthHeader=function(e){return function(t){return s.createElement(f,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?s.createElement(e,c({},t,{authHeader:r.authState.auth.type+" "+r.authState.auth.token})):s.createElement(e,c({},t,{authHeader:""}))}))}},e.withAuthUser=function(e){return function(t){return s.createElement(f,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?s.createElement(e,c({},t,{authState:r.authState.userState})):s.createElement(e,c({},t,{authState:null}))}))}},e.withIsAuthenticated=function(e){return function(t){return s.createElement(f,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return r.authState.auth?new Date(r.authState.auth.expiresAt)>new Date?s.createElement(e,c({},t,{isAuth:!0})):(r.dispatch(p()),s.createElement(e,c({},t,{isAuth:!1}))):s.createElement(e,c({},t,{isAuth:!1}))}))}},e.withSignIn=function(e){return function(t){return s.createElement(f,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return s.createElement(e,c({},t,{signIn:function(e){var t=e.token,n=e.tokenType,o=e.authState,i=e.expiresIn,a=e.refreshToken,s=e.refreshTokenExpireIn,u=new Date((new Date).getTime()+60*i*1e3);if(r.authState.isUsingRefreshToken){if(a&&s){var h=new Date((new Date).getTime()+60*s*1e3);return r.dispatch(k({auth:{token:t,type:n,expiresAt:u},userState:o,refresh:{token:a,expiresAt:h}})),!0}throw new Error('Make sure you given "refreshToken" and "refreshTokenExpireIn" parameter')}if(a&&s)throw new Error("The app doesn't implement 'refreshToken' feature.\n So you have to implement refresh token feature from 'AuthProvider' before using it.");return r.dispatch(k({auth:{token:t,type:n,expiresAt:u},userState:o,refresh:null})),!0}}))}))}},e.withSignOut=function(e){return function(t){return s.createElement(f,null,(function(r){if(null===r)throw new Error("Auth Provider is missing. Please add the AuthProvider before Router");return s.createElement(e,c({},t,{signOut:function(){try{return!!r&&(r.dispatch(p()),!0)}catch(e){return!1}}}))}))}},Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=index.umd.js.map

@@ -22,2 +22,8 @@ /**

import { AuthActions } from './utils/actions';
/**
* Universal types
*/
export declare type AuthStateUserObject = {
[x: string]: any;
};
export interface AuthKitStateInterface {

@@ -37,2 +43,6 @@ auth: {

}
/**
* SignIn function param
* Used by: useSignIn and withSignIn
*/
export interface signInFunctionParams {

@@ -46,2 +56,5 @@ token: string;

}
/**
* Context values type
*/
export interface AuthContextInterface {

@@ -51,6 +64,9 @@ authState: AuthKitStateInterface;

}
/**
* Auth Provider Props
*/
export interface AuthProviderProps {
authType: 'cookie' | 'localstorage';
authName: string;
refreshToken?: boolean;
refresh?: createRefreshParamInterface;
cookieDomain?: string;

@@ -60,4 +76,23 @@ cookieSecure?: boolean;

}
export declare type AuthStateUserObject = {
[x: string]: any;
/**
* Refresh Token types
*/
export declare type refreshTokenCallback = (param: {
authToken?: string;
authTokenExpireAt?: Date;
refreshToken?: string;
refreshTokenExpiresAt?: Date;
authUserState: AuthStateUserObject | null;
}) => {
isSuccess: boolean;
newAuthToken: string;
newAuthTokenExpireIn?: number | null;
newRefreshToken?: string | null;
newRefreshTokenExpiresIn?: number | null;
newAuthUserState?: AuthStateUserObject | null;
};
export interface createRefreshParamInterface {
interval: number;
refreshApiCallback: refreshTokenCallback;
}
export declare type refreshFunctionType = (param: createRefreshParamInterface) => createRefreshParamInterface;

@@ -23,3 +23,4 @@ /**

SignIn = 0,
SignOut = 1
SignOut = 1,
RefreshToken = 2
}

@@ -38,2 +39,9 @@ export interface SignInActionPayload {

}
export interface RefreshTokenActionPayload {
newAuthToken: string | null;
newAuthTokenExpireIn?: number | null;
newRefreshToken?: string | null;
newRefreshTokenExpiresIn?: number | null;
newAuthUserState?: AuthStateUserObject | null;
}
export interface SignInAction {

@@ -43,5 +51,9 @@ type: ActionType.SignIn;

}
export interface RefreshTokenAction {
type: ActionType.RefreshToken;
payload: RefreshTokenActionPayload;
}
export interface SignOutAction {
type: ActionType.SignOut;
}
export declare type AuthActions = SignInAction | SignOutAction;
export declare type AuthActions = SignInAction | SignOutAction | RefreshTokenAction;

@@ -21,3 +21,3 @@ /**

import { AuthKitStateInterface } from '../types';
import { AuthActions, SignInAction, SignInActionPayload, SignOutAction } from './actions';
import { AuthActions, SignInAction, SignInActionPayload, SignOutAction, RefreshTokenAction, RefreshTokenActionPayload } from './actions';
/**

@@ -36,4 +36,9 @@ * Auth Reducer

/**
* used to refresh the Token
* @param refreshTokenParam
*/
export declare function doRefresh(refreshTokenParam: RefreshTokenActionPayload): RefreshTokenAction;
/**
* Used to make sign out
*/
export declare function doSignOut(): SignOutAction;
{
"name": "react-auth-kit",
"version": "1.4.18",
"version": "1.5.0",
"description": "Authentication Library for React, which makes Token based auth very easy",

@@ -50,22 +50,22 @@ "source": "src/index.tsx",

"devDependencies": {
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@testing-library/react": "^12.0.0",
"@types/jest": "^27.0.1",
"@types/js-cookie": "^2.2.7",
"@types/node": "^16.7.3",
"@types/react": "^17.0.19",
"@commitlint/cli": "^13.2.1",
"@commitlint/config-conventional": "^13.2.0",
"@rollup/plugin-node-resolve": "^13.0.5",
"@testing-library/react": "^12.1.2",
"@types/jest": "^27.0.2",
"@types/js-cookie": "^3.0.0",
"@types/node": "^16.11.0",
"@types/react": "^17.0.30",
"@types/react-dom": "^17.0.9",
"@types/react-router-dom": "^5.1.8",
"@typescript-eslint/eslint-plugin": "^4.29.3",
"@typescript-eslint/parser": "^4.29.3",
"@types/react-router-dom": "^5.3.1",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"codecov": "^3.8.3",
"eslint": "^7.32.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-jest": "^24.4.0",
"eslint-plugin-react": "^7.25.1",
"eslint-plugin-jest": "^25.2.1",
"eslint-plugin-react": "^7.26.1",
"husky": "^7.0.0",
"jest": "^27.1.0",
"lint-staged": "^11.1.2",
"jest": "^27.2.5",
"lint-staged": "^11.2.3",
"mkdirp": "^1.0.4",

@@ -75,24 +75,25 @@ "npm-run-all": "^4.1.5",

"rimraf": "^3.0.2",
"rollup": "^2.56.3",
"rollup": "^2.58.0",
"rollup-plugin-filesize": "^9.0.2",
"rollup-plugin-license": "^2.2.0",
"rollup-plugin-license": "^2.6.0",
"rollup-plugin-peer-deps-external": "^2.2.3",
"rollup-plugin-terser": "^7.0.0",
"rollup-plugin-typescript2": "^0.30.0",
"ts-jest": "^27.0.5",
"ts-jest": "^27.0.6",
"tslib": "^2.3.1",
"typedoc": "^0.21.7",
"typescript": "^4.3.5",
"typedoc": "^0.22.5",
"typescript": "^4.4.4",
"utility-types": "^3.10.0"
},
"dependencies": {
"js-cookie": "^3.0.0",
"@testing-library/react-hooks": "^7.0.2",
"js-cookie": "^3.0.1",
"react": ">=16.8.0",
"react-router-dom": "^5.2.0"
"react-router-dom": "^5.3.0"
},
"peerDependencies": {
"js-cookie": "^3.0.0",
"js-cookie": "^3.0.1",
"react": ">=16.8.0",
"react-dom": ">=16.8.0",
"react-router-dom": "^5.2.0"
"react-router-dom": "^5.3.0"
},

@@ -99,0 +100,0 @@ "lint-staged": {

@@ -33,3 +33,3 @@ <p align="center">

**React Auth Kit** is a lightweight Javascript library for React JS, which makes the implementation of `Token based`
**React Auth Kit** is a lightweight JavaScript library for React JS, which makes the implementation of `Token based`
authentication very easy

@@ -40,13 +40,24 @@

* 📦 Very lightweight
* ⚛️Build for React Js
* ⚛️ Build for React Js
* 🚀 Fast and easy to Implement
* 🛡️ Supports Json Web Token (JWT)
* 🔒 Secure Client Side Authentication
* ⌨️Highly typed and written in TypeScript
* ⌨️ Highly typed and written in TypeScript
## Documentation
Visit [https://authkit.arkadip.me](https://authkit.arkadip.me) for the complete documentation
Visit [Auth Kit](https://authkit.arkadip.me) for the offical documentation.
## HALL OF FAME ✨
We love our contributors! If you'd like to contribute anything from a bug-fix to a feature update, you can read our [CONTRIBUTING GUIDE](https://github.com/react-auth-kit/react-auth-kit/blob/master/CONTRIBUTING.md)
[![Contributors](https://contrib.rocks/image?repo=react-auth-kit/react-auth-kit)](https://github.com/react-auth-kit/react-auth-kit/graphs/contributors)
<p align="center">&mdash; 🔑 &mdash;</p>
<p align="center"><i>React Auth Kit is <a href="https://github.com/react-auth-kit/react-auth-kit/blob/master/LICENSE">Apache 2.0 License</a> code</i></p>

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet