Barkmaster commited on
Commit
3f017fc
·
verified ·
1 Parent(s): c800687

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +52 -0
index.html CHANGED
@@ -623,6 +623,58 @@ async function doLogin() {
623
  const btn = $('login-btn');
624
  btn.disabled = true; btn.textContent = 'AUTHENTICATING...';
625
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
626
  try {
627
  const res = await fetch(`${SB_URL}/auth/v1/token?grant_type=password`, {
628
  method:'POST',
 
623
  const btn = $('login-btn');
624
  btn.disabled = true; btn.textContent = 'AUTHENTICATING...';
625
 
626
+ try {
627
+ const res = await fetch(`${SB_URL}/auth/v1/token?grant_type=password`, {
628
+ method:'POST',
629
+ headers:{'apikey':SB_KEY,'Content-Type':'application/json'},
630
+ body:JSON.stringify({email,password:pass})
631
+ });
632
+ const data = await res.json();
633
+ if (!res.ok) throw new Error(data.error_description || data.message || 'Login failed');
634
+
635
+ const userId = data.user?.id;
636
+ // FIXED: Added fallback safety so it never crashes with 'undefined split'
637
+ let username = "Operator";
638
+ if (data.user && data.user.email) {
639
+ username = data.user.email.split('@')[0];
640
+ }
641
+
642
+ // Auto-Repair/Fetch Stats
643
+ const sr = await fetch(`${SB_URL}/rest/v1/player_stats?id=eq.${userId}&select=*`, {
644
+ headers:{'apikey':SB_KEY,'Authorization':`Bearer ${data.access_token}`}
645
+ });
646
+ const sd = await sr.json();
647
+
648
+ if (!sd || sd.length === 0) {
649
+ await fetch(`${SB_URL}/rest/v1/player_stats`, {
650
+ method:'POST',
651
+ headers:{ 'apikey':SB_KEY, 'Authorization':`Bearer ${data.access_token}`, 'Content-Type':'application/json', 'Prefer':'return=minimal' },
652
+ body:JSON.stringify({
653
+ id: userId, username: username, kills: 0, deaths: 0, rank_points: 1000,
654
+ gold: 0, level: 1, equipped_weapon: 'Pistol', equipped_armor: 'None',
655
+ headshots: 0, wins: 0, losses: 0, playtime_hours: 0
656
+ })
657
+ });
658
+ } else if (sd[0].username) {
659
+ username = sd[0].username;
660
+ }
661
+
662
+ authUser = { id:userId, email:data.user?.email, username, token:data.access_token };
663
+
664
+ if (!isBotAccount(authUser.email)) {
665
+ localStorage.setItem('warzone_session', JSON.stringify(authUser));
666
+ }
667
+
668
+ $('login-email').value=''; $('login-password').value='';
669
+ await loadData();
670
+ goto('dashboard');
671
+ } catch(e) {
672
+ setAlert('login-alert','error',`⚠ ${e.message.toUpperCase()}`);
673
+ } finally {
674
+ btn.disabled=false; btn.textContent='AUTHENTICATING...';
675
+ }
676
+ }
677
+
678
  try {
679
  const res = await fetch(`${SB_URL}/auth/v1/token?grant_type=password`, {
680
  method:'POST',