私の新しいプロジェクトでは、webmachine と mochiweb を使用したいと考えています。私が最初にやりたいことは、認証です。
「dispatch.conf」を編集して、次のようなリソースを作成します。
{["auth"], my_res_auth, []}.
{["protected"], my_res_protected, []}.
{['*'], my_res_default, []}.
「保護された」リソースにアクセスするときに、ログインしていない場合は「認証」リソースにリダイレクトしたいと考えています。「認証」リソースには、ユーザー名とパスワードを含む Web フォームが含まれており、すべての認証作業を行います。
そのようなコードを my_res_protected.erl の中に入れました:
is_authorized(ReqData, State) ->
case my_auth:is_authorized(ReqData) of
true -> {true, ReqData, State};
false ->
% here should be something to redirect user to "auth" resource
% currently i put such thing, which is incorrect:
{true, wrq:do_redirect(true, wrq:set_resp_header("location", "/auth", ReqData)), State}
% dont know what should i put instead of "true"
end.
私はそれを行う方法のいくつかの例をグーグルで検索しましたが、認証が必要なすべてのリソースにこの関数を配置する必要があるのは好きではありません。
それを行う方法はありますか?