OpusCapita / filemanager

React based FileManager for browser ( + FS REST API for Node.js and Express)
https://demo.core.dev.opuscapita.com/filemanager/master/?currentComponentName=FileManager&maxContainerWidth=100%25&showSidebar=false
Apache License 2.0
565 stars 122 forks source link

Failed to work with Passport.js #239

Closed cjs840312 closed 6 years ago

cjs840312 commented 6 years ago

Hi, I'm trying to protect the API by Passport.js I use the localStrategy with sessions and the code works well when no authorize function applied but it can't fatch the data if I add the authorize function ( last line in server.js) Authorization works well for other url so it may not be passport's problem.

in client

import React from "react";
import { FileManager, FileNavigator } from "@opuscapita/react-filemanager";
import connectorNodeV1 from "@opuscapita/react-filemanager-connector-node-v1";

const apiOptions = Object.assign(
    {},
    connectorNodeV1.apiOptions,
    { apiRoot: "/courseResource" },
);

const fileManager = () => (
    <div style={{ height: "100%", width: "100%" }}>
        <FileManager>
            <FileNavigator
                id="filemanager"
                api={connectorNodeV1.api}
                apiOptions={apiOptions}
                capabilities={connectorNodeV1.capabilities}
                initialResourceId="Lw"
                listViewLayout={connectorNodeV1.listViewLayout}
                viewLayoutOptions={connectorNodeV1.viewLayoutOptions}
            />
        </FileManager>
    </div>
);

export default fileManager;

in server.js

server.use("/api", filemanagerMiddleware({
    fsRoot: "static/",
    rootName: "data",
    readOnly: false,
    logger: {
        log: () => {},
        info: () => {},
        warn: () => {},
        error: () => {},
    },
}));

server.use(
    ["/api"],
    passport.authorize(
        "local",
        {
            failureRedirect: "/",
        },
    ),
);
kvolkovich-sc commented 6 years ago

Hello, @cjs840312!

Did you found solution?

cjs840312 commented 6 years ago

Not yet, let me first check it out if it is really filemanager's problem. Sorry for bothering. I'll update if I found the root cause

cjs840312 commented 6 years ago

It seem's like passport.js doesn't work well with axios

cjs840312 commented 6 years ago

I solve it in a dirty way. I rewrite the authorize part and things fixed

authRouter.use(
    ["/api"],
    (req, res, next) => {
        if (req.session.passport) {
            next();
        } else {
            res.redirect("/");
        }
    },
);