Skip to content

Commit

Permalink
Merge pull request #117 from AckeeCZ/feat/7.0.0
Browse files Browse the repository at this point in the history
Feat/7.0.0
  • Loading branch information
cermakjiri authored May 5, 2023
2 parents 6a3e985 + c2d1c38 commit 216ba88
Show file tree
Hide file tree
Showing 23 changed files with 170 additions and 371 deletions.
50 changes: 0 additions & 50 deletions src/HOC/authorizable.tsx

This file was deleted.

57 changes: 25 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
export {
TokensPersistence,
setTokensPersistence,
applyAccessTokenResolve,
unapplyAccessTokenResolve,
APPLY_ACCESS_TOKEN_REQUEST,
UNAPPLY_ACCESS_TOKEN_REQUEST,
} from 'modules/tokens';

export { checkAccessTokenExpiration } from 'modules/tokens/modules/refreshment';

export { terminate, ACCESS_TOKEN_AVAILABLE, ACCESS_TOKEN_UNAVAILABLE } from './services/actions';
export {
setUserWithTokens,
LOGIN_FAILURE,
LOGIN_SUCCESS,
loginRequest,
loginReset,
logoutRequest,
LOGIN_SUCCESS,
LOGIN_FAILURE,
setUserWithTokens,
} from 'modules/auth-session';
export { RETRIEVE_TOKENS_REQUEST, RETRIEVE_TOKENS_RESOLVE } from 'modules/tokens/modules/retrieval';
export { AUTH_SESSION_END, AUTH_SESSION_PAUSE, AUTH_SESSION_RESUME, AUTH_SESSION_START } from './services/actions';

export { TokensPersistence, setTokensPersistence } from 'modules/tokens';
export { checkAccessTokenExpiration } from 'modules/tokens/modules/refreshment';
export type { RefreshTokensRequestPayload } from 'modules/tokens/modules/refreshment';
export { RETRIEVE_TOKENS_REQUEST, RETRIEVE_TOKENS_RESOLVE, retrieveTokens } from 'modules/tokens/modules/retrieval';
export * from './components/Authenticated';
export { PetrusErrorType, isPetrusError, storageDrivers } from './config';
export { configure } from './configure';
export * from './HOC/authorizable';
export { withAuthSession, getAuthStateChannel, getAccessToken } from './services/sagas';
export * from './constants';
export * from './hooks/useAuthenticated';
export { createExpirationDate } from './modules/oAuth';
export {
ACCESS_TOKEN_AVAILABLE,
ACCESS_TOKEN_UNAVAILABLE,
AUTH_SESSION_END,
AUTH_SESSION_PAUSE,
AUTH_SESSION_RESUME,
AUTH_SESSION_START,
terminate,
} from './services/actions';
export { getAccessToken, withAuthSession } from './services/sagas';
export {
entitiesSelector,
apiSelector,
tokensSelector,
accessTokenSelector,
apiSelector,
entitiesSelector,
tokensPersistenceSelector,
tokensSelector,
} from './services/selectors';
export { createExpirationDate } from './modules/oAuth';
export { storageDrivers, isPetrusError, PetrusErrorType } from './config';
export * from './constants';
export { retrieveTokens } from 'modules/tokens/modules/retrieval';

export * from './hooks/useAuthenticated';
export * from './components/Authenticated';

export * from './types';
export type { RefreshTokensRequestPayload } from 'modules/tokens/modules/refreshment';
11 changes: 4 additions & 7 deletions src/modules/auth-session/sagas/directLogin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { put, takeLeading, take } from 'redux-saga/effects';
import { put, take, takeLeading } from 'redux-saga/effects';

import { config, isPetrusError, PetrusError, PetrusErrorType } from 'config';
import { setTokens } from 'services/actions';
import { applyAccessTokenExternally } from 'modules/tokens/modules/external';

import { login, logout, setUserWithTokens, fetchUser } from '../actions';
import { loginSelector } from '../selectors';
import { appSelect } from 'services/utils/reduxSaga';
import { validateTokens } from 'services/utils';
import { appSelect } from 'services/utils/reduxSaga';
import { fetchUser, login, logout, setUserWithTokens } from '../actions';
import { loginSelector } from '../selectors';

export default function* directLogin() {
yield takeLeading(setUserWithTokens, function* (action) {
Expand All @@ -26,8 +25,6 @@ export default function* directLogin() {
yield put(fetchUser.success(user));
yield put(setTokens(tokens));

yield* applyAccessTokenExternally(tokens);

yield put(login.success());
} catch (e) {
if (isPetrusError(e)) {
Expand Down
4 changes: 1 addition & 3 deletions src/modules/auth-session/sagas/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { put, takeLeading } from 'redux-saga/effects';
import { config, isPetrusError, PetrusError, PetrusErrorType } from 'config';
import { setTokens } from 'services/actions';
import { validateTokens } from 'services/utils';
import { applyAccessTokenExternally } from 'modules/tokens/modules/external';

import { fetchUser, login } from '../actions';
import type { PetrusTokens, PetrusUser } from 'types';
import { fetchUser, login } from '../actions';

function* resolveAuthUser(tokens: PetrusTokens, user?: PetrusUser): Generator<any, PetrusUser> {
return user ? user : yield config.handlers.getAuthUser(tokens);
Expand Down Expand Up @@ -34,7 +33,6 @@ export default function* loginHandler() {

validateTokens(tokens);
yield put(setTokens(tokens));
yield* applyAccessTokenExternally(tokens);

const authUser = yield* resolveAuthUser(tokens, user);
yield put(fetchUser.success(authUser));
Expand Down
1 change: 0 additions & 1 deletion src/modules/tokens/configure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const handlers = ({
};

export const options = (customOptions: PetrusCustomConfig['tokens']): PetrusConfig['tokens'] => ({
applyAccessTokenExternally: false,
autoStartTokensRetrieval: true,
...refreshmentConfig.options,
...customOptions,
Expand Down
6 changes: 2 additions & 4 deletions src/modules/tokens/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as configure from './configure';

export { configure };
export { TokensPersistence, setTokensPersistence } from './modules/storage';
export * from './services/reducers/entities';
export { default as saga } from './services/sagas';
export * from './modules/external';

export { TokensPersistence, setTokensPersistence } from './modules/storage';
export { configure };
55 changes: 0 additions & 55 deletions src/modules/tokens/modules/external/actions/applyAccessToken.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/modules/tokens/modules/external/actions/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/modules/tokens/modules/external/index.ts

This file was deleted.

25 changes: 0 additions & 25 deletions src/modules/tokens/modules/external/sagas/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/modules/tokens/modules/refreshment/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ type TokensRefreshmentOptions = {

export const options: TokensRefreshmentOptions = {
// refresh tokens 0.5s before token expires
requestDurationEstimate: 500, // 0.5s
minRequiredExpiration: 1000 * 60, // 1m
requestDurationEstimate: 2 * 60 * 100, // 2m,
minRequiredExpiration: 2 * 60 * 1000, // 2m

checkTokenExpirationOnTabFocus: true,
} as const;
6 changes: 3 additions & 3 deletions src/modules/tokens/modules/refreshment/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as config from './config';

export { config };
export * from './actions';
export * from './reducers/api';
export { default as saga } from './sagas';
export * from './actions';
export { refreshTokensTask } from './sagas/refreshTokens';
export { isTokenExpired } from './sagas/utils';
export { refreshExpiredToken } from './sagas/checkAccessTokenExpiration';
export { config };
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { takeEvery, put } from 'redux-saga/effects';
import { put, takeEvery } from 'redux-saga/effects';

import { entitiesSelector } from 'services/selectors';
import { appSelect } from 'services/utils/reduxSaga';

import { refreshTokens, checkAccessTokenExpiration } from '../actions';
import { checkAccessTokenExpiration, refreshTokens } from '../actions';
import { isTokenExpired } from './utils';

export function* refreshExpiredToken() {
function* refreshExpiredToken() {
const { tokens } = yield* appSelect(entitiesSelector);

if (tokens && isTokenExpired(tokens.accessToken)) {
Expand Down
41 changes: 20 additions & 21 deletions src/modules/tokens/modules/refreshment/sagas/refreshTokens.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
import { put, takeLeading } from 'redux-saga/effects';

import { config, isPetrusError, PetrusError, PetrusErrorType } from 'config';
import { setTokens, deleteTokens } from 'services/actions';
import { deleteTokens, setTokens } from 'services/actions';
import { tokensSelector } from 'services/selectors';
import { validateTokens } from 'services/utils';
import { applyAccessTokenExternally } from 'modules/tokens/modules/external';

import { refreshTokens } from '../actions';
import { appSelect } from 'services/utils/reduxSaga';
import type { PetrusTokens } from 'types';
import { refreshTokens, RefreshTokensRequestPayload } from '../actions';

export function* refreshTokensTask(currentTokens?: RefreshTokensRequestPayload) {
const tokens = currentTokens ? currentTokens : yield* appSelect(tokensSelector);

if (!tokens?.refreshToken) {
throw new PetrusError(
PetrusErrorType.UNAVAILABLE_TOKENS,
`Can't refresh access token without a refresh token. Received 'tokens': ${JSON.stringify(tokens, null, 2)}`,
);
}

const refreshedTokens: PetrusTokens = yield config.handlers.refreshTokens(tokens as Required<PetrusTokens>);

validateTokens(refreshedTokens);

return refreshedTokens;
}

export default function* refreshTokensHandler() {
yield takeLeading(refreshTokens.request, function* (action) {
try {
const tokens = action.payload ? action.payload : yield* appSelect(tokensSelector);

if (!tokens?.refreshToken) {
throw new PetrusError(
PetrusErrorType.UNAVAILABLE_TOKENS,
`Can't refresh access token without a refresh token. Received 'tokens': ${JSON.stringify(
tokens,
null,
2,
)}`,
);
}

const refreshedTokens: PetrusTokens = yield config.handlers.refreshTokens(tokens as Required<PetrusTokens>);

validateTokens(refreshedTokens);
const refreshedTokens = yield* refreshTokensTask(action.payload);

yield put(setTokens(refreshedTokens));

yield* applyAccessTokenExternally(refreshedTokens);

yield put(refreshTokens.success());
} catch (e) {
if (isPetrusError(e)) {
Expand Down
Loading

0 comments on commit 216ba88

Please sign in to comment.