38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|||
|
|
|
||
|
|
export async function POST(request: Request) {
|
||
|
|
const { code } = await request.json();
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await fetch('https://oauth.yandex.ru/token', {
|
||
|
|
method: 'POST',
|
||
|
|
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||
|
|
body: new URLSearchParams({
|
||
|
|
grant_type: 'authorization_code',
|
||
|
|
code,
|
||
|
|
client_id: 'b356a7ceac0f4c50a3b434220f86bce0',
|
||
|
|
client_secret: '0800f7774f7d4e19ad4b434f10afd4ab',
|
||
|
|
})
|
||
|
|
});
|
||
|
|
|
||
|
|
const data = await response.json();
|
||
|
|
|
||
|
|
console.log(data);
|
||
|
|
|
||
|
|
if (data.access_token) {
|
||
|
|
const userInfo = await fetch('https://login.yandex.ru/info?format=json', {
|
||
|
|
headers: { 'Authorization': `OAuth ${data.access_token}` }
|
||
|
|
}).then(r => r.json());
|
||
|
|
|
||
|
|
console.log(userInfo);
|
||
|
|
|
||
|
|
// Тут мне нужно будет отправить сделать запрос в бек с userInfo и там уже редиректить на дешборд
|
||
|
|
|
||
|
|
return NextResponse.json({ success: true });
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Token exchange failed:', error);
|
||
|
|
}
|
||
|
|
|
||
|
|
return NextResponse.json({ success: false }, { status: 400 });
|
||
|
|
}
|