// Direct authentication using bearer token from environment
// Bypasses Puppeteer for faster, more reliable auth
import { SessionInfo } from './types.js';
export class DirectAuth {
private bearerToken: string | null = null;
private sessionInfo: SessionInfo = { isAuthenticated: false };
private sessionTimeout: number;
constructor() {
this.sessionTimeout = 3600000; // 1 hour
const envToken = process.env.SUPERSTORE_BEARER_TOKEN;
if (envToken && envToken.startsWith('eyJ')) {
this.bearerToken = envToken;
this.sessionInfo = {
isAuthenticated: true,
sessionExpiry: new Date(Date.now() + this.sessionTimeout),
lastActivity: new Date()
};
console.log('Direct auth initialized with bearer token from environment');
}
}
async isAuthenticated(): Promise<boolean> {
if (!this.bearerToken) return false;
// Check if session has expired
if (this.sessionInfo.sessionExpiry && new Date() > this.sessionInfo.sessionExpiry) {
console.log('Bearer token session expired');
return false;
}
return this.sessionInfo.isAuthenticated;
}
getAuthToken(): string | null {
return this.bearerToken;
}
async getCookieJarLike(): Promise<any[]> {
// No cookies needed when using bearer token directly
return [];
}
async getCookies(): Promise<any[]> {
return [];
}
getSessionInfo(): SessionInfo {
return { ...this.sessionInfo };
}
async refreshSession(): Promise<boolean> {
// For bearer token mode, user must update env var when token expires
return this.sessionInfo.isAuthenticated;
}
async login(): Promise<boolean> {
// Already authenticated via env token
return this.sessionInfo.isAuthenticated;
}
async logout(): Promise<void> {
this.sessionInfo = { isAuthenticated: false };
}
async cleanup(): Promise<void> {
await this.logout();
}
}