Skip to content

Commit

Permalink
feat :: refresh #29
Browse files Browse the repository at this point in the history
  • Loading branch information
wjzlskxk committed Dec 8, 2024
1 parent 6827d27 commit 492081e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/lib/axios/responseHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { AxiosError } from "axios";
import {
ACCESS_TOKEN_KEY,
REFRESH_TOKEN_KEY,
} from "../../constants/token/token.constant";
import AuthRepositoryImpl from "../../repositories/auth/AuthRepositoryImpl";
import token from "../token/token";

export const responseHandler = async (error: AxiosError) => {
if (error.response) {
const {
response: { status },
} = error;

const usingAccessToken = token.getToken(ACCESS_TOKEN_KEY);
const usingRefreshToken = token.getToken(REFRESH_TOKEN_KEY);

if (
usingAccessToken !== undefined &&
usingRefreshToken !== undefined &&
status === 401
) {
try {
const { data: newAccessToken } = await AuthRepositoryImpl.refresh(
usingAccessToken
);
token.setToken(ACCESS_TOKEN_KEY, newAccessToken.accessToken);
} catch (error) {}
}
}
};
1 change: 1 addition & 0 deletions src/repositories/auth/AuthRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TokenResponse } from "../../types/token/Token.type";

export interface AuthRepository {
login({ code }: LoginParam): Promise<TokenResponse>;
refresh(accessToken: string): Promise<{ data: { accessToken: string } }>;
}

export interface LoginParam {
Expand Down
9 changes: 9 additions & 0 deletions src/repositories/auth/AuthRepositoryImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ class AuthRepositoryImpl implements AuthRepository {
const { data } = await axios.post(`${config.SERVER}/auth/login`, { code });
return data;
}

public async refresh(
accessToken: string
): Promise<{ data: { accessToken: string } }> {
const { data } = await axios.post(`${config.SERVER}/auth/refresh`, {
accessToken,
});
return data;
}
}

export default new AuthRepositoryImpl();

0 comments on commit 492081e

Please sign in to comment.