Answer the question
In order to leave comments, you need to log in
How to set up passport-vkontakte correctly?
Good afternoon!
I do authorization on the site using VK, using passport-vkontakte , on the one hand, everything is according to the instructions, on the other, something does not work out.
(Yes, and the examples in the documentation are, to put it mildly, so-so. I didn’t see anything intelligible in them, so if there are ready-made implementations, please send links, I will assemble this constructor by example)
This is what I have now reached:
var passport = require('passport');
var AuthVKStrategy = require('passport-vkontakte').Strategy;
app.use(require('express-session')({secret:'keyboard cat', resave: true, saveUninitialized: true}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new AuthVKStrategy(
{
clientID: vk_app_id, // VK.com docs call it 'API ID', 'app_id', 'api_id', 'client_id' or 'apiId'
clientSecret: vk_app_secret,
callbackURL: "http://localhost:3000/auth/vkontakte/callback"
},
function(accessToken, refreshToken, params, profile, done) {
// User.findOrCreate({ vkontakteId: profile.id }, function (err, user) {
// return done(err, user);
// });
return done(null, profile);
}
));
passport.serializeUser(function(user, done) {
// console.log(user.id);
console.log(user);
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
app.get('/auth/vkontakte',
passport.authenticate('vkontakte', {
scope: ['status', 'email', 'friends', 'notify'],
profileFields: ['email', 'city', 'bdate']
}),
function(req, res){
// The request will be redirected to vk.com for authentication, with
// extended permissions.
});
app.get('/auth/vkontakte/callback',
passport.authenticate('vkontakte', {
successRedirect: 'http://localhost:3000/vk',
failureRedirect: 'http://localhost:3000/fail'
})
);
app.get('/vk', function(req, res) {
//Here you have an access to req.user
res.send('ok')
// res.json(req.user)
console.log(req.user);
});
app.get('/logout', function (req, res) {
req.logout();
res.redirect(`http://localhost:3002`);
});
passport.serializeUser
and passport.deserializeUser
whether they are needed at all. + when executing the code, an error occurs ReferenceError: User is not defined
var cors = require('cors')
app.use(cors());
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question