@@ -16,23 +16,34 @@ init(Req0, State) ->
1616 login (cowboy_req :method (Req0 ), Req0 , State ).
1717
1818login (<<" POST" >>, Req0 , State ) ->
19- {ok , Body , _ } = cowboy_req :read_urlencoded_body (Req0 ),
20- AccessToken = proplists :get_value (<<" access_token" >>, Body ),
21- case rabbit_mgmt_util :is_authorized_user (Req0 , # context {}, <<" " >>, AccessToken , false ) of
22- {true , Req1 , _ } ->
23- NewBody = [" <html><head></head><body><script src='js/prefs.js'></script><script type='text/javascript'>" ,
24- " set_token_auth('" , AccessToken , " '); window.location = '" , rabbit_mgmt_util :get_path_prefix (),
25- " /'</script></body></html>" ],
26- Req2 = cowboy_req :reply (200 , #{<<" content-type" >> => <<" text/html; charset=utf-8" >>}, NewBody , Req1 ),
27- {ok , Req2 , State };
28- {false , ReqData1 , Reason } ->
29- Home = cowboy_req :uri (ReqData1 , #{path => rabbit_mgmt_util :get_path_prefix () ++ " /" , qs => " error=" ++ Reason }),
30- ReqData2 = cowboy_req :reply (302 ,
31- #{<<" Location" >> => iolist_to_binary (Home ) },
32- <<>>, ReqData1 ),
33- {ok , ReqData2 , State }
34- end ;
19+ {ok , Body , _ } = cowboy_req :read_urlencoded_body (Req0 ),
20+ AccessToken = proplists :get_value (<<" access_token" >>, Body ),
21+ case rabbit_mgmt_util :is_authorized_user (Req0 , # context {}, <<" " >>, AccessToken , false ) of
22+ {true , Req1 , _ } ->
23+ SetCookie = cowboy_req :set_resp_cookie (<<" token" >>, AccessToken , Req1 ),
24+ Home = cowboy_req :uri (SetCookie , #{
25+ path => rabbit_mgmt_util :get_path_prefix () ++ " /"
26+ }),
27+ Redirect = cowboy_req :reply (302 , #{
28+ <<" Location" >> => iolist_to_binary (Home )
29+ }, <<>>, SetCookie ),
30+ {ok , Redirect , State };
31+ {false , ReqData1 , Reason } ->
32+ replyWithError (Reason , ReqData1 , State )
33+ end ;
3534
3635login (_ , Req0 , State ) ->
3736 % % Method not allowed.
3837 {ok , cowboy_req :reply (405 , Req0 ), State }.
38+
39+ replyWithError (Reason , Req , State ) ->
40+ Home = cowboy_req :uri (Req , #{
41+ path => rabbit_mgmt_util :get_path_prefix () ++ " /" ,
42+ qs => " error=" ++ Reason
43+ }),
44+ Req2 = cowboy_req :reply (302 , #{
45+ <<" Location" >> => iolist_to_binary (Home )
46+ }, <<>>, Req ),
47+ {ok , Req2 , State }.
48+
49+
0 commit comments