📝 Reformat code for improved readability and consistency in the OAuth composable
This commit is contained in:
@@ -118,8 +118,12 @@ export function useOAuth(provider = 'challonge') {
|
|||||||
* @throws {Error} If OAuth credentials not configured
|
* @throws {Error} If OAuth credentials not configured
|
||||||
*/
|
*/
|
||||||
function getAuthorizationUrl(scopeOrOptions, options = {}) {
|
function getAuthorizationUrl(scopeOrOptions, options = {}) {
|
||||||
const clientId = import.meta.env[`VITE_${provider.toUpperCase()}_CLIENT_ID`];
|
const clientId = import.meta.env[
|
||||||
const redirectUri = import.meta.env[`VITE_${provider.toUpperCase()}_REDIRECT_URI`];
|
`VITE_${provider.toUpperCase()}_CLIENT_ID`
|
||||||
|
];
|
||||||
|
const redirectUri = import.meta.env[
|
||||||
|
`VITE_${provider.toUpperCase()}_REDIRECT_URI`
|
||||||
|
];
|
||||||
|
|
||||||
if (!clientId || !redirectUri) {
|
if (!clientId || !redirectUri) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@@ -182,7 +186,10 @@ export function useOAuth(provider = 'challonge') {
|
|||||||
sessionStorage.setItem('oauth_return_to', returnTo);
|
sessionStorage.setItem('oauth_return_to', returnTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`🔐 Starting ${provider} OAuth flow with state:`, state.substring(0, 8) + '...');
|
console.log(
|
||||||
|
`🔐 Starting ${provider} OAuth flow with state:`,
|
||||||
|
state.substring(0, 8) + '...'
|
||||||
|
);
|
||||||
|
|
||||||
// Redirect to OAuth provider
|
// Redirect to OAuth provider
|
||||||
window.location.href = authUrl;
|
window.location.href = authUrl;
|
||||||
@@ -214,7 +221,9 @@ export function useOAuth(provider = 'challonge') {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (storedProvider !== provider) {
|
if (storedProvider !== provider) {
|
||||||
const err = new Error(`Provider mismatch: expected ${storedProvider}, got ${provider}`);
|
const err = new Error(
|
||||||
|
`Provider mismatch: expected ${storedProvider}, got ${provider}`
|
||||||
|
);
|
||||||
state.error.value = err.message;
|
state.error.value = err.message;
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
@@ -268,7 +277,9 @@ export function useOAuth(provider = 'challonge') {
|
|||||||
sessionStorage.removeItem('oauth_provider');
|
sessionStorage.removeItem('oauth_provider');
|
||||||
sessionStorage.removeItem('oauth_return_to');
|
sessionStorage.removeItem('oauth_return_to');
|
||||||
|
|
||||||
console.log(`✅ ${provider} OAuth authentication successful, expires in ${data.expires_in}s`);
|
console.log(
|
||||||
|
`✅ ${provider} OAuth authentication successful, expires in ${data.expires_in}s`
|
||||||
|
);
|
||||||
return tokens;
|
return tokens;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
state.error.value = err.message;
|
state.error.value = err.message;
|
||||||
@@ -331,7 +342,9 @@ export function useOAuth(provider = 'challonge') {
|
|||||||
state.tokens.value = tokens;
|
state.tokens.value = tokens;
|
||||||
localStorage.setItem(state.storageKey, JSON.stringify(tokens));
|
localStorage.setItem(state.storageKey, JSON.stringify(tokens));
|
||||||
|
|
||||||
console.log(`✅ ${provider} token refreshed, new expiry in ${data.expires_in}s`);
|
console.log(
|
||||||
|
`✅ ${provider} token refreshed, new expiry in ${data.expires_in}s`
|
||||||
|
);
|
||||||
return tokens;
|
return tokens;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
state.error.value = err.message;
|
state.error.value = err.message;
|
||||||
@@ -363,7 +376,9 @@ export function useOAuth(provider = 'challonge') {
|
|||||||
|
|
||||||
// Refresh if expired or expiring within 5 minutes
|
// Refresh if expired or expiring within 5 minutes
|
||||||
if (expiresIn < fiveMinutes) {
|
if (expiresIn < fiveMinutes) {
|
||||||
console.log(`🔄 ${provider} token expiring in ${Math.floor(expiresIn / 1000)}s, refreshing...`);
|
console.log(
|
||||||
|
`🔄 ${provider} token expiring in ${Math.floor(expiresIn / 1000)}s, refreshing...`
|
||||||
|
);
|
||||||
await refreshTokenFn();
|
await refreshTokenFn();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -392,7 +407,9 @@ export function useOAuth(provider = 'challonge') {
|
|||||||
function generateState() {
|
function generateState() {
|
||||||
const array = new Uint8Array(32);
|
const array = new Uint8Array(32);
|
||||||
crypto.getRandomValues(array);
|
crypto.getRandomValues(array);
|
||||||
return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
|
return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join(
|
||||||
|
''
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user