Ghost

struxadotcloud/struxa

public
Fork0
CodeIssues0Pull requests0
1branch
struxaappswebsrcappapiaccountgoogle-driveauthorizeroute.ts
feat(web): google drive oauth, download and remote config routesd97fdf12 months ago41 lines · 1.4 KB
1import { randomBytes } from "crypto";
2import { NextResponse, type
3import { getAuth }
4import { getAppUrl, getOperatorGDriveConfig
5
6export async function GET
7 const auth =
8 const session =
9 if (!session)
10
11 const config =
12 if (!config)
13
14 const appUrl =
15 if (!appUrl)
16
17 const state =
18 const redirectUri =
19
20 const params =
21 client_id: config.clientId
22 redirect_uri: redirectUri,
23 response_type: "code",
24 scope: "https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.email",
25 access_type: "offline",
26 prompt: "consent",
27 state,
28 });
29
30 const res =
31 `https://accounts.google.com/o/oauth2/v2/auth?${params.toString
32 );
33 res.cookies.set
34 httpOnly: true,
35 sameSite: "lax",
36 path: "/",
37 maxAge: 600,
38 secure: process.env
39 });
40 return res;
41}
NextRequest
}
from
"
next/server
"
;
from
"
@struxa/auth
"
;
}
from
"
@struxa/api/services/google-drive
"
;
(
req
:
NextRequest
)
{
await
getAuth
()
;
await
auth
.
api
.
getSession
(
{
headers
:
req
.
headers
}
)
;
return
new
Response
(
"
Unauthorized
"
,
{
status
:
401
}
)
;
await
getOperatorGDriveConfig
()
;
return
new
Response
(
"
Google Drive is not configured
"
,
{
status
:
400
}
)
;
await
getAppUrl
()
;
return
new
Response
(
"
APP_URL is not configured
"
,
{
status
:
400
}
)
;
randomBytes
(
16
)
.
toString
(
"
hex
"
)
;
`
${
appUrl
}
/api/account/google-drive/callback
`
;
new
URLSearchParams
(
{
,
NextResponse
.
redirect
(
()
}
`
,
(
"
gdrive_oauth_state
"
,
state
,
{
.
NODE_ENV
===
"
production
"
,