Ошибка при звонке на passport-facebook-token - PullRequest
0 голосов
/ 22 мая 2019

Я реализовал passport-facebook-token, импортировал его из App.ts, загрузился в свое промежуточное ПО, внутри моего файла Router я импортировал 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 и function ...

...