mirror of
https://github.com/muerwre/orchidmap-front.git
synced 2025-04-25 11:06:40 +07:00
backend: initial log in dialog
This commit is contained in:
parent
6f6e6ae6d7
commit
8bba2ff1ba
8 changed files with 83 additions and 7 deletions
58
backend/routes/auth/social/vk.js
Normal file
58
backend/routes/auth/social/vk.js
Normal file
|
@ -0,0 +1,58 @@
|
|||
const { User } = require('../../../models/User');
|
||||
const axios = require('axios');
|
||||
const { generateGuest, generateRandomUrl, generateUser } = require('../guest');
|
||||
//
|
||||
// const generateTokenUrl = (host, code) => (
|
||||
// `https://oauth.vk.com/access_token?client_id=5987644&redirect_uri=http://${host}/` +
|
||||
// `auth/social/vk&client_secret=Z71DsxoMF7PS9kayLuks&code=${code}`
|
||||
// );
|
||||
|
||||
const fetchUserData = async (req) => {
|
||||
const { query: { code } } = req;
|
||||
const host = req.get('host');
|
||||
|
||||
const { data: { access_token, user_id } } = await axios.get(
|
||||
'https://oauth.vk.com/access_token',
|
||||
{
|
||||
params: {
|
||||
client_id: 5987644,
|
||||
client_secret: 'Z71DsxoMF7PS9kayLuks',
|
||||
code,
|
||||
redirect_uri: `http://${host}/auth/social/vk`,
|
||||
}
|
||||
}
|
||||
).catch(() => ({ data: { } }));
|
||||
|
||||
const { data } = await axios.get(
|
||||
'https://api.vk.com/method/users.get',
|
||||
{
|
||||
params: {
|
||||
user_id,
|
||||
fields: 'photo',
|
||||
v: '5.67',
|
||||
access_token,
|
||||
}
|
||||
}
|
||||
).catch(() => ({ data: { response: [] } }));
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
module.exports = async (req, res) => {
|
||||
const { response } = await fetchUserData(req);
|
||||
|
||||
const {
|
||||
first_name = '', last_name = '', photo = '', id = ''
|
||||
} = response[0];
|
||||
|
||||
const newUser = await generateUser(`vk:${id}`, 'vk');
|
||||
const user = {
|
||||
...newUser, first_name, last_name, photo
|
||||
};
|
||||
|
||||
// todo: error handling
|
||||
// console.log('USE', user);
|
||||
|
||||
res.send(user);
|
||||
};
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue