feat: add auth setup and login flow

This commit is contained in:
2026-06-15 15:56:35 +05:30
parent 134e2d1bb8
commit 9de65d0ce6
18 changed files with 664 additions and 19 deletions

View File

@@ -1,25 +1,26 @@
import { ENV_CONSTANT } from '@/constants/secrect.constant';
import { tokenService } from '@/services/token.service';
import axios from 'axios';
import { signOut } from 'next-auth/react';
const BASE_URL = ENV_CONSTANT.BASE_API_URL;
const axiosClient = axios.create({
baseURL: BASE_URL,
headers: {
// "Content-Type": "application/json",
'Content-Type': 'application/json',
'ngrok-skip-browser-warning': 'true',
},
});
// Add request interceptor to inject access token
axiosClient.interceptors.request.use(
(config) => {
if (typeof window !== 'undefined') {
const token = localStorage.getItem('token');
if (token && config.headers) {
config.headers['Authorization'] = `Bearer ${token}`;
}
async (config) => {
const token = await tokenService.getAccessToken();
if (token && config.headers) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => {
@@ -27,18 +28,23 @@ axiosClient.interceptors.request.use(
},
);
// Basic response interceptor
axiosClient.interceptors.response.use(
(response) => response,
(error) => {
// Standard error handling can be added here later
async (error) => {
if (error?.response?.status === 401 && typeof window !== 'undefined') {
await signOut({ callbackUrl: '/login' });
}
return Promise.reject(error);
},
);
export const axiosAuth = axios.create({
baseURL: BASE_URL,
headers: { 'Content-Type': 'application/json' },
headers: {
'Content-Type': 'application/json',
'ngrok-skip-browser-warning': 'true',
},
});
export default axiosClient;