Commit a14bfaa3 authored by Vadim's avatar Vadim

конец занятия №91

parent ac05badd
......@@ -7,5 +7,9 @@ module.exports = {
db: {
name: "shop",
url: "mongodb://localhost"
},
facebook: {
appId : '175634290592255',
secret: '8904c56e5400c12237cce6f85e1cefe0'
}
};
\ No newline at end of file
......@@ -36,6 +36,10 @@ const UserSchema = new Schema({
required: true,
enum: ['user', 'admin'],
default: 'user'
},
facebookId: {
type: Number,
minlength: 10
}
});
......
const express = require("express");
const User = require("./models/User");
const auth = require("./middleware/auth");
const config = require('./config');
const {nanoid} = require('nanoid');
const axios = require('axios');
const router = express.Router();
......@@ -49,6 +52,43 @@ const createRouter = () => {
return res.send(success);
});
router.post('/facebookLogin', async (req, res) => {
const inputToken = req.body.accessToken;
const accessToken = `${config.facebook.appId}|${config.facebook.secret}`;
const tokenUrl = `https://graph.facebook.com/debug_token?input_token=${inputToken}&access_token=${accessToken}`;
try{
const response = await axios.get(tokenUrl);
if(response.data.error) {
return res.status(401).send({message: 'Facebook token error'});
}
if(response.data.data.user_id !== req.body.userID) {
return res.status(401).send({message: 'Wrong User ID'});
}
let user = await User.findOne({facebookId: req.body.id});
if(!user) {
user = new User({
username: req.body.email,
email: req.body.email,
password: nanoid(),
facebookId: req.body.id
})
}
user.generateToken();
await user.save({validateBeforeSave: false});
return res.send({message: 'Login or register successful', user});
}catch (e) {
return res.status(401).send({message: 'Facebook token error', e});
}
});
return router;
};
......
......@@ -155,6 +155,14 @@
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
},
"axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"requires": {
"follow-redirects": "^1.10.0"
}
},
"balanced-match": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
......@@ -760,6 +768,11 @@
}
}
},
"follow-redirects": {
"version": "1.13.2",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.2.tgz",
"integrity": "sha512-6mPTgLxYm3r6Bkkg0vNM0HTjfGrOEtsfbhagQvbxDEsEkpNhw582upBaoRZylzen6krEmxXJgt9Ju6HiI4O7BA=="
},
"forwarded": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",
......
......@@ -11,6 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1",
"bcrypt": "^5.0.0",
"cors": "^2.8.5",
"express": "^4.17.1",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment