From deadf7a09d59178534bbb01e88cd237fcc739b60 Mon Sep 17 00:00:00 2001 From: viiprogrammer Date: Wed, 29 May 2024 21:28:37 +0300 Subject: [PATCH] feat: vk login --- src/main/index.js | 30 +++++++++++- .../utils/windows/openWindowInterceptor.js | 2 +- .../views/account/login/AccountLoginView.vue | 49 ++++++++++++++++++- 3 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 2b542023..0e8a6e94 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -1,4 +1,4 @@ -import { app } from 'electron' +import {app, BrowserWindow, ipcMain} from 'electron' import proxy from 'node-global-proxy'; let proxyServer @@ -52,6 +52,7 @@ import { broadcastTorrentEvents } from '@main/handlers/torrents/torrentsHandler' import Tray from './utils/tray' import Menu from './utils/menu' import { openWindowInterceptor } from '@main/utils/windows/openWindowInterceptor' +import {showAppError} from "@main/handlers/notifications/notificationsHandler"; const { discordActivity } = require('./utils/discord') const { @@ -94,6 +95,33 @@ app.on('window-all-closed', () => { }) app.on('web-contents-created', (event, webContents) => { + webContents.on('did-finish-load', async () => { + if (webContents.getURL().startsWith('https://id.vk.com/')) { + webContents.on('will-redirect', async (event, url) => { + if (!url.startsWith('https://www.anilibria.tv/')) { + return true + } + + const cookies = await webContents.session.cookies.get({ url: 'https://www.anilibria.tv' }) + + const { value: sessionId } = cookies.find(cookie => cookie.name === 'PHPSESSID') || {} + + if (sessionId) { + Main.getWindow().webContents.send('VK_CODE', sessionId) + } + + BrowserWindow.fromWebContents(webContents).hide() + + webContents.on('did-finish-load', async () => { + await webContents.session.clearStorageData() + webContents.destroy() + }) + + return true + }); + } + }) + webContents.setWindowOpenHandler(openWindowInterceptor) webContents.setUserAgent(`${meta.name}/${version}`) webContents.on('will-attach-webview', (event, webPreferences, params) => { diff --git a/src/main/utils/windows/openWindowInterceptor.js b/src/main/utils/windows/openWindowInterceptor.js index dd0dcd48..12605f12 100644 --- a/src/main/utils/windows/openWindowInterceptor.js +++ b/src/main/utils/windows/openWindowInterceptor.js @@ -2,7 +2,7 @@ import { shell } from 'electron' function openWindowInterceptor (details) { if (!details.url.startsWith('resource://')) { - if (!details.url.startsWith('https://oauth.vk.com/authorize')) { + if (!details.url.startsWith('https://oauth.vk.com/authorize') && !details.url.startsWith('https://id.vk.com/auth')) { shell.openExternal(details.url) return { action: 'deny' } } diff --git a/src/renderer/views/account/login/AccountLoginView.vue b/src/renderer/views/account/login/AccountLoginView.vue index 3c05e883..4ee3f42b 100644 --- a/src/renderer/views/account/login/AccountLoginView.vue +++ b/src/renderer/views/account/login/AccountLoginView.vue @@ -41,6 +41,12 @@ Назад + + + + Вход через VK + + @@ -52,7 +58,7 @@ // Images import LibriaTyan03 from '@assets/images/libria-tyan/LibriaTyan03.svg' - +import { ipcRenderer } from "electron"; // Utils import { required } from 'vuelidate/lib/validators' import { BackViewMixin } from '@mixins/views' @@ -76,9 +82,48 @@ export default { login: { required }, password: { required }, }, - + mounted () { + ipcRenderer.on('VK_CODE', async (event, session) => { + try { + this.loading = true + await this.$store.dispatchPromise('app/account/setSession', session) + + // Get profile data + await this.$store.dispatchPromise('app/account/getProfile') + await this.toBack() + + // Get user favorites + this.$store.dispatchPromise('favorites/getFavorites') + this.loading = false + } catch (e) { + console.error(e) + + if (e.response.status === 401) { + this.$toasted.error('Пользователь не зарегистрирован') + } + this.loading = false + } + }) + }, + beforeDestroy() { + ipcRenderer.removeAllListeners('VK_CODE') + }, methods: { + authorizeWithVK () { + window.open( + 'https://id.vk.com/auth?return_auth_hash=677a695397f7de30e9&redirect_uri=https%3A%2F%2Fwww.anilibria.tv%2Fpublic%2Fvk.php&redirect_uri_hash=12e7b3a47bed04dc26&force_hash=&app_id=5315207&response_type=code&code_challenge=&code_challenge_method=&scope=0&state=' + ,'targetWindow', + `toolbar=no, + location=no, + status=no, + menubar=no, + scrollbars=yes, + resizable=yes, + width=SomeSize, + height=SomeSize` + ) + }, /** * Authorize *