vk auth
This commit is contained in:
@@ -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<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');
|
||||||
|
}
|
||||||
@@ -1,33 +1,62 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { useEffect, useRef } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import * as VKID from '@vkid/sdk';
|
import * as VKID from '@vkid/sdk';
|
||||||
|
import { vkAuthorization } from '@/app/actions/vkAuth';
|
||||||
|
|
||||||
export default function VKLogin() {
|
export default function VKLogin() {
|
||||||
|
let [codeVerifier, setCodeVerifier] = useState('');
|
||||||
|
|
||||||
const vkButton = useRef<HTMLDivElement>(null)
|
const vkButton = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
function clickHandler() {
|
function generateAuthString() {
|
||||||
alert('Кнопка пока не подключена к бизнес аккаунту!');
|
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-';
|
||||||
/* VKID.Auth.login()
|
const minLength = 43;
|
||||||
.then(vkidOnSuccess)
|
const maxLength = 128;
|
||||||
.catch(console.error); */
|
|
||||||
|
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('vkidOnSuccess');
|
||||||
console.log(data);
|
console.log(data);
|
||||||
} */
|
console.log(codeVerifier);
|
||||||
|
vkAuthorization(data, codeVerifier)
|
||||||
|
setCodeVerifier(generateAuthString());
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setCodeVerifier(generateAuthString());
|
||||||
|
console.log(codeVerifier);
|
||||||
|
|
||||||
/* useEffect(() => {
|
|
||||||
VKID.Config.init({
|
VKID.Config.init({
|
||||||
app: 54389559,
|
app: 54389559,
|
||||||
redirectUrl: 'http://localhost',
|
redirectUrl: 'http://localhost',
|
||||||
responseMode: VKID.ConfigResponseMode.Callback,
|
responseMode: VKID.ConfigResponseMode.Callback,
|
||||||
source: VKID.ConfigSource.LOWCODE,
|
source: VKID.ConfigSource.LOWCODE,
|
||||||
scope: 'email'
|
scope: 'email phone vkid.personal_info',
|
||||||
|
codeVerifier: codeVerifier
|
||||||
});
|
});
|
||||||
}, []); */
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button id="VKIDSDKAuthButton" className="VkIdWebSdk__button VkIdWebSdk__button_reset">
|
<button id="VKIDSDKAuthButton" className="VkIdWebSdk__button VkIdWebSdk__button_reset">
|
||||||
|
|||||||
Reference in New Issue
Block a user