-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from NathanBrodin/feat-gmail
Feat gmail
- Loading branch information
Showing
4 changed files
with
210 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.