Lashtw commited on
Commit
34e0dbf
·
verified ·
1 Parent(s): d0841e1

Upload 10 files

Browse files
Files changed (2) hide show
  1. src/services/auth.js +15 -1
  2. src/views/InstructorView.js +11 -3
src/services/auth.js CHANGED
@@ -1,5 +1,5 @@
1
  import { auth, googleProvider, db } from "./firebase.js";
2
- import { signInWithPopup, signOut } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-auth.js";
3
  import {
4
  doc,
5
  getDoc,
@@ -28,6 +28,20 @@ export async function signInWithGoogle() {
28
  }
29
  }
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  /**
32
  * Sign out
33
  */
 
1
  import { auth, googleProvider, db } from "./firebase.js";
2
+ import { signInWithPopup, signInWithRedirect, signOut } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-auth.js";
3
  import {
4
  doc,
5
  getDoc,
 
28
  }
29
  }
30
 
31
+ /**
32
+ * Sign in with Google (Redirect Mode)
33
+ * Use this if popup is blocked
34
+ */
35
+ export async function signInWithGoogleRedirect() {
36
+ try {
37
+ await signInWithRedirect(auth, googleProvider);
38
+ // Does not return value, page will redirect
39
+ } catch (error) {
40
+ console.error("Google Redirect Error:", error);
41
+ throw error;
42
+ }
43
+ }
44
+
45
  /**
46
  * Sign out
47
  */
src/views/InstructorView.js CHANGED
@@ -1,5 +1,5 @@
1
  import { createRoom, subscribeToRoom, getChallenges, resetProgress, removeUser } from "../services/classroom.js";
2
- import { signInWithGoogle, signOutUser, checkInstructorPermission, getInstructors, addInstructor, updateInstructor, removeInstructor } from "../services/auth.js";
3
  import { generateMonsterSVG, getNextMonster, MONSTER_DEFS } from "../utils/monsterUtils.js";
4
 
5
  // Load html-to-image dynamically (Better support than html2canvas)
@@ -384,8 +384,16 @@ export function setupInstructorEvents() {
384
  console.error(error);
385
  let msg = error.message;
386
  if (error.code === 'auth/popup-blocked') {
387
- msg = "瀏覽器攔截了登入視窗。請允許此網站的彈出視窗,或不要在點擊後移動滑鼠。";
388
- alert(msg);
 
 
 
 
 
 
 
 
389
  }
390
  if (error.code === 'auth/operation-not-allowed' || error.message.includes('invalid')) {
391
  msg = "Google 登入功能未啟用。請前往 Firebase Console > Authentication開啟 Google Sign-In。";
 
1
  import { createRoom, subscribeToRoom, getChallenges, resetProgress, removeUser } from "../services/classroom.js";
2
+ import { signInWithGoogle, signInWithGoogleRedirect, signOutUser, checkInstructorPermission, getInstructors, addInstructor, updateInstructor, removeInstructor } from "../services/auth.js";
3
  import { generateMonsterSVG, getNextMonster, MONSTER_DEFS } from "../utils/monsterUtils.js";
4
 
5
  // Load html-to-image dynamically (Better support than html2canvas)
 
384
  console.error(error);
385
  let msg = error.message;
386
  if (error.code === 'auth/popup-blocked') {
387
+ msg = "瀏覽器攔截了登入視窗。";
388
+ // Show Redirect Option
389
+ if (confirm("瀏覽器攔截了彈出視窗。是否改用頁面跳轉方式登入?\n(將會重新整理頁面)")) {
390
+ try {
391
+ await signInWithGoogleRedirect();
392
+ } catch (err) {
393
+ alert("跳轉失敗: " + err.message);
394
+ }
395
+ return;
396
+ }
397
  }
398
  if (error.code === 'auth/operation-not-allowed' || error.message.includes('invalid')) {
399
  msg = "Google 登入功能未啟用。請前往 Firebase Console > Authentication開啟 Google Sign-In。";