Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google Tasks Integration #100

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
49 changes: 49 additions & 0 deletions client/src/integrations/googletasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

const API_BASE_URL = process.env.REACT_APP_PROD || 'http://localhost:3001/api';

export const importFromGoogleTasks = async (addTask, setIsLoading) => {
setIsLoading(true);
try {
const messageHandler = async (event) => {
if (event.data.type === 'googletasks-auth-success') {
window.removeEventListener('message', messageHandler);
if (event.data.tasks && Array.isArray(event.data.tasks)) {
const tasksToAdd = event.data.tasks.map(taskName => ({
name: taskName,
desc: 'Imported from Google Tasks',
difficulty: 5,
importance: 5,
deadline: null,
collaborative: false,
experience: 150
}));
addTask(tasksToAdd);
}
setIsLoading(false);
} else if (event.data.type === 'googletasks-auth-error') {
window.removeEventListener('message', messageHandler);
setIsLoading(false);
}
};

window.addEventListener('message', messageHandler);

const width = 500;
const height = 600;
const left = window.screenX + (window.outerWidth - width) / 2;
const top = window.screenY + (window.outerHeight - height) / 2;

const popup = window.open(
`${API_BASE_URL}/auth/googletasks`,
'Import from Google Tasks',
`width=${width},height=${height},left=${left},top=${top},popup=1`
);

if (!popup || popup.closed) {
throw new Error('Popup blocked! Please allow popups for this site.');
}
} catch (error) {
console.error('Google Tasks import failed:', error);
setIsLoading(false);
}
};
26 changes: 26 additions & 0 deletions client/src/integrations/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from 'react';
import { ChevronDown, ChevronUp, Link2 } from 'lucide-react';
import { importFromTodoist } from './todoist';
import { importFromGoogleTasks } from './googletasks';

const IntegrationsDropdown = ({ addTask, isAuthenticated }) => {
const [isOpen, setIsOpen] = useState(false);
Expand Down Expand Up @@ -55,6 +56,31 @@ const IntegrationsDropdown = ({ addTask, isAuthenticated }) => {
</>
)}
</button>

<button
onClick={() => {
importFromGoogleTasks(addTask, setIsLoading);
setIsOpen(false);
}}
disabled={isLoading}
className="w-full px-4 py-3 text-gray-800 dark:text-white
hover:bg-gray-100 dark:hover:bg-gray-700 flex items-center gap-3"
title="Import from Google Tasks"
>
{isLoading ? (
<svg className="w-5 h-5 animate-spin" viewBox="0 0 24 24">
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"/>
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/>
</svg>
) : (
<>
<svg className="w-8 h-8" viewBox="0 0 24 24" fill="#4285f4">
<path d="M12 0c-6.627 0-12 5.373-12 12s5.373 12 12 12 12-5.373 12-12-5.373-12-12-12zm-1.959 17l-4.5-4.319 1.395-1.435 3.08 2.937 7.021-7.183 1.422 1.409-8.418 8.591z"/>
</svg>
<span className="text-sm font-medium">Import from Google Tasks</span>
</>
)}
</button>
</div>
)}
</div>
Expand Down
47 changes: 36 additions & 11 deletions server/config/passport-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,48 @@ passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: "/api/auth/google/callback",
proxy: true
proxy: true,
accessType: 'offline', // Requests refresh token
prompt: 'consent select_account', // Forces consent AND account selection
enablePersistentToken: true, // Ensures refresh token is always included
hostedDomain: undefined, // Add this to ensure all domains are allowed
includeGrantedScopes: true, // Add this to include all granted scopes
scope: [
'profile',
'email',
'https://www.googleapis.com/auth/tasks.readonly'
]
},
async (_accessToken, _refreshToken, profile, done) => {
async (accessToken, refreshToken, params, profile, done) => {
try {
// Calculate expiry time from expires_in
const expiry_date = Date.now() + (params.expires_in * 1000);

console.log('[OAuth Debug] Received tokens:', {
hasAccessToken: !!accessToken,
hasRefreshToken: !!refreshToken,
expiryDate: new Date(expiry_date),
timeUntilExpiry: params.expires_in,
scope: params.scope
});

const db = await connectToDatabase();
const usersCollection = db.collection('users');

const existingUser = await usersCollection.findOne({ googleId: profile.id });

if (existingUser) {
const userData = {
...existingUser,
lastLogin: new Date()
};

// Update last login time
// Only update last login, no token storage
await usersCollection.updateOne(
{ _id: existingUser._id },
{ $set: { lastLogin: new Date() }}
);

console.log('User logged in with ID:', existingUser._id.toString());
return done(null, userData);
return done(null, existingUser, {
accessToken,
refreshToken,
expiry_date
});
}

const newUser = {
Expand All @@ -65,8 +84,14 @@ passport.use(new GoogleStrategy({
newUser._id = result.insertedId;

console.log('New user created with ID:', newUser._id.toString());
return done(null, newUser);

return done(null, newUser, {
accessToken,
refreshToken,
expiry_date
});
} catch (error) {
// ...existing code...
return done(error);
}
}
Expand Down
Loading