From a0c1f00be4d3fce8136009f2a4b0a484820c19d2 Mon Sep 17 00:00:00 2001 From: muerwre Date: Mon, 10 Dec 2018 14:47:03 +0700 Subject: [PATCH] auth: cleaned code --- backend/routes/auth.js | 2 - backend/routes/auth/social/vk_iframe.js | 65 ------------------------- src/constants/api.js | 1 - src/redux/user/actions.js | 2 - src/redux/user/constants.js | 2 - src/redux/user/sagas.js | 35 +------------ 6 files changed, 1 insertion(+), 106 deletions(-) delete mode 100644 backend/routes/auth/social/vk_iframe.js diff --git a/backend/routes/auth.js b/backend/routes/auth.js index 100c3c4..b102f0f 100644 --- a/backend/routes/auth.js +++ b/backend/routes/auth.js @@ -3,7 +3,6 @@ const guest = require('./auth/guest'); const list = require('./auth/list'); const check = require('./auth/check'); const vk = require('./auth/social/vk'); -const vk_iframe = require('./auth/social/vk_iframe'); const router = express.Router(); @@ -11,6 +10,5 @@ router.get('/', check); router.get('/list', list); router.get('/guest', guest); router.get('/social/vk', vk); -router.get('/social/vk_iframe', vk_iframe); module.exports = router; diff --git a/backend/routes/auth/social/vk_iframe.js b/backend/routes/auth/social/vk_iframe.js deleted file mode 100644 index 53c289b..0000000 --- a/backend/routes/auth/social/vk_iframe.js +++ /dev/null @@ -1,65 +0,0 @@ -const { User } = require('../../../models'); -const axios = require('axios'); -const { generateUser } = require('../guest'); - -const fetchUserData = async (req, res) => { - const { query: { user_id, access_token } } = req; - - const result = await axios.get( - 'http://api.vk.com/method/users.get', - { - params: { - user_id, - fields: 'photo', - v: '5.67', - access_token, - } - } - ).catch(() => { - res.send({ success: false, error: 'iframe auth failed' }); - }); - - console.log("RESULT!", result); - const { data } = result; - if (!data) { - console.log('OOOPS!', result); - res.send({ success: false, error: 'iframe auth failed', result }); - } - - return data; -}; - -module.exports = async (req, res) => { - const { response } = await fetchUserData(req, res); - - console.log('RESP', response); - - const { - first_name = '', last_name = '', photo = '', id = '' - } = response[0]; - - console.log('GOT IFRAME USER?', { first_name, last_name, photo, id }); - - const newUser = await generateUser(`vk:${id}`, 'vk'); - const name = `${first_name} ${last_name}`; - const user = { - ...newUser, first_name, last_name, photo, name, - }; - - const auth = await User.findOne({ _id: user._id }).populate('routes'); - - if (auth) { - await auth.set({ - first_name, last_name, name, photo - }).save(); - - res.send({ success: true, ...user }); - } else { - const created = await User.create(user, (err, result) => { - return result.toObject(); - }); - - res.send({ success: true, ...user, ...created }); - } -}; - diff --git a/src/constants/api.js b/src/constants/api.js index d18a817..9767b3f 100644 --- a/src/constants/api.js +++ b/src/constants/api.js @@ -5,5 +5,4 @@ export const API = { CHECK_TOKEN: `${CLIENT.API_ADDR}/auth`, GET_MAP: `${CLIENT.API_ADDR}/route`, POST_MAP: `${CLIENT.API_ADDR}/route`, - VK_IFRAME_AUTH: `${CLIENT.API_ADDR}/auth/social/vk_iframe`, }; diff --git a/src/redux/user/actions.js b/src/redux/user/actions.js index 2a9e102..d788c39 100644 --- a/src/redux/user/actions.js +++ b/src/redux/user/actions.js @@ -48,5 +48,3 @@ export const setReady = ready => ({ type: ACTIONS.SET_READY, ready }); export const gotVkUser = user => ({ type: ACTIONS.GOT_VK_USER, user }); export const keyPressed = ({ key }) => ({ type: ACTIONS.KEY_PRESSED, key }); - -export const iframeLoginVk = payload => ({ type: ACTIONS.IFRAME_LOGIN_VK, ...payload }); diff --git a/src/redux/user/constants.js b/src/redux/user/constants.js index 4a60ebf..a25340d 100644 --- a/src/redux/user/constants.js +++ b/src/redux/user/constants.js @@ -48,6 +48,4 @@ export const ACTIONS = ({ GOT_VK_USER: 'GOT_VK_USER', KEY_PRESSED: 'KEY_PRESSED', - - IFRAME_LOGIN_VK: 'IFRAME_LOGIN_VK', }: { [key: String]: String }); diff --git a/src/redux/user/sagas.js b/src/redux/user/sagas.js index 9b15044..101191f 100644 --- a/src/redux/user/sagas.js +++ b/src/redux/user/sagas.js @@ -12,7 +12,7 @@ import { setSaveOverwrite, setSaveSuccess, setTitle, setUser } from '$redux/user/actions'; -import { getUrlData, parseQuery, pushPath } from '$utils/history'; +import { getUrlData, pushPath } from '$utils/history'; import { editor } from '$modules/Editor'; import { ACTIONS } from '$redux/user/constants'; import { MODES } from '$constants/modes'; @@ -28,7 +28,6 @@ import { } from '$utils/renderer'; import { LOGOS } from '$constants/logos'; import { DEFAULT_PROVIDER } from '$constants/providers'; -import { store } from '$redux/store'; const getUser = state => (state.user.user); const getState = state => (state.user); @@ -102,27 +101,8 @@ function* loadMapSaga(path) { return map; } -function* iframeLoginVkSaga({ viewer_id: user_id, access_token, auth_key }) { - const data = yield call(getVkUserInfo, { user_id, access_token }); - - console.log('PARAMS', { user_id, access_token, auth_key }); - - if (data) console.log('GOT DATA!', data); - // if (user) return yield put(setUser(user)); - - // return null; - return; -} - function* mapInitSaga() { const { hash } = getUrlData(); - const { viewer_id, access_token, auth_key } = yield parseQuery(window.location.search); - - // const viewer_id = '360004'; - // const access_token = '35baba3da5ac109775bc818f9f04d031ffeeb5a0f36afb42c3ab9a45035b04a12e7c70478c19dde07752b'; - - // if (viewer_id && access_token) yield call(vkIframeAuth, { viewer_id, access_token }); - if (viewer_id && access_token) yield put(iframeLoginVk({ viewer_id, access_token, auth_key })); if (hash && /^#map/.test(hash)) { const [, newUrl] = hash.match(/^#map[:/?!](.*)$/); @@ -134,25 +114,14 @@ function* mapInitSaga() { const { path, mode } = getUrlData(); - if (path) { const map = yield call(loadMapSaga, path); - // const map = yield call(getStoredMap, { name: path }); if (map) { - // yield editor.setData(map); - // yield editor.fitDrawing(); - // yield put(setChanged(false)); - if (mode && mode === 'edit') { yield put(setEditing(true)); editor.startEditing(); - // yield call(startEditingSaga); // <-- this is working - // yield put(setEditing(true)); - // editor.startEditing(); } else { - // yield put(setEditing(false)); // <-- this is working - // yield call(stopEditingSaga); yield put(setEditing(false)); editor.stopEditing(); } @@ -452,6 +421,4 @@ export function* userSaga() { yield takeLatest(ACTIONS.GOT_VK_USER, gotVkUserSaga); yield takeLatest(ACTIONS.KEY_PRESSED, keyPressedSaga); - - yield takeLatest(ACTIONS.IFRAME_LOGIN_VK, iframeLoginVkSaga); }