57 lines
1.2 KiB
TypeScript
57 lines
1.2 KiB
TypeScript
'use server'
|
|||
|
|
|
||
|
|
import { API_BASE_URL } from '@/app/actions/definitions';
|
||
|
|
import { createSession } from '@/app/actions/session';
|
||
|
|
import { redirect } from 'next/navigation';
|
||
|
|
|
||
|
|
|
||
|
|
export async function vkAuthorization(data : any, codeVerifier: string) {
|
||
|
|
|
||
|
|
console.log(data);
|
||
|
|
console.log(codeVerifier);
|
||
|
|
const { code, state } = data
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await fetch(`${API_BASE_URL}/api/v1/vk/authorization/${code}/${state}`, {
|
||
|
|
method: 'GET',
|
||
|
|
headers: {
|
||
|
|
"Content-Type": "application/json",
|
||
|
|
"Accept": "application/json"
|
||
|
|
}
|
||
|
|
});
|
||
|
|
if (response.ok) {
|
||
|
|
let parsed = await response.json();
|
||
|
|
if (parsed.message_desc === 'Operation successful') {
|
||
|
|
/* await createSession(parsed.message_body.token, email); */
|
||
|
|
} else {
|
||
|
|
throw (`${parsed.message_code}`);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
throw ('request-ended-with-an-error');
|
||
|
|
}
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
const errors: Record<string, string> = {}
|
||
|
|
|
||
|
|
switch (error) {
|
||
|
|
case '2':
|
||
|
|
errors['server'] = 'password-does-not-match'
|
||
|
|
break;
|
||
|
|
|
||
|
|
case '4':
|
||
|
|
errors['server'] = 'email-not-found'
|
||
|
|
break;
|
||
|
|
|
||
|
|
default:
|
||
|
|
errors['server'] = 'request-ended-with-an-error'
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (error) {
|
||
|
|
return error;
|
||
|
|
}
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
|
||
|
|
redirect('/pages/dashboard');
|
||
|
|
}
|