mirror of
https://github.com/muerwre/orchidmap-front.git
synced 2025-04-25 19:16:41 +07:00
auth: added login through vk iframe
This commit is contained in:
parent
678f2b17a4
commit
9427866e60
5 changed files with 75 additions and 3 deletions
|
@ -3,6 +3,7 @@ const guest = require('./auth/guest');
|
||||||
const list = require('./auth/list');
|
const list = require('./auth/list');
|
||||||
const check = require('./auth/check');
|
const check = require('./auth/check');
|
||||||
const vk = require('./auth/social/vk');
|
const vk = require('./auth/social/vk');
|
||||||
|
const vk_iframe = require('./auth/social/vk_iframe');
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
@ -10,5 +11,6 @@ router.get('/', check);
|
||||||
router.get('/list', list);
|
router.get('/list', list);
|
||||||
router.get('/guest', guest);
|
router.get('/guest', guest);
|
||||||
router.get('/social/vk', vk);
|
router.get('/social/vk', vk);
|
||||||
|
router.get('/social/vk_iframe', vk_iframe);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
57
backend/routes/auth/social/vk_iframe.js
Normal file
57
backend/routes/auth/social/vk_iframe.js
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
const { User } = require('../../../models');
|
||||||
|
const axios = require('axios');
|
||||||
|
const { generateUser } = require('../guest');
|
||||||
|
const { STRINGS } = require('../../../config/strings');
|
||||||
|
|
||||||
|
const fetchUserData = async (req, res) => {
|
||||||
|
const { query: { user_id, access_token } } = req;
|
||||||
|
|
||||||
|
const { data } = await axios.get(
|
||||||
|
'https://api.vk.com/method/users.get',
|
||||||
|
{
|
||||||
|
params: {
|
||||||
|
user_id,
|
||||||
|
fields: 'photo',
|
||||||
|
v: '5.67',
|
||||||
|
access_token,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
).catch(() => {
|
||||||
|
return res.send({ success: false, error: 'iframe auth failed' });
|
||||||
|
});
|
||||||
|
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = async (req, res) => {
|
||||||
|
const { response } = await fetchUserData(req, res);
|
||||||
|
|
||||||
|
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,4 +5,5 @@ export const API = {
|
||||||
CHECK_TOKEN: `${CLIENT.API_ADDR}/auth`,
|
CHECK_TOKEN: `${CLIENT.API_ADDR}/auth`,
|
||||||
GET_MAP: `${CLIENT.API_ADDR}/route`,
|
GET_MAP: `${CLIENT.API_ADDR}/route`,
|
||||||
POST_MAP: `${CLIENT.API_ADDR}/route`,
|
POST_MAP: `${CLIENT.API_ADDR}/route`,
|
||||||
|
VK_IFRAME_AUTH: `${CLIENT.API_ADDR}/auth/social/vk_iframe`,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { REHYDRATE } from 'redux-persist';
|
import { REHYDRATE } from 'redux-persist';
|
||||||
import { delay } from 'redux-saga';
|
import { delay } from 'redux-saga';
|
||||||
import { takeLatest, select, call, put, takeEvery, race, take } from 'redux-saga/effects';
|
import { takeLatest, select, call, put, takeEvery, race, take } from 'redux-saga/effects';
|
||||||
import { checkUserToken, getGuestToken, getStoredMap, postMap } from '$utils/api';
|
import { checkUserToken, getGuestToken, getStoredMap, getVkIframeUser, postMap } from '$utils/api';
|
||||||
import {
|
import {
|
||||||
hideRenderer,
|
hideRenderer,
|
||||||
setActiveSticker, setAddress,
|
setActiveSticker, setAddress,
|
||||||
|
@ -102,11 +102,19 @@ function* loadMapSaga(path) {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function* vkIframeAuth({ viewer_id, access_token }) {
|
||||||
|
const user = yield call(getVkIframeUser, { viewer_id, access_token });
|
||||||
|
|
||||||
|
if (user) return yield put(setUser(user));
|
||||||
|
}
|
||||||
|
|
||||||
function* mapInitSaga() {
|
function* mapInitSaga() {
|
||||||
const { hash } = getUrlData();
|
const { hash } = getUrlData();
|
||||||
const { viewer_id, access_token } = yield parseQuery(window.location.search);
|
const { viewer_id, access_token } = yield parseQuery(window.location.search);
|
||||||
|
// const viewer_id = '360004';
|
||||||
|
// const access_token = '35baba3da5ac109775bc818f9f04d031ffeeb5a0f36afb42c3ab9a45035b04a12e7c70478c19dde07752b';
|
||||||
|
|
||||||
if (viewer_id && access_token) console.log('GOT THEM!', { viewer_id, access_token });
|
if (viewer_id && access_token) yield call(vkIframeAuth, { viewer_id, access_token });
|
||||||
|
|
||||||
if (hash && /^#map/.test(hash)) {
|
if (hash && /^#map/.test(hash)) {
|
||||||
const [, newUrl] = hash.match(/^#map[:/?!](.*)$/);
|
const [, newUrl] = hash.match(/^#map[:/?!](.*)$/);
|
||||||
|
|
|
@ -31,4 +31,8 @@ export const postMap = ({
|
||||||
force,
|
force,
|
||||||
logo,
|
logo,
|
||||||
distance,
|
distance,
|
||||||
}).then(result => (result && result.data && result.data));
|
}).then(result => (result && result.data && result.data)).catch(() => null);
|
||||||
|
|
||||||
|
export const getVkIframeUser = ({ viewer_id: user_id, access_token }) => (axios.get(API.VK_IFRAME_AUTH, {
|
||||||
|
params: { user_id, access_token }
|
||||||
|
}).then(result => (result && result.data))).catch(() => null);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue