기능구현중
This commit is contained in:
50
server/api/auth/google/index.get.ts
Normal file
50
server/api/auth/google/index.get.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Google OAuth 시작
|
||||
* GET /api/auth/google
|
||||
*
|
||||
* Query params:
|
||||
* - extend: 'groups' - 구글 그룹 접근 권한 추가 요청
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const query = getQuery(event)
|
||||
|
||||
const clientId = config.googleClientId || process.env.GOOGLE_CLIENT_ID
|
||||
const redirectUri = config.googleRedirectUri || process.env.GOOGLE_REDIRECT_URI || 'http://localhost:3000/api/auth/google/callback'
|
||||
|
||||
if (!clientId) {
|
||||
throw createError({ statusCode: 500, message: 'Google OAuth가 설정되지 않았습니다.' })
|
||||
}
|
||||
|
||||
// 기본 scope + 확장 scope
|
||||
let scopes = ['openid', 'email', 'profile']
|
||||
|
||||
// 구글 그룹 권한 요청 시 추가 scope
|
||||
if (query.extend === 'groups') {
|
||||
scopes.push(
|
||||
'https://www.googleapis.com/auth/gmail.readonly', // 그룹 메일 읽기
|
||||
'https://www.googleapis.com/auth/cloud-identity.groups.readonly' // 그룹 정보 읽기
|
||||
)
|
||||
}
|
||||
|
||||
const scope = encodeURIComponent(scopes.join(' '))
|
||||
const state = Math.random().toString(36).substring(7) // CSRF 방지
|
||||
|
||||
// state를 쿠키에 저장
|
||||
setCookie(event, 'oauth_state', state, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
maxAge: 300 // 5분
|
||||
})
|
||||
|
||||
const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?` +
|
||||
`client_id=${clientId}` +
|
||||
`&redirect_uri=${encodeURIComponent(redirectUri)}` +
|
||||
`&response_type=code` +
|
||||
`&scope=${scope}` +
|
||||
`&state=${state}` +
|
||||
`&access_type=offline` +
|
||||
`&prompt=consent`
|
||||
|
||||
return sendRedirect(event, authUrl)
|
||||
})
|
||||
Reference in New Issue
Block a user