Skip to content

Commit

Permalink
Merge pull request #10 from NathanBrodin/feat-gmail
Browse files Browse the repository at this point in the history
Feat gmail
  • Loading branch information
NathanBrodin committed Feb 14, 2024
2 parents 958219d + 86fcee6 commit 2dab1b1
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 19 deletions.
22 changes: 22 additions & 0 deletions app/api/mail/gmail/messages/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getGmailClient } from '@/lib/gmail/client';
import { getServerSession } from 'next-auth';

export async function GET() {
const session = await getServerSession();

if (!session) {
return {
status: 401,
body: { error: 'Unauthorized' },
};
}

const gmail = getGmailClient(session);

const messages = await gmail.users.messages.list({
userId: 'me',
maxResults: 10,
});

return Response.json({ messages });
}
24 changes: 24 additions & 0 deletions lib/gmail/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { google } from 'googleapis';
import { Session } from 'next-auth';

// Creates a new OAuth2 client with the provided credentials
function getClient(session: Session & { refreshToken?: string }) {
const auth = new google.auth.OAuth2({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
});

auth.setCredentials({
refresh_token: session.refreshToken,
});

return auth;
}

// Returns a new Gmail client, authenticated with the provided session
export function getGmailClient(session: Session) {
const auth = getClient(session);
const gmail = google.gmail({ version: 'v1', auth });

return gmail;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"eslint-plugin-cypress": "^2.15.1",
"framer-motion": "^11.0.3",
"geist": "^1.2.1",
"googleapis": "^133.0.0",
"jotai": "^2.6.4",
"lucide-react": "^0.321.0",
"negotiator": "^0.6.3",
Expand Down
Loading

0 comments on commit 2dab1b1

Please sign in to comment.