Получаю такю ошибку:
Access to XMLHttpRequest at '
https://oauth.vk.com/authorize?response_type=code&...' (redirected from '
localhost:3000/auth/vkontakte') from origin '
localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Запрос к серверу:
axios.get(api).then((response) => {
console.log(response.data)
})
Код сервера:
const passport = require('passport');
const VKontakteStrategy = require('passport-vkontakte').Strategy;
app.use(require("cookie-parser")());
app.use(require("body-parser").urlencoded({ extended: true }));
app.use(
require("express-session")({
secret: "keyboard cat",
resave: true,
saveUninitialized: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
passport.use(
new VKontakteStrategy(
{
clientID: 7937563, // VK.com docs call it 'API ID', 'app_id', 'api_id', 'client_id' or 'apiId'
clientSecret: "тут типа секретного ключа",
callbackURL: "http://localhost:3000/auth/vkontakte/callback",
},
function myVerifyCallbackFn(
accessToken,
refreshToken,
params,
profile,
done
) {
// Now that we have user's `profile` as seen by VK, we can
// use it to find corresponding database records on our side.
// Also we have user's `params` that contains email address (if set in
// scope), token lifetime, etc.
// Here, we have a hypothetical `User` class which does what it says.
User.findOrCreate({ vkontakteId: profile.id })
.then(function (user) {
done(null, user);
})
.catch(done);
}
)
);
// User session support for our hypothetical `user` objects.
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
User.findById(id)
.then(function (user) {
done(null, user);
})
.catch(done);
});
app.get("/auth/vkontakte", passport.authenticate("vkontakte"));
app.get(
"/auth/vkontakte/callback",
passport.authenticate("vkontakte", {
successRedirect: "/",
failureRedirect: "/login",
})
);
app.get("/", function (req, res) {
//Here you have an access to req.user
res.json(req.user);
});
Пробовал JSONP, но выдаёт ошибку 401