All files / src/api/drive auth.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49          53x                                               53x 19x   19x 17x     2x 2x   2x               2x  
import { createRoute, RouteHandler } from '@hono/zod-openapi'
import { ErrorSchema, AuthUrlResponseSchema } from '../../schema/drive'
 
import type { EnvHono } from '../..'
 
export const authUrlRoute = createRoute({
  method: 'get',
  path: '/auth-url',
  responses: {
    200: {
      content: {
        'application/json': {
          schema: AuthUrlResponseSchema
        }
      },
      description: 'Google OAuth authorization URL'
    },
    400: {
      content: {
        'application/json': {
          schema: ErrorSchema
        }
      },
      description: 'Missing configuration'
    }
  },
  tags: ['Drive API']
})
 
export const authUrlHandler: RouteHandler<typeof authUrlRoute, EnvHono> = (c) => {
  const { GOOGLE_CLIENT_ID } = c.env
 
  if (!GOOGLE_CLIENT_ID) {
    return c.json({ error: 'Missing GOOGLE_CLIENT_ID' }, 400)
  }
 
  const redirectUri = 'http://localhost:3000/api/drive/callback'
  const scope = 'https://www.googleapis.com/auth/drive.file'
 
  const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?` +
    `client_id=${encodeURIComponent(GOOGLE_CLIENT_ID)}` +
    `&redirect_uri=${encodeURIComponent(redirectUri)}` +
    `&scope=${encodeURIComponent(scope)}` +
    `&access_type=offline` +
    `&prompt=consent` +
    `&response_type=code`
 
  return c.json({ authUrl }, 200)
}