前提
showStatusファンクションが問題なんですが、つねに"Not authenticated"が返ってきます。
実現したいこと
If文で"Authenticated"かどうか判定。ない場合は、"Not authenticated"で、認証済みの場合は、"Authenticated"が返ってくる。
authenticateファンクションでは、認証されていない場合は、パスワードとユーザーネーム確認。
一致した場合は、/へ進む、一致しないばあは、404が返る、すでに認証済みの場合は、GETと同様の動きになる。
別の質問ですが、authenticateファンクションで、最後のElse分をresponse.body="Authenticated";からresponse.body=showStatusと変えることはできますか?
発生している問題・エラーメッセージ
showStatusファンクションで、つねに"Not authenticated"が返ってきます。
該当のソースコード
App.js
import { Application, Router } from "https://deno.land/x/oak@v10.6.0/mod.ts"; import { Session } from "https://deno.land/x/oak_sessions@v3.5.1/mod.ts"; const app = new Application(); const router = new Router(); const session = new Session(); app.use(session.initMiddleware()); const showStatus = async({ response,state }) => { const authenticated = await state.session.get("authenticated"); if(authenticated){ await state.session.set("authenticated", true); response.body = "Authenticated"; } response.body = "Not authenticated"; }; const authenticate = async ({ request, response, state }) => { const authenticated = await state.session.get("authenticated"); if(!authenticated){ const body = request.body(); const params = await body.value; const username = params.get("username"); const password = params.get("password"); if (username === "Minuteman" && password === "00000000") { await state.session.set("authenticated", true); response.redirect("/"); } else { response.status = 401; } }else{ response.body="Authenticated"; } }; router.get("/", showStatus); router.post("/", authenticate); app.use(router.routes()); app.listen({ port: 7777 });
errorMiddleware.js
const errorMiddleware = async (context, next) => { try { await next(); } catch (e) { console.log(e); } }; export { errorMiddleware };
試したこと
if文で、(!authenticated)と反対にしてみましたが、結果は同じでした。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。