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
50 changes: 50 additions & 0 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// app/api/auth/[...nextauth]/route.ts
import NextAuth, { NextAuthOptions } from "next-auth";
import { JWT } from "next-auth/jwt";
import { Session } from "next-auth";
import GoogleProvider from "next-auth/providers/google";

export const authOptions: NextAuthOptions = {
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
authorization: {
params: {
scope: "openid email profile https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/calendar.events",
prompt: "consent",
access_type: "offline",
response_type: "code",
},
},
}),
],

callbacks: {
async jwt({ token, account }: { token: JWT; account?: any }) {
// Persist the OAuth tokens right after sign-in
if (account) {
token.accessToken = account.access_token;
token.refreshToken = account.refresh_token;
}
return token;
},
async session({ session, token }: { session: Session; token: JWT }) {
// Make the access token available in the client session
session.accessToken = token.accessToken as string;
return session;
},
},

// remove the custom pages override so NextAuth uses its default /api/auth/* routes
// pages: {
// signIn: '/auth/signin',
// },

secret: process.env.NEXTAUTH_SECRET,
};

const handler = NextAuth(authOptions);

// export both GET and POST so Next.js can route OAuth flows correctly
export { handler as GET, handler as POST };
55 changes: 55 additions & 0 deletions app/api/calendar/events/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { NextRequest, NextResponse } from 'next/server'
import { google } from 'googleapis'
import { getServerSession } from 'next-auth/next'
import { authOptions } from '../../auth/[...nextauth]/route'

export async function GET(request: NextRequest) {
try {
const session = await getServerSession(authOptions)

if (!session?.accessToken) {
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 })
}

const oauth2Client = new google.auth.OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.NEXTAUTH_URL
)

oauth2Client.setCredentials({
access_token: session.accessToken as string,
})

const calendar = google.calendar({ version: 'v3', auth: oauth2Client })

// Get events for the next 7 days
const now = new Date()
const oneWeekFromNow = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000)

const response = await calendar.events.list({
calendarId: 'primary',
timeMin: now.toISOString(),
timeMax: oneWeekFromNow.toISOString(),
singleEvents: true,
orderBy: 'startTime',
})

const events = response.data.items?.map(event => ({
id: event.id,
title: event.summary || 'Untitled Event',
start: event.start?.dateTime || event.start?.date,
end: event.end?.dateTime || event.end?.date,
description: event.description,
location: event.location,
})) || []

return NextResponse.json({ events })
} catch (error) {
console.error('Error fetching calendar events:', error)
return NextResponse.json(
{ error: 'Failed to fetch calendar events' },
{ status: 500 }
)
}
}
Loading