Skip to content

Commit

Permalink
optimize auth lib
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Jan 5, 2024
1 parent b485e20 commit a09d06d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
1 change: 1 addition & 0 deletions lib/js/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare module 'axios' {
export interface AxiosRequestConfig {
meta?: RequestMeta;
errorHandled?: boolean;
handledErrorStatuses?: number[];
}
}

Expand Down
8 changes: 8 additions & 0 deletions lib/js/api/src/useRequestErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export default function useRequestErrorHandler({
const status = error.response?.status;
const data = error.response?.data;

const handledStatuses = error.config?.handledErrorStatuses;
if (handledStatuses && handledStatuses.length > 0
&& status
&& handledStatuses.includes(status)
) {
return;
}

switch (status) {
case 401:
toast.error(
Expand Down
26 changes: 10 additions & 16 deletions lib/js/auth/src/client/OAuthClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,7 @@ export default class OAuthClient<UIR extends UserInfoResponse> {

const {tokens} = res;

this.persistTokens(tokens);

this.handleSessionTimeout(tokens);

await this.triggerEvent<LoginEvent>(loginEventType, {
tokens,
});
await this.triggerLogin(tokens);

return res;
}
Expand Down Expand Up @@ -225,24 +219,24 @@ export default class OAuthClient<UIR extends UserInfoResponse> {

const {tokens} = res;

await this.triggerLogin(tokens);

return res;
}

public async triggerLogin(tokens: AuthTokens): Promise<void> {
this.handleSessionTimeout(tokens);

await this.triggerEvent<RefreshTokenEvent>(loginEventType, {
await this.triggerEvent<LoginEvent>(loginEventType, {
tokens,
});

return res;
}

async getTokenFromCustomGrantType(data: Record<string, any> = {}): Promise<TokenResponseWithTokens> {
const res = await this.getToken(data);
const {tokens} = res;

this.handleSessionTimeout(tokens);

await this.triggerEvent<RefreshTokenEvent>(loginEventType, {
tokens,
});
await this.triggerLogin(tokens);

return res;
}
Expand Down Expand Up @@ -303,7 +297,7 @@ export default class OAuthClient<UIR extends UserInfoResponse> {
await Promise.all(orderedListeners.map(({h}) => !e.stopPropagation && h(e)).filter(f => !!f));
}

public handleSessionTimeout(tokens: AuthTokens): void {
private handleSessionTimeout(tokens: AuthTokens): void {
this.clearSessionTimeout();

if (tokens.refreshExpiresIn) {
Expand Down
4 changes: 3 additions & 1 deletion lib/js/react-auth/src/components/SessionExpireContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export default function SessionExpireContainer({}: Props) {
const [displayExpire, setDisplayExpire] = React.useState(false);

const displayExpireModal = React.useCallback(() => {
setDisplayExpire(true);
if (tokens) {
setDisplayExpire(true);
}
}, [tokens]);

const onClose = React.useCallback(() => {
Expand Down

0 comments on commit a09d06d

Please sign in to comment.