File size: 982 Bytes
82a25a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ea509c
 
 
 
82a25a8
 
 
 
 
 
 
beb4929
82a25a8
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

import axios from "axios";

const TOKEN_KEY = "pdfflow_token";

export const api = axios.create({
  baseURL: process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000",
});

// Attach JWT token to every request
api.interceptors.request.use((config) => {
  if (typeof window !== "undefined") {
    const token = localStorage.getItem(TOKEN_KEY);
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
  }
  return config;
});

// Handle 401 responses — clear stale token only.
// No-login model: tools work without auth, so a 401 must NOT force a redirect
// to signin. We just drop any stale/expired token and let the caller handle
// the error inline (e.g. an account-only page can prompt to sign in).
api.interceptors.response.use(
  (response) => response,
  (error) => {
    if (
      error.response?.status === 401 &&
      typeof window !== "undefined"
    ) {
      localStorage.removeItem(TOKEN_KEY);
    }
    return Promise.reject(error);
  }
);