Google login is working

This commit is contained in:
Michał Zieliński
2025-08-22 11:30:09 +02:00
parent 14c61ca1ee
commit 569245c296
5 changed files with 185 additions and 56 deletions

View File

@@ -1,35 +1,119 @@
window.initGoogleSignIn = async function(clientId) {
try {
if (!clientId) {
throw new Error('ClientId is required');
let googleClient = null;
let isSigningIn = false;
function waitForGoogleApi() {
return new Promise((resolve, reject) => {
if (window.google?.accounts?.oauth2) {
resolve(window.google);
return;
}
// Inicjalizacja Google Sign-In z dynamicznym ClientId
google.accounts.id.initialize({
client_id: clientId,
callback: handleGoogleResponse,
auto_select: false,
cancel_on_tap_outside: true
});
const maxAttempts = 20;
let attempts = 0;
const checkGoogle = setInterval(() => {
attempts++;
if (window.google?.accounts?.oauth2) {
clearInterval(checkGoogle);
resolve(window.google);
} else if (attempts >= maxAttempts) {
clearInterval(checkGoogle);
reject(new Error('Google OAuth2 API failed to load within the timeout period'));
}
}, 100);
});
}
// Wyświetl popup logowania
google.accounts.id.prompt((notification) => {
if (notification.isNotDisplayed() || notification.isSkippedMoment()) {
console.log('Google Sign-In popup not displayed');
async function handleAuthError(error, context = '') {
const errorMessage = error?.message || error?.type || error?.toString() || 'Unknown error';
const fullError = `${context}: ${errorMessage}`;
console.error('Google Auth Error:', { context, error, fullError });
await DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInError', fullError);
}
async function fetchUserInfo(accessToken) {
const response = await fetch('https://www.googleapis.com/oauth2/v3/userinfo', {
headers: { 'Authorization': `Bearer ${accessToken}` }
});
if (!response.ok) {
const errorText = await response.text();
console.error('Failed to fetch user info:', errorText);
await DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInError',
`Failed to fetch user info: HTTP ${response.status}`);
return null;
}
return await response.json();
}
window.initGoogleSignIn = async function(clientId) {
if (googleClient) {
return googleClient;
}
try {
const google = await waitForGoogleApi();
googleClient = google.accounts.oauth2.initTokenClient({
client_id: clientId,
scope: 'email profile',
callback: async (tokenResponse) => {
try {
if (tokenResponse.error) {
console.error('Token response error:', tokenResponse.error);
await DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInError',
tokenResponse.error);
return;
}
const userInfo = await fetchUserInfo(tokenResponse.access_token);
if (!userInfo) return;
await DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInSuccess',
tokenResponse.access_token,
userInfo.name || '',
userInfo.email || '',
userInfo.picture || ''
);
} catch (error) {
console.error('Callback error:', error);
await DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInError',
error.message || 'Unknown callback error');
} finally {
isSigningIn = false;
}
},
error_callback: async (error) => {
console.error('OAuth flow error:', error);
await DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInError',
error.type || 'OAuth flow error');
isSigningIn = false;
}
});
return googleClient;
} catch (error) {
console.error('Google Sign-In initialization error:', error);
DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInError', error.message);
console.error('Initiaxcrun xctrace list deviceslization error:', error);
await DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInError',
error.message || 'Failed to initialize Google Sign-In');
isSigningIn = false;
}
};
function handleGoogleResponse(response) {
if (response.credential) {
// Token otrzymany - wyślij do Blazor
DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInSuccess', response.credential);
} else {
DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInError', 'No credential received');
window.requestGoogleSignIn = async function() {
if (isSigningIn) {
console.log('Sign-in already in progress');
return;
}
}
if (!googleClient) {
console.error('Google Sign-In not initialized');
await DotNet.invokeMethodAsync('Bimix.UI.Shared', 'OnGoogleSignInError',
'Google Sign-In not initialized. Call initGoogleSignIn first.');
return;
}
isSigningIn = true;
googleClient.requestAccessToken();
};