拨打护照-facebook-令牌时出错

时间:2019-05-22 18:12:04

标签: node.js typescript passport-facebook passport-facebook-token

我实现了passport-facebook-token,从App.ts导入了它,引导到中间件中,在我的路由器文件中,我导入了来自App的Passport,并将其步进到/ auth / facebook调用中,但是它总是返回:InternalOAuthError:无法获取用户个人资料

我的代码在这里

Router.ts

import { AuthController } from "../controllers/AuthController";
import { UserController } from "../controllers/UserController";
import { EstacaoController } from "../controllers/EstacaoController";
import { EstacaoAreaController } from '../controllers/EstacaoAreaController';
import { CalculateRouterController } from '../controllers/CalculateRouterController';
import authVerify from '../middleware/authverify';
import {Request, Response} from 'express'
import facebookStrategy from 'passport-facebook';
import * as passport from 'passport';

import {Passport} from '../App';
export default class Router {

    private authController: AuthController;
    private userController: UserController;
    private estacaoController: EstacaoController;
    private estacaoAreaController: EstacaoAreaController;
    private calculateRouterController: CalculateRouterController;

    private BASE_URL: string = "/api/v1";

    private AUTH_URL: string = this.BASE_URL + "/auth";
    private FACEBOOK_AUTH_URL: string = this.BASE_URL + "/auth/facebook";
    private GOOGLE_AUTH_URL: string = this.BASE_URL + "/auth/google";
    private TWITTER_AUTH_URL: string = this.BASE_URL + "/auth/twitter";

    private REGISTER_URL: string = this.BASE_URL + "/register";

    private ESTACAO_URL: string = this.BASE_URL + "/estacao";
    private ESTACAO_URL_GET: string = this.BASE_URL + "/estacao/pontos";

    private ESTACAOAREA_URL: string = this.BASE_URL + "/estacao/area";

    private CALCULAR_ROTA_URL: string = this.BASE_URL + "/calcular/rota/:origem/:destino"
    constructor(authController: AuthController, userController: UserController,
        estacaoController: EstacaoController, estacaoAreaController: EstacaoAreaController,
        calculateRouterController: CalculateRouterController) {
        this.authController = authController;
        this.userController = userController;
        this.estacaoController = estacaoController;
        this.estacaoAreaController = estacaoAreaController
        this.calculateRouterController = calculateRouterController
    }

    public startWith(app) {

        // AUTH
        app.post('/auth/facebook', Passport.authenticate('facebookToken',  {session: false}))


        app.route(this.AUTH_URL).post(this.authController.getUser);
        // app.route(this.FACEBOOK_AUTH_URL).post(Auth().authenticateFacebook(), this.authController.getFacebookOrGoogleUser);
        // app.route(this.GOOGLE_AUTH_URL).all(Auth().authenticateGoogle()).post(this.authController.getFacebookOrGoogleUser);


        // REGISTER
        app.route(this.REGISTER_URL).post(this.userController.createUser);

        //Estacao
        app.route(this.ESTACAO_URL).all(authVerify).post(this.estacaoController.createNewStation);
        app.route(this.ESTACAO_URL_GET).all(authVerify).get(this.estacaoController.getStation);

        //EstacaoArea
        app.route(this.ESTACAOAREA_URL).post(this.estacaoAreaController.createNewAreaStation);

        //calcularRota
        app.route(this.CALCULAR_ROTA_URL).get(this.calculateRouterController.calculateRouterNow);

    }
}

App.ts

import * as express from 'express'
import * as bodyParser from 'body-parser';
import { Server, createServer } from 'http';
import { sequelize } from "./db/sequelize";
import Router from './routes/Router';
import * as io from 'socket.io';
import * as passport from 'passport'
import { AuthController } from './controllers/AuthController';
import { UserRepository } from './repository/UserRepository';
import { UserController } from './controllers/UserController';
import { EstacaoController } from './controllers/EstacaoController';
import { EstacaoRepository } from './repository/EstacaoRepository';
import { EstacaoAreaController } from './controllers/EstacaoAreaController';
import { EstacaoAreaRepository } from './repository/EstacaoAreaRepository';
import { LinhasTrilhosRepository } from './repository/LinhasTrilhosRepository';
import { CalculateRouterController } from './controllers/CalculateRouterController';
import { CalculateRouterRepository } from './repository/CalculateRouterRepository';
import { initializeStrategies } from './middleware/-initialize'
import { Database } from './db/Database';
// import { BilheteRepository } from './repository/BilheteRepository';
// import { BilheteController } from './controllers/BilheteController';


export const Passport = passport;

export class App {

    public static readonly PORT: number = 3000;
    private app: express.Application;
    private port: string | number;
    private server: Server;
    public auth;

    constructor() {
        this.createApp();
        this.config();
        this.createServer();
        this.initializeStrategies();
        this.middleware();
        this.listen();

        new Database();

    }

    private async listen() {
        try {
            await sequelize.sync({ force: false })

            this.server.listen(this.port, () => {
                console.log('Running server on port %s', this.port);
            });
        } catch (e) {
            console.log("FATAL ERROR: COULD NOT CONNECT TO DATABASE.");
            console.log("ERROR:" + e);
        }
    }

    private createApp(): void {
        this.app = express();
    }

    private config(): void {
        this.port = process.env.PORT || App.PORT;
    }



    private createServer(): void {
        this.server = createServer(this.app);
    }

    private middleware(): void {
        this.app.use(bodyParser.json());
        this.app.use(bodyParser.urlencoded({ extended: false }));
        this.app.use(passport.initialize())
        this.app.use(passport.session())

        const userRepository = new UserRepository();
        const estacaoRepository = new EstacaoRepository();
        const estacaoAreaRepository = new EstacaoAreaRepository();
        const calculateRouterRepository = new CalculateRouterRepository();
        const linhasTrilhosRepository = new LinhasTrilhosRepository();

        new Router(new AuthController(userRepository),
            new UserController,
            new EstacaoController(estacaoRepository),
            new EstacaoAreaController(estacaoAreaRepository),
            new CalculateRouterController(calculateRouterRepository,
                linhasTrilhosRepository))
            .startWith(this.app);
    }

    private initializeStrategies(): void {
            initializeStrategies(passport)
    }
    public getApp(): express.Application {
        return this.app;
    }
}

我已经更改了所有内容,并且继续出现错误,我不知道是否需要将Passport传递到特定的地方,但是仍然存在错误,我有与此类似的代码,可以在javascript和函数中使用...

0 个答案:

没有答案