0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Express.jsにPassport.jsで、任意のフォルダ配下のみをBasic認証する

Posted at

概要

expressを使って特定フォルダのみパスワードを掛けたい。

参考サイト、、、っていうかほぼ以下のページからゴニョゴニョした。
ありがとうございます。
Express.jsにPassport.jsで、任意のフォルダ配下のみをBasic認証する
https://qiita.com/hoshimado/items/272fba1aeb728e14f278

const express = require("express");
const PORT = 3000;
const app = express();
//
const passport = require("passport");
const passportHttp = require("passport-http");
passport.use(new passportHttp.BasicStrategy(
    function (username, password, done) {
      if(username=="user" && password=="pass"){
        return done(null,true);
      }else{
        return done(null, false);
      }
    }
));
app.use("/admin", passport.authenticate("basic",{session: false}), express.static("public/admin") );
//
app.use("/", express.static("public/") );

app.listen(PORT, () => {
  console.log("app listening on port " + PORT + " " + new Date());
});

結論

動いた!ばんざーい。
いまはただそれしかない。

しかし、express-basic-authを使うほうがいまどきなのかしらん。
express全体にかけることはできても、個別のフォルダにかけるのがなんかわからんかった。
(portを2つ開けて2つのexpressを立ち上げて、publicとprivateに分けるみたいののでいいのかなぁっともおもったけどね。それはちょっとね。)

参考サイト

Express.jsにPassport.jsで、任意のフォルダ配下のみをBasic認証する
https://qiita.com/hoshimado/items/272fba1aeb728e14f278

Basic HTTP authentication with Node and Express 4
☑ express-basic-auth is what you want
https://www.titanwolf.org/Network/q/1ad72a53-1a15-4d32-9d05-3b39b3148093/y

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?