token.tsx (882B)
1 import { AppState } from "#http/routes/_middleware.ts"; 2 import { Handlers } from "$fresh/server.ts"; 3 import * as V from "$valita"; 4 5 export const handler: Handlers<void, AppState> = { 6 async POST(req, ctx) { 7 const { app, forms } = ctx.state; 8 const { oauth2Token } = app; 9 10 const data = await forms.inputs( 11 req, 12 V.object({ 13 client_id: V.string(), 14 client_secret: V.string(), 15 code: V.string(), 16 }), 17 ); 18 19 const result = await oauth2Token.execute({ 20 clientId: data.client_id, 21 clientSecret: data.client_secret, 22 code: data.code, 23 }); 24 25 if (result.status === "issued") { 26 return Response.json({ 27 access_token: result.accessToken, 28 token_type: "bearer", 29 expires_in: result.expire, 30 state: result.state, 31 }); 32 } 33 34 return new Response(null, { status: 400 }); 35 }, 36 };