106 lines
2.5 KiB
TypeScript
106 lines
2.5 KiB
TypeScript
// ============================================================================
|
|
// Auth API
|
|
// ============================================================================
|
|
|
|
import { api, saveAuthTokens, clearAuthTokens } from "./client";
|
|
import { STORAGE_KEYS } from "@/lib/utils/constants";
|
|
import type {
|
|
User,
|
|
AuthInitResponse,
|
|
AuthCompleteRequest,
|
|
AuthCompleteResponse,
|
|
AuthRefreshRequest,
|
|
AuthRefreshResponse,
|
|
} from "@/lib/types/api";
|
|
|
|
export const authApi = {
|
|
/**
|
|
* Initialize Telegram auth flow
|
|
*/
|
|
init: () => api.get<AuthInitResponse>("/auth/init", { requireAuth: false }),
|
|
|
|
/**
|
|
* Complete auth with one-time token from bot
|
|
*/
|
|
complete: async (token: string): Promise<{ access_token: string }> => {
|
|
// GET request with token as query parameter
|
|
const response = await api.get<{ access_token: string }>(
|
|
`/auth/complete?token=${token}`,
|
|
{
|
|
requireAuth: false,
|
|
}
|
|
);
|
|
|
|
// Save access token to storage
|
|
if (typeof window !== "undefined") {
|
|
localStorage.setItem(STORAGE_KEYS.ACCESS_TOKEN, response.access_token);
|
|
}
|
|
|
|
return response;
|
|
},
|
|
|
|
/**
|
|
* Refresh access token
|
|
*/
|
|
refresh: async (): Promise<string> => {
|
|
if (typeof window === "undefined") {
|
|
throw new Error("Cannot refresh token on server");
|
|
}
|
|
|
|
const refreshToken = localStorage.getItem(STORAGE_KEYS.REFRESH_TOKEN);
|
|
if (!refreshToken) {
|
|
throw new Error("No refresh token available");
|
|
}
|
|
|
|
const data: AuthRefreshRequest = { refresh_token: refreshToken };
|
|
const response = await api.post<AuthRefreshResponse>(
|
|
"/auth/refresh",
|
|
data,
|
|
{
|
|
requireAuth: false,
|
|
}
|
|
);
|
|
|
|
// Update access token
|
|
localStorage.setItem(STORAGE_KEYS.ACCESS_TOKEN, response.access_token);
|
|
|
|
return response.access_token;
|
|
},
|
|
|
|
/**
|
|
* Get current user info
|
|
*/
|
|
me: () => api.get<User>("/auth/me"),
|
|
|
|
/**
|
|
* Logout (clear tokens)
|
|
*/
|
|
logout: () => {
|
|
clearAuthTokens();
|
|
},
|
|
|
|
/**
|
|
* Get user from storage (no API call)
|
|
*/
|
|
getUserFromStorage: (): User | null => {
|
|
if (typeof window === "undefined") return null;
|
|
|
|
const userJson = localStorage.getItem(STORAGE_KEYS.USER);
|
|
if (!userJson) return null;
|
|
|
|
try {
|
|
return JSON.parse(userJson);
|
|
} catch {
|
|
return null;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Check if user is authenticated
|
|
*/
|
|
isAuthenticated: (): boolean => {
|
|
if (typeof window === "undefined") return false;
|
|
return !!localStorage.getItem(STORAGE_KEYS.ACCESS_TOKEN);
|
|
},
|
|
};
|