added cookies storage for git
Browse files- app/lib/hooks/useGit.ts +26 -20
app/lib/hooks/useGit.ts
CHANGED
|
@@ -1,35 +1,38 @@
|
|
| 1 |
import type { WebContainer } from '@webcontainer/api';
|
| 2 |
import { useCallback, useEffect, useRef, useState, type MutableRefObject } from 'react';
|
| 3 |
import { webcontainer as webcontainerPromise } from '~/lib/webcontainer';
|
| 4 |
-
import git, { type PromiseFsClient } from 'isomorphic-git';
|
| 5 |
import http from 'isomorphic-git/http/web';
|
| 6 |
import Cookies from 'js-cookie';
|
| 7 |
import { toast } from 'react-toastify';
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
export function useGit() {
|
| 10 |
const [ready, setReady] = useState(false);
|
| 11 |
const [webcontainer, setWebcontainer] = useState<WebContainer>();
|
| 12 |
const [fs, setFs] = useState<PromiseFsClient>();
|
| 13 |
const fileData = useRef<Record<string, { data: any; encoding?: string }>>({});
|
| 14 |
-
const lookupSavedPassword: (url: string) => any | null = (url: string) => {
|
| 15 |
-
try {
|
| 16 |
-
// Save updated API keys to cookies with 30 day expiry and secure settings
|
| 17 |
-
const creds = Cookies.get(`git:${url}`);
|
| 18 |
-
|
| 19 |
-
if (creds) {
|
| 20 |
-
const parsedCreds = JSON.parse(creds);
|
| 21 |
-
|
| 22 |
-
if (typeof parsedCreds === 'object' && parsedCreds !== null) {
|
| 23 |
-
return parsedCreds;
|
| 24 |
-
}
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
return null;
|
| 28 |
-
} catch (error) {
|
| 29 |
-
console.error('Error saving API keys to cookies:', error);
|
| 30 |
-
return null;
|
| 31 |
-
}
|
| 32 |
-
};
|
| 33 |
useEffect(() => {
|
| 34 |
webcontainerPromise.then((container) => {
|
| 35 |
fileData.current = {};
|
|
@@ -76,6 +79,9 @@ export function useGit() {
|
|
| 76 |
onAuthFailure: (url, _auth) => {
|
| 77 |
toast.error(`Error Authenticating with ${url.split('/')[2]}`);
|
| 78 |
},
|
|
|
|
|
|
|
|
|
|
| 79 |
});
|
| 80 |
|
| 81 |
const data: Record<string, { data: any; encoding?: string }> = {};
|
|
|
|
| 1 |
import type { WebContainer } from '@webcontainer/api';
|
| 2 |
import { useCallback, useEffect, useRef, useState, type MutableRefObject } from 'react';
|
| 3 |
import { webcontainer as webcontainerPromise } from '~/lib/webcontainer';
|
| 4 |
+
import git, { type GitAuth, type PromiseFsClient } from 'isomorphic-git';
|
| 5 |
import http from 'isomorphic-git/http/web';
|
| 6 |
import Cookies from 'js-cookie';
|
| 7 |
import { toast } from 'react-toastify';
|
| 8 |
|
| 9 |
+
const lookupSavedPassword = (url: string) => {
|
| 10 |
+
const domain = url.split('/')[2];
|
| 11 |
+
const gitCreds = Cookies.get(`git:${domain}`);
|
| 12 |
+
|
| 13 |
+
if (!gitCreds) {
|
| 14 |
+
return null;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
try {
|
| 18 |
+
const { username, password } = JSON.parse(gitCreds || '{}');
|
| 19 |
+
return { username, password };
|
| 20 |
+
} catch (error) {
|
| 21 |
+
console.log(`Failed to parse Git Cookie ${error}`);
|
| 22 |
+
return null;
|
| 23 |
+
}
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
const saveGitAuth = (url: string, auth: GitAuth) => {
|
| 27 |
+
const domain = url.split('/')[2];
|
| 28 |
+
Cookies.set(`git:${domain}`, JSON.stringify(auth));
|
| 29 |
+
};
|
| 30 |
+
|
| 31 |
export function useGit() {
|
| 32 |
const [ready, setReady] = useState(false);
|
| 33 |
const [webcontainer, setWebcontainer] = useState<WebContainer>();
|
| 34 |
const [fs, setFs] = useState<PromiseFsClient>();
|
| 35 |
const fileData = useRef<Record<string, { data: any; encoding?: string }>>({});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
useEffect(() => {
|
| 37 |
webcontainerPromise.then((container) => {
|
| 38 |
fileData.current = {};
|
|
|
|
| 79 |
onAuthFailure: (url, _auth) => {
|
| 80 |
toast.error(`Error Authenticating with ${url.split('/')[2]}`);
|
| 81 |
},
|
| 82 |
+
onAuthSuccess: (url, auth) => {
|
| 83 |
+
saveGitAuth(url, auth);
|
| 84 |
+
},
|
| 85 |
});
|
| 86 |
|
| 87 |
const data: Record<string, { data: any; encoding?: string }> = {};
|