API reformatting
This commit is contained in:
30
src/routes/private/api/google/auth/refresh/+server.ts
Normal file
30
src/routes/private/api/google/auth/refresh/+server.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { getOAuthClient } from '$lib/google-server.js';
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
try {
|
||||
const { refreshToken } = await request.json();
|
||||
|
||||
if (!refreshToken) {
|
||||
return json({ error: 'Refresh token is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
const oauth = getOAuthClient();
|
||||
oauth.setCredentials({ refresh_token: refreshToken });
|
||||
|
||||
const { credentials } = await oauth.refreshAccessToken();
|
||||
|
||||
if (!credentials.access_token) {
|
||||
return json({ error: 'Failed to refresh token' }, { status: 500 });
|
||||
}
|
||||
|
||||
return json({
|
||||
accessToken: credentials.access_token,
|
||||
expiresIn: credentials.expiry_date
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error refreshing access token:', error);
|
||||
return json({ error: 'Failed to refresh access token' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
31
src/routes/private/api/google/auth/revoke/+server.ts
Normal file
31
src/routes/private/api/google/auth/revoke/+server.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
try {
|
||||
const { accessToken } = await request.json();
|
||||
|
||||
if (!accessToken) {
|
||||
return json({ error: 'Access token is required' }, { status: 400 });
|
||||
}
|
||||
|
||||
// Call Google's token revocation endpoint
|
||||
const response = await fetch(`https://accounts.google.com/o/oauth2/revoke?token=${accessToken}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
}
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
return json({ success: true });
|
||||
} else {
|
||||
const error = await response.text();
|
||||
console.error('Error revoking token:', error);
|
||||
return json({ error: 'Failed to revoke token' }, { status: 500 });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error revoking access token:', error);
|
||||
return json({ error: 'Failed to revoke access token' }, { status: 500 });
|
||||
}
|
||||
};
|
||||
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