Files
Job-Info/NewApproach/auth/auth-universal.js
T
aewing c33ee3dfe7 Scaffold NewApproach: Full-stack Bun+Hono+TypeScript+TailwindCSS project
- Create modular project structure with src/backend, src/frontend, public
- Hono server with routing, static file serving, and API endpoints
- TailwindCSS styling pipeline with Tailwind CLI
- Vanilla JS frontend (extensible for frameworks)
- Complete TypeScript configuration and type checking
- Auth module as self-contained submodule
- Ready for development: bun run dev (port 3000)
- All dependencies installed and tested

Also:
- Lock main branch with read-only settings in VS Code
- Create .github/copilot-instructions.md with Monica persona
- Initialize session logging system

Status: Project ready for feature development
2026-01-09 13:33:22 +00:00

67 lines
1.7 KiB
JavaScript

// Universal Auth Module: Immutable, Pattern-Based, Project-Agnostic
// Usage: import and call Auth.use('pb-graph') or Auth.use('pb') or Auth.use('graph')
// Provides: getToken(type), ensureToken(type), refreshToken(type), getUserInfo(), promptReauth()
// Token types: 'pb' (PocketBase user/agent), 'graph' (Graph delegated), 'graphAgent' (Graph app-only)
// All storage, retrieval, refresh, and UI are standardized and immutable
const TOKEN_KEYS = {
'pb-user': 'pbUserToken',
'pb-agent': 'pbAgentToken',
'graph-user': 'graphUserToken',
'graph-agent': 'graphAgentToken'
};
function parsePattern(pattern) {
return pattern.split('+').map(s => s.trim()).filter(Boolean);
}
window.Auth = (() => {
let pattern = 'pb-user'; // default
let enabledTypes = ['pb-user'];
let pb = null;
let popup = null;
let agentCreds = { email: '', password: '' };
let superuserCreds = { email: '', password: '' };
// --- Setup ---
function use(type) {
pattern = type;
enabledTypes = parsePattern(type);
if (enabledTypes.some(t => t.startsWith('pb'))) {
initPocketBase();
}
return this;
}
function setPB(pbInstance) {
pb = pbInstance;
}
function initPocketBase() {
if (pb) return;
pb = new PocketBase('http://127.0.0.1:8090');
}
// --- Token Management ---
function getToken(type) {
const key = TOKEN_KEYS[type];
return key ? localStorage.getItem(key) : null;
}
function setToken(type, token) {
const key = TOKEN_KEYS[type];
if (key) {
if (token) localStorage.setItem(key, token);
else localStorage.removeItem(key);
}
}
// --- Public API ---
return {
use,
setPB,
getToken,
setToken
};
})();