115 lines
3.9 KiB
JavaScript
115 lines
3.9 KiB
JavaScript
(function () {
|
|
var msgEl = document.getElementById('gcb-msg');
|
|
var errEl = document.getElementById('gcb-err');
|
|
var spinEl = document.getElementById('gcb-spin');
|
|
var backBtn = document.getElementById('gcb-back');
|
|
|
|
function loginHref() {
|
|
return typeof appPath === 'function' ? appPath('/Login/') : '/Login/';
|
|
}
|
|
|
|
function lobbyHref() {
|
|
return typeof appPath === 'function' ? appPath('/Main-Lobby/') : '/Main-Lobby/';
|
|
}
|
|
|
|
function apiUrl(path) {
|
|
return (typeof appPath === 'function' ? appPath(path) : path);
|
|
}
|
|
|
|
function showError(text) {
|
|
if (spinEl) spinEl.style.display = 'none';
|
|
if (msgEl) msgEl.textContent = 'เข้าสู่ระบบไม่สำเร็จ';
|
|
if (errEl) {
|
|
errEl.textContent = text || 'เกิดข้อผิดพลาด';
|
|
errEl.classList.remove('is-hidden');
|
|
}
|
|
if (backBtn) backBtn.classList.remove('is-hidden');
|
|
}
|
|
|
|
function finishLogin(data) {
|
|
try {
|
|
localStorage.setItem('isLoggedIn', 'true');
|
|
localStorage.setItem('loginType', 'google');
|
|
localStorage.setItem('jdPlayerKey', data.playerKey);
|
|
localStorage.setItem('jdGoogleSessionActive', '1');
|
|
sessionStorage.removeItem('jdGuestModeActive');
|
|
if (data.displayName) {
|
|
localStorage.setItem('roomLobbyDisplayName', data.displayName);
|
|
localStorage.setItem('playerName', data.displayName);
|
|
}
|
|
if (data.email) {
|
|
localStorage.setItem('googleEmail', data.email);
|
|
}
|
|
if (data.avatarUrl) {
|
|
localStorage.setItem('googleAvatarUrl', data.avatarUrl);
|
|
}
|
|
if (data.coins != null) {
|
|
localStorage.setItem('jdCoins', String(Math.max(0, parseInt(data.coins, 10) || 0)));
|
|
}
|
|
sessionStorage.removeItem('oauthGoogleState');
|
|
sessionStorage.removeItem('oauthGoogleMergeGuestKey');
|
|
} catch (e) { /* ignore */ }
|
|
if (typeof jdTrackPlayerLog === 'function') {
|
|
jdTrackPlayerLog('login_google', { email: data.email || '' });
|
|
}
|
|
window.location.replace(lobbyHref());
|
|
}
|
|
|
|
if (backBtn) {
|
|
backBtn.addEventListener('click', function () {
|
|
window.location.href = loginHref();
|
|
});
|
|
}
|
|
|
|
var params = new URLSearchParams(window.location.search);
|
|
var oauthErr = params.get('error');
|
|
if (oauthErr) {
|
|
showError('Google: ' + oauthErr);
|
|
return;
|
|
}
|
|
|
|
var code = params.get('code');
|
|
var state = params.get('state') || '';
|
|
if (!code) {
|
|
showError('ไม่พบ authorization code จาก Google');
|
|
return;
|
|
}
|
|
|
|
var savedState = '';
|
|
try { savedState = sessionStorage.getItem('oauthGoogleState') || ''; } catch (e) { /* ignore */ }
|
|
if (!savedState || savedState !== state) {
|
|
showError('state ไม่ตรง (อาจหมดอายุหรือเปิดลิงก์ซ้ำ) — ลอง Login ใหม่');
|
|
return;
|
|
}
|
|
|
|
var mergeGuestKey = '';
|
|
try { mergeGuestKey = sessionStorage.getItem('oauthGoogleMergeGuestKey') || ''; } catch (e2) { /* ignore */ }
|
|
|
|
var redirectUri = window.location.origin + (typeof appPath === 'function'
|
|
? appPath('/Login/google-callback.html')
|
|
: '/Login/google-callback.html');
|
|
|
|
fetch(apiUrl('/Admin/api/oauth-google-callback.php'), {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
credentials: 'omit',
|
|
body: JSON.stringify({
|
|
code: code,
|
|
redirectUri: redirectUri,
|
|
state: state,
|
|
mergeGuestKey: mergeGuestKey || undefined,
|
|
}),
|
|
})
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (j) {
|
|
if (!j || !j.ok) {
|
|
showError((j && j.error) || 'เซิร์ฟเวอร์ปฏิเสธการเข้าสู่ระบบ');
|
|
return;
|
|
}
|
|
finishLogin(j);
|
|
})
|
|
.catch(function () {
|
|
showError('เชื่อมต่อเซิร์ฟเวอร์ไม่ได้');
|
|
});
|
|
})();
|