import { auth, provider, signInWithPopup, signOut, onAuthStateChanged, signInWithEmailAndPassword, createUserWithEmailAndPassword, sendPasswordResetEmail } from '/js/firebase-auth.js'; let manualLoginDone = false; const OVERLAY_HTML = `
GAME ACCOUNT SHOP

古明地shop

ゲームアカウントの売買は
安心・安全な古明地shopで

✓ 石垢販売 ✓ 進行垢販売 ✓ フォロ爆
こいし

SIGN IN

古明地shopにログインする

または
アカウントがない方は 新規登録
パスワードを忘れた方
`; function showError(id, msg) { const el = document.getElementById(id); el.textContent = msg; el.style.display = msg ? 'block' : 'none'; } function jpError(code) { const map = { 'auth/invalid-email': 'メールアドレスの形式が正しくありません', 'auth/user-not-found': 'このメールアドレスは登録されていません', 'auth/wrong-password': 'パスワードが正しくありません', 'auth/invalid-credential': 'メールアドレスまたはパスワードが正しくありません', 'auth/email-already-in-use': 'このメールアドレスはすでに登録されています', 'auth/weak-password': 'パスワードは6文字以上にしてください', 'auth/too-many-requests': 'しばらく時間をおいてから再試行してください', 'auth/popup-closed-by-user': '', }; return map[code] || 'エラーが発生しました。もう一度お試しください。'; } function attachGate() { // Google document.getElementById('agGoogleBtn').addEventListener('click', async () => { try { manualLoginDone = true; await signInWithPopup(auth, provider); } catch (e) { manualLoginDone = false; const msg = jpError(e.code); if (msg) showError('agError', msg); } }); // Email login document.getElementById('agLoginBtn').addEventListener('click', async () => { const email = document.getElementById('agEmail').value.trim(); const pass = document.getElementById('agPassword').value; showError('agError', ''); if (!email || !pass) { showError('agError', 'メールアドレスとパスワードを入力してください'); return; } try { manualLoginDone = true; await signInWithEmailAndPassword(auth, email, pass); } catch (e) { manualLoginDone = false; showError('agError', jpError(e.code)); } }); // Enter key document.getElementById('agPassword').addEventListener('keydown', e => { if (e.key === 'Enter') document.getElementById('agLoginBtn').click(); }); // Register document.getElementById('agRegisterBtn').addEventListener('click', async () => { const email = document.getElementById('agRegEmail').value.trim(); const pass = document.getElementById('agRegPassword').value; showError('agRegError', ''); if (!email || !pass) { showError('agRegError', 'メールアドレスとパスワードを入力してください'); return; } try { manualLoginDone = true; await createUserWithEmailAndPassword(auth, email, pass); } catch (e) { manualLoginDone = false; showError('agRegError', jpError(e.code)); } }); // Switch to register document.getElementById('agShowRegister').addEventListener('click', e => { e.preventDefault(); document.getElementById('agLoginForm').style.display = 'none'; document.getElementById('agRegisterForm').style.display = 'block'; document.getElementById('agToRegister').style.display = 'none'; document.getElementById('agToLogin').style.display = 'inline'; }); // Switch to login document.getElementById('agShowLogin').addEventListener('click', e => { e.preventDefault(); document.getElementById('agRegisterForm').style.display = 'none'; document.getElementById('agLoginForm').style.display = 'block'; document.getElementById('agToLogin').style.display = 'none'; document.getElementById('agToRegister').style.display = 'inline'; }); // Forgot password document.getElementById('agForgotPw').addEventListener('click', async e => { e.preventDefault(); const email = document.getElementById('agEmail').value.trim(); if (!email) { showError('agError', 'メールアドレスを入力してからクリックしてください'); return; } try { await sendPasswordResetEmail(auth, email); showError('agError', ''); alert(`${email} にパスワードリセットメールを送信しました`); } catch (e2) { showError('agError', jpError(e2.code)); } }); } // ログイン済みかをセッションフラグで確認(同一セッション内) const wasLoggedIn = sessionStorage.getItem('komeizi_authed') === '1'; if (!wasLoggedIn) { document.documentElement.style.visibility = 'hidden'; } document.addEventListener('DOMContentLoaded', () => { if (!wasLoggedIn) { document.body.insertAdjacentHTML('afterbegin', OVERLAY_HTML); document.documentElement.style.visibility = ''; attachGate(); } }); const BAN_HTML = `
🚫

アクセスが制限されています

このアカウントはサイトへのアクセスが制限されています。
心当たりがある場合はお問い合わせください。

`; onAuthStateChanged(auth, async (user) => { document.documentElement.style.visibility = ''; const gate = document.getElementById('authGate'); if (user) { // Firebaseの自動ログイン(IndexedDB)をブロック — 手動ログインのみ許可 if (!manualLoginDone && sessionStorage.getItem('komeizi_authed') !== '1') { await signOut(auth); return; } try { const token = await user.getIdToken(); const res = await fetch('https://komeizi-shop.onrender.com/api/auth/check', { method: 'POST', headers: { 'Authorization': 'Bearer ' + token } }); const data = await res.json(); if (data.banned) { sessionStorage.removeItem('komeizi_authed'); document.body.insertAdjacentHTML('afterbegin', BAN_HTML); return; } } catch {} sessionStorage.setItem('komeizi_authed', '1'); if (gate) gate.remove(); } else { sessionStorage.removeItem('komeizi_authed'); if (!gate && document.body) { document.body.insertAdjacentHTML('afterbegin', OVERLAY_HTML); attachGate(); } } });