我不确定我做错了什么我已经按照文档进行了操作,看起来我正确设置了所有内容,但它只是不想授予我访问令牌并重定向我!我需要从 react-router 重定向到 auth/github 路由吗?
任何指导我正确方向并指出我的错误的帮助将不胜感激!
const GitHubStrategy = require('passport-local').Strategy;
// My client and secret are in the empty strings.
const GITHUB_CLIENT_ID = '';
const GITHUB_CLIENT_SECRET = '';
const GITHUB_CALLBACK_URL = '/auth/github/callback';
passport.serializeUser(function (user, cb) {
cb(null, user);
});
passport.deserializeUser(function (obj, cb) {
cb(null, obj);
});
passport.use(
new GitHubStrategy(
{
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: process.env.GITHUB_CALLBACK_URL,
},
function (accessToken, refreshToken, profile, cb) {
console.log(accessToken, refreshToken, profile);
return cb(null, profile);
}
)
);
module.exports = GitHubStrategy;
const router = require('express').Router();
const db = require('../models');
router.get('/users', (req, res) => {
db.User.find({
username: { $regex: new RegExp(req.query.q, 'i') },
}).then(() => console.log(req.params.q));
});
router.get(
callbackURL,
passport.authenticate('github', { failureRedirect: '/login' }),
function (req, res) {
// Successful authentication, redirect home.
res.redirect('/home');
}
);
module.exports = router;
const express = require('express');
const session = require('express-session');
const passport = require('./config/GithubPassport2');
const apiRoutes = require('./routes/apiRoutes');
const app = express();
const db = require('./models');
const cors = require('cors');
const PORT = process.env.PORT || 3001;
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
app.use(
session({ secret: 'keyboard cat', resave: true, saveUninitialized: true })
);
app.use(passport.initialize());
app.use(passport.sessions());
app.use(
cors({
origin: 'http://localhost:3000', // allow to server to accept request from different origin
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
credentials: true, // allow session cookie from browser to pass through
})
);
// Use apiRoutes
app.use('/api', apiRoutes);
// Send every request to the React app
// Define any API routes before this runs
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, './client/build/index.html'));
});
// Start the API server
db.sequelize.sync().then(function () {
app.listen(PORT, function () {
console.log(` ==> API Server now listening on PORT ${PORT}!`);
});
});
import React from 'react';
import { Link, Redirect } from 'react-router-dom';
function SignIn({ icon }) {
return (
<section className='container-fluid'>
<section className='row'>
<section className='col-12 col-sm-6 col-md-3' />
<form>
<div className='form-group'></div>
<Link
className={icon}
to='/auth/github'
style={{ paddingLeft: '40px' }}>
Sign in with Github
</Link>
</form>
</section>
</section>
);
}
SignIn.defaultProps = {
icon: 'fab fa-github',
};
export default SignIn;```