fix: switch to service account auth for Google Calendar
Some checks failed
Deploy to VM / deploy (push) Failing after 1s
Some checks failed
Deploy to VM / deploy (push) Failing after 1s
This commit is contained in:
@@ -1,41 +1,61 @@
|
||||
export const dynamic = 'force-dynamic'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { google } from 'googleapis'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
|
||||
function getAuth() {
|
||||
// Service account JSON (inline or from file)
|
||||
const saJson = process.env.GOOGLE_SA_JSON
|
||||
if (saJson) {
|
||||
const sa = JSON.parse(saJson)
|
||||
return new google.auth.GoogleAuth({
|
||||
credentials: sa,
|
||||
scopes: ['https://www.googleapis.com/auth/calendar.readonly'],
|
||||
})
|
||||
}
|
||||
// Fallback: file
|
||||
const saPath = path.join(process.cwd(), 'google-sa.json')
|
||||
if (fs.existsSync(saPath)) {
|
||||
return new google.auth.GoogleAuth({
|
||||
keyFile: saPath,
|
||||
scopes: ['https://www.googleapis.com/auth/calendar.readonly'],
|
||||
})
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const { searchParams } = new URL(req.url)
|
||||
const range = searchParams.get('range') || 'today'
|
||||
|
||||
const clientId = process.env.GOOGLE_CLIENT_ID
|
||||
const clientSecret = process.env.GOOGLE_CLIENT_SECRET
|
||||
const refreshToken = process.env.GOOGLE_REFRESH_TOKEN
|
||||
const svetaCalendarId = process.env.SVETA_CALENDAR_ID
|
||||
|
||||
if (!clientId || !clientSecret || !refreshToken) {
|
||||
const auth = getAuth()
|
||||
if (!auth) {
|
||||
return NextResponse.json({ events: [], error: 'not_configured' })
|
||||
}
|
||||
|
||||
const auth = new google.auth.OAuth2(clientId, clientSecret)
|
||||
auth.setCredentials({ refresh_token: refreshToken })
|
||||
const daniilCalendarId = process.env.DANIIL_CALENDAR_ID || 'daniilklimov25@gmail.com'
|
||||
const svetaCalendarId = process.env.SVETA_CALENDAR_ID || ''
|
||||
|
||||
const now = new Date()
|
||||
const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate())
|
||||
let timeMin = todayStart.toISOString()
|
||||
let timeMin: string
|
||||
let timeMax: string
|
||||
|
||||
if (range === 'today') {
|
||||
timeMin = new Date(now.getFullYear(), now.getMonth(), now.getDate()).toISOString()
|
||||
timeMax = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1).toISOString()
|
||||
} else if (range === 'week') {
|
||||
timeMin = new Date(now.getFullYear(), now.getMonth(), now.getDate()).toISOString()
|
||||
timeMax = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 7).toISOString()
|
||||
} else {
|
||||
timeMax = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59).toISOString()
|
||||
timeMin = new Date(now.getFullYear(), now.getMonth(), 1).toISOString()
|
||||
timeMax = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59).toISOString()
|
||||
}
|
||||
|
||||
const calendarClient = google.calendar({ version: 'v3', auth })
|
||||
const calendarClient = google.calendar({ version: 'v3', auth: auth as any })
|
||||
|
||||
const calendars = [
|
||||
{ id: 'daniilklimov25@gmail.com', owner: 'daniil', color: '#6366f1', name: 'Даниил' },
|
||||
{ id: daniilCalendarId, owner: 'daniil', color: '#6366f1', name: 'Даниил' },
|
||||
...(svetaCalendarId ? [{ id: svetaCalendarId, owner: 'sveta', color: '#ec4899', name: 'Света' }] : [])
|
||||
]
|
||||
|
||||
@@ -71,5 +91,9 @@ export async function GET(req: Request) {
|
||||
})
|
||||
.sort((a, b) => new Date(a.start).getTime() - new Date(b.start).getTime())
|
||||
|
||||
return NextResponse.json({ events: allEvents, fetchedAt: new Date().toISOString() })
|
||||
const errors = results
|
||||
.filter(r => r.status === 'rejected')
|
||||
.map(r => (r as PromiseRejectedResult).reason?.message || 'unknown')
|
||||
|
||||
return NextResponse.json({ events: allEvents, errors: errors.length ? errors : undefined, fetchedAt: new Date().toISOString() })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user