File size: 3,644 Bytes
d5c104e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!DOCTYPE html>
<html>
<head>
  <title>Completing Authentication...</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
      background-color: #f5f5f5;
    }
    .container {
      text-align: center;
      padding: 20px;
      background: white;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .spinner {
      border: 3px solid #f3f3f3;
      border-top: 3px solid #3498db;
      border-radius: 50%;
      width: 40px;
      height: 40px;
      animation: spin 1s linear infinite;
      margin: 20px auto;
    }
    @keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }
  </style>
</head>
<body>
  <div class="container">
    <h2>Completing authentication...</h2>
    <div class="spinner"></div>
    <p id="status">Please wait...</p>
  </div>

  <script>
    function updateStatus(message) {
      document.getElementById('status').textContent = message;
    }

    try {
      // Extract token from URL
      let token = null;
      let authData = { type: 'auth-success' };
      
      // For Google and Microsoft (token in hash)
      if (window.location.hash) {
        const hashParams = new URLSearchParams(window.location.hash.substring(1));
        token = hashParams.get('access_token');
      }
      
      // For Slack (code in query params)
      if (!token && window.location.search) {
        const queryParams = new URLSearchParams(window.location.search);
        
        // Check if this is a Slack OAuth response (has code and state=slack)
        const code = queryParams.get('code');
        const state = queryParams.get('state');
        
        if (code && state === 'slack') {
          // This is a Slack OAuth response
          token = code; // Use the code as the token for now
          authData.code = code;
          authData.provider = 'slack';
          
          // Extract team information if available
          const team = queryParams.get('team');
          const teamId = queryParams.get('team_id');
          const teamName = queryParams.get('team_name');
          
          if (team || teamId) {
            authData.team_id = teamId || team;
            authData.team_name = teamName || team;
          }
        } else if (code && !token) {
          // Legacy Slack handling without state parameter
          updateStatus('Slack authentication detected. Using authorization code.');
          token = code;
          authData.code = code;
          authData.provider = 'slack';
        }
      }
      
      if (token) {
        updateStatus('Authentication successful! Closing window...');
        
        // Send token back to parent window
        if (window.opener) {
          window.opener.postMessage({
            ...authData,
            token: token
          }, window.location.origin);
          
          // Close window after a short delay
          setTimeout(() => window.close(), 1000);
        } else {
          updateStatus('Unable to communicate with main window. Please close this window manually.');
        }
      } else {
        updateStatus('Authentication failed');
        setTimeout(() => {
          if (window.opener) {
            window.opener.postMessage({ type: 'auth-failed', error: 'No token received' }, '*');
            window.close();
          }
        }, 3000);
      }
    } catch (error) {
      updateStatus('An error occurred: ' + error.message);
      console.error('Auth error:', error);
    }
  </script>
</body>
</html>