feat: new improvement for the GitHub API Authentication Fix (#1537)
* Add environment variables section to ConnectionsTab and fallback token to git-info * Add remaining code from original branch * Import Repo Fix * refactor the UI * add a rate limit counter * Update GithubConnection.tsx * Update NetlifyConnection.tsx * fix: ui style * Sync with upstream and preserve GitHub connection and DataTab fixes * fix disconnect buttons * revert commits * Update api.git-proxy.$.ts * Update api.git-proxy.$.ts
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
import { atom } from 'nanostores';
|
||||
import type { NetlifyConnection } from '~/types/netlify';
|
||||
import type { NetlifyConnection, NetlifyUser } from '~/types/netlify';
|
||||
import { logStore } from './logs';
|
||||
import { toast } from 'react-toastify';
|
||||
|
||||
// Initialize with stored connection or defaults
|
||||
// Initialize with stored connection or environment variable
|
||||
const storedConnection = typeof window !== 'undefined' ? localStorage.getItem('netlify_connection') : null;
|
||||
const envToken = import.meta.env.VITE_NETLIFY_ACCESS_TOKEN;
|
||||
|
||||
// If we have an environment token but no stored connection, initialize with the env token
|
||||
const initialConnection: NetlifyConnection = storedConnection
|
||||
? JSON.parse(storedConnection)
|
||||
: {
|
||||
user: null,
|
||||
token: '',
|
||||
token: envToken || '',
|
||||
stats: undefined,
|
||||
};
|
||||
|
||||
@@ -17,6 +20,52 @@ export const netlifyConnection = atom<NetlifyConnection>(initialConnection);
|
||||
export const isConnecting = atom<boolean>(false);
|
||||
export const isFetchingStats = atom<boolean>(false);
|
||||
|
||||
// Function to initialize Netlify connection with environment token
|
||||
export async function initializeNetlifyConnection() {
|
||||
const currentState = netlifyConnection.get();
|
||||
|
||||
// If we already have a connection, don't override it
|
||||
if (currentState.user || !envToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
isConnecting.set(true);
|
||||
|
||||
const response = await fetch('https://api.netlify.com/api/v1/user', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${envToken}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to connect to Netlify: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const userData = await response.json();
|
||||
|
||||
// Update the connection state
|
||||
const connectionData: Partial<NetlifyConnection> = {
|
||||
user: userData as NetlifyUser,
|
||||
token: envToken,
|
||||
};
|
||||
|
||||
// Store in localStorage for persistence
|
||||
localStorage.setItem('netlify_connection', JSON.stringify(connectionData));
|
||||
|
||||
// Update the store
|
||||
updateNetlifyConnection(connectionData);
|
||||
|
||||
// Fetch initial stats
|
||||
await fetchNetlifyStats(envToken);
|
||||
} catch (error) {
|
||||
console.error('Error initializing Netlify connection:', error);
|
||||
logStore.logError('Failed to initialize Netlify connection', { error });
|
||||
} finally {
|
||||
isConnecting.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
export const updateNetlifyConnection = (updates: Partial<NetlifyConnection>) => {
|
||||
const currentState = netlifyConnection.get();
|
||||
const newState = { ...currentState, ...updates };
|
||||
|
||||
Reference in New Issue
Block a user