diff --git a/src/app/actions/vkAuth.ts b/src/app/actions/vkAuth.ts new file mode 100644 index 0000000..fc5c2ca --- /dev/null +++ b/src/app/actions/vkAuth.ts @@ -0,0 +1,57 @@ +'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 = {} + + 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'); +} \ No newline at end of file diff --git a/src/app/components/VKLogin.tsx b/src/app/components/VKLogin.tsx index 9bd07ca..d79f632 100644 --- a/src/app/components/VKLogin.tsx +++ b/src/app/components/VKLogin.tsx @@ -1,33 +1,62 @@ 'use client' -import { useEffect, useRef } from 'react'; +import { useEffect, useRef, useState } from 'react'; import * as VKID from '@vkid/sdk'; +import { vkAuthorization } from '@/app/actions/vkAuth'; export default function VKLogin() { + let [codeVerifier, setCodeVerifier] = useState(''); const vkButton = useRef(null) - function clickHandler() { - alert('Кнопка пока не подключена к бизнес аккаунту!'); -/* VKID.Auth.login() - .then(vkidOnSuccess) - .catch(console.error); */ + function generateAuthString() { + const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-'; + const minLength = 43; + const maxLength = 128; + + const length = Math.floor(Math.random() * (maxLength - minLength + 1)) + minLength; + + let result = ''; + const charsLength = chars.length; + + for (let i = 0; i < length; i++) { + result += chars.charAt(Math.floor(Math.random() * charsLength)); + } + + return result; } -/* function vkidOnSuccess(data: any) { + + function clickHandler() { + /* alert('Кнопка пока не подключена к бизнес аккаунту!'); */ + console.log('clickHandler'); + console.log(codeVerifier); + VKID.Auth.login() + .then(vkidOnSuccess) + .catch(console.error); + } + + function vkidOnSuccess(data: any) { console.log('vkidOnSuccess'); console.log(data); - } */ + console.log(codeVerifier); + vkAuthorization(data, codeVerifier) + setCodeVerifier(generateAuthString()); + } + + useEffect(() => { + setCodeVerifier(generateAuthString()); + console.log(codeVerifier); -/* useEffect(() => { VKID.Config.init({ app: 54389559, redirectUrl: 'http://localhost', responseMode: VKID.ConfigResponseMode.Callback, source: VKID.ConfigSource.LOWCODE, - scope: 'email' + scope: 'email phone vkid.personal_info', + codeVerifier: codeVerifier }); - }, []); */ + }, []); return (