Skip to content

Commit

Permalink
chore: ⚡ update axios options
Browse files Browse the repository at this point in the history
  • Loading branch information
lehuygiang28 committed Jun 14, 2024
1 parent 175aa09 commit 86f9127
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
13 changes: 10 additions & 3 deletions apps/fe/src/hooks/useAxiosAuth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { useSession } from 'next-auth/react';
import { useEffect } from 'react';
import { useRefreshToken } from './useRefreshToken';
import axiosInstance from '~/libs/axios';
import { axiosInstance } from '~/libs/axios';

export function useAxiosAuth() {
export type UseAxiosAuthPayload = {
baseURL?: string;
};

export function useAxiosAuth({ baseURL }: UseAxiosAuthPayload) {
const { data: session, status } = useSession();
const refreshToken = useRefreshToken();

Expand All @@ -12,6 +16,9 @@ export function useAxiosAuth() {
return;
}

if (!axiosInstance.defaults?.baseURL) {
axiosInstance.defaults.baseURL = baseURL ?? process.env.NEXT_PUBLIC_API_URL;
}
const requestIntercept = axiosInstance.interceptors.request.use(
(config) => {
if (!config.headers['Authorization']) {
Expand Down Expand Up @@ -40,7 +47,7 @@ export function useAxiosAuth() {
axiosInstance.interceptors.request.eject(requestIntercept);
axiosInstance.interceptors.response.eject(responseIntercept);
};
}, [session, refreshToken, status]);
}, [session, refreshToken, status, baseURL]);

return axiosInstance;
}
8 changes: 4 additions & 4 deletions apps/fe/src/libs/next-auth/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import GithubProvider from 'next-auth/providers/github';
import CredentialsProvider from 'next-auth/providers/credentials';
import { AuthValidatePasswordlessDto, LoginResponseDto } from '~be/app/auth/dtos';
import { AxiosError } from 'axios';
import axios from '../axios';
import { axiosInstance } from '~/libs/axios';

export const authOptions = {
providers: [
Expand All @@ -28,7 +28,7 @@ export const authOptions = {
hash: credentials?.hash || '',
};

return axios
return axiosInstance
.post<LoginResponseDto>(path, payload)
.then((response) => {
return response.data as unknown as User;
Expand All @@ -44,7 +44,7 @@ export const authOptions = {
async signIn({ user, account }: { user: User; account: Account | null }) {
if (account?.provider === 'google') {
try {
const { data: userData } = await axios.post<LoginResponseDto>(
const { data: userData } = await axiosInstance.post<LoginResponseDto>(
'/auth/login/google',
{
idToken: account.id_token,
Expand All @@ -71,7 +71,7 @@ export const authOptions = {
}
} else if (account?.provider === 'github') {
try {
const { data: userData } = await axios.post<LoginResponseDto>(
const { data: userData } = await axiosInstance.post<LoginResponseDto>(
'/auth/login/github',
{
accessToken: account.access_token,
Expand Down

0 comments on commit 86f9127

Please sign in to comment.