здесь вы - рабочая копия для вашей установки:
, во-первых, обязательно включите стратегию с сеансами, используя app.use (passport.initialize ());и чтобы включить сериализацию и десериализацию, именно здесь ваш код будет получать авторизацию с сервера.
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const Twitch = require('./model');
const mongoose = require("mongoose");
const passport = require('passport');
const twitchStrategy = require("passport-twitch").Strategy;
mongoose
.connect(
"<mongourl>"
)
.then(() => {
console.log("connected to database!");
})
.catch(() => {
console.log("connection failed");
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-requested-With, Content-Type, Accept"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS, PUT"
);
next();
});
app.use(passport.initialize());
passport.use(new twitchStrategy({
clientID: "<clientid>",
clientSecret: "<clientsecret>",
callbackURL: "http://localhost:3000/auth/twitch/callback",
scope: "user_read"
},
function(accessToken, refreshToken, profile, done) {
twitch.save({ twitchId: profile.id }, function (err, user) {
console.log(user);
return done(err, user);
});
}
));
passport.serializeUser(function(user, done) {
console.log(user);
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
app.get("/", function (req, res) {
res.send(`<html><head></head><body>Here</body></html>`);
});
app.get("/auth/twitch", passport.authenticate("twitch"));
app.get("/auth/twitch/callback" ,passport.authenticate("twitch"), function(req, res) {
res.redirect("/");
});
module.exports = app;
без сеансов
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const passport = require('passport');
const twitchStrategy = require("passport-twitch").Strategy;
const axios = require('axios');
const twitchAxios = axios.create({
baseURL: 'http://localhost:3000',
timeout: 1000,
headers:{
"Content-type": "application/json",
"Accept": "application/json",
"Authorization": "bearer TOKEN"
}
});
mongoose
.connect(
""
)
.then(() => {
console.log("connected to database!");
})
.catch(() => {
console.log("connection failed");
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-requested-With, Content-Type, Accept"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, PUT, DELETE, OPTIONS, PUT"
);
next();
});
app.use(passport.initialize());
passport.use(new twitchStrategy({
clientID: "",
clientSecret: "",
callbackURL: "http://localhost:3000/auth/twitch/callback",
scope: "user_read"
},
function(accessToken, refreshToken, profile, done) {
twitch.save({ twitchId: profile.id }, function (err, user) {
return done(err, user);
});
}
));
app.get("/", function (req, res) {
res.send(`<html><head></head><body>Here</body></html>`);
});
app.get("/auth/twitch", passport.authenticate("twitch",{session: false}));
app.get("/auth/twitch/callback", function(req, res) {
twitchAxios.get('/').then(console.log)
res.redirect("/");
});
module.exports = app;