mirror of
https://github.com/muerwre/orchidmap-front.git
synced 2025-04-25 11:06:40 +07:00
auth: cleaned code
This commit is contained in:
parent
c2a202307b
commit
a0c1f00be4
6 changed files with 1 additions and 106 deletions
|
@ -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;
|
||||
|
|
|
@ -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 });
|
||||
}
|
||||
};
|
||||
|
|
@ -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`,
|
||||
};
|
||||
|
|
|
@ -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 });
|
||||
|
|
|
@ -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 });
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue