Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions src/pages/Background/modules/saveToDrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,65 @@ const saveToDrive = async (videoBlob, fileName, sendResponse) => {
payload = JSON.parse(atob(token.split(".")[1]));
} catch (err) {
// Token is invalid, refresh it
chrome.identity.getAuthToken(
{ interactive: true },
(newToken) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError));
const isEdge = navigator.userAgent.includes('Edg');

if (isEdge) {
// For Edge, use signIn to refresh the token
signIn().then((newToken) => {
if (!newToken) {
reject(new Error("Failed to refresh token"));
} else {
resolve(newToken);
}
}
);
}).catch((error) => {
reject(error);
});
} else {
// For Chrome, use the standard method
chrome.identity.getAuthToken(
{ interactive: true },
(newToken) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError));
} else {
resolve(newToken);
}
}
);
}
return;
}

const expirationTime = payload.exp * 1000; // Convert to milliseconds
const currentTime = Date.now();
if (currentTime >= expirationTime) {
// Token has expired, refresh it
chrome.identity.getAuthToken(
{ interactive: true },
(newToken) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError));
const isEdge = navigator.userAgent.includes('Edg');

if (isEdge) {
// For Edge, use signIn to refresh the token
signIn().then((newToken) => {
if (!newToken) {
reject(new Error("Failed to refresh token"));
} else {
resolve(newToken);
}
}
);
}).catch((error) => {
reject(error);
});
} else {
// For Chrome, use the standard method
chrome.identity.getAuthToken(
{ interactive: true },
(newToken) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError));
} else {
resolve(newToken);
}
}
);
}
} else {
// Token is still valid
resolve(token);
Expand Down
61 changes: 51 additions & 10 deletions src/pages/Background/modules/signIn.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
const signIn = async () => {
try {
const token = await chrome.identity.getAuthToken({ interactive: true });
// Check if the browser is Microsoft Edge
const isEdge = navigator.userAgent.includes('Edg');

if (isEdge) {
// For Edge, use the launchWebAuthFlow method instead
// Use the published extension's redirect URI for consistency
const publishedExtensionId = 'kbbdabhdfibnancpjfhlkhafgdilcnji';
const redirectUri = `https://${publishedExtensionId}.chromiumapp.org/`;
const clientId = "560517327251-m7n1k3kddknu7s9s4ejvrs1bj91gutd7.apps.googleusercontent.com";
const scopes = "https://www.googleapis.com/auth/drive.file";

// Construct the OAuth URL
const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?` +
`client_id=${clientId}&` +
`response_type=token&` +
`redirect_uri=${encodeURIComponent(redirectUri)}&` +
`scope=${encodeURIComponent(scopes)}`;

// Use launchWebAuthFlow for Edge
const responseUrl = await chrome.identity.launchWebAuthFlow({
url: authUrl,
interactive: true
});

// Extract the token from the response URL
const tokenMatch = responseUrl.match(/access_token=([^&]+)/);
if (!tokenMatch) {
throw new Error("Failed to extract token from response");
}

const token = tokenMatch[1];

// Save token to storage
await new Promise((resolve) =>
chrome.storage.local.set({ token: token }, () => resolve())
);

return token;
} else {
// For Chrome, use the standard getAuthToken method
const token = await chrome.identity.getAuthToken({ interactive: true });

if (!token) {
throw new Error("User cancelled sign-in or failed to get token");
}
if (!token) {
throw new Error("User cancelled sign-in or failed to get token");
}

// Save token to storage
await new Promise((resolve) =>
chrome.storage.local.set({ token: token.token }, () => resolve())
);
// Save token to storage
await new Promise((resolve) =>
chrome.storage.local.set({ token: token.token }, () => resolve())
);

const userInfo = await chrome.identity.getProfileUserInfo();
const userInfo = await chrome.identity.getProfileUserInfo();

return token.token; // Return the token if sign-in is successful
return token.token; // Return the token if sign-in is successful
}
} catch (error) {
console.error("Error signing in:", error.message);
return null;
Expand Down