API reformatting
This commit is contained in:
33
src/routes/private/api/google/auth/userinfo/+server.ts
Normal file
33
src/routes/private/api/google/auth/userinfo/+server.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { getOAuthClient } from '$lib/google-server.js';
|
||||
import { google } from 'googleapis';
|
||||
|
||||
export const GET: RequestHandler = async ({ request }) => {
|
||||
try {
|
||||
const authHeader = request.headers.get('authorization');
|
||||
|
||||
if (!authHeader?.startsWith('Bearer ')) {
|
||||
return json({ error: 'Missing or invalid authorization header' }, { status: 401 });
|
||||
}
|
||||
|
||||
const accessToken = authHeader.slice(7);
|
||||
|
||||
// Create OAuth client with the token
|
||||
const oauth = getOAuthClient();
|
||||
oauth.setCredentials({ access_token: accessToken });
|
||||
|
||||
// Call the userinfo endpoint to get user details
|
||||
const oauth2 = google.oauth2({ version: 'v2', auth: oauth });
|
||||
const userInfo = await oauth2.userinfo.get();
|
||||
|
||||
return json({
|
||||
email: userInfo.data.email,
|
||||
name: userInfo.data.name,
|
||||
picture: userInfo.data.picture
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching user info:', error);
|
||||
return json({ error: 'Failed to fetch user info' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user