/** * Session store system main include file * * Created on 2019/07/13. */ #ifndef HTTPD_UTILS_SESSION_H #define HTTPD_UTILS_SESSION_H #include "session_kvmap.h" #include "session_store.h" // Customary keys /** Session key for OK flash message */ #define SESS_FLASH_OK "flash_ok" /** Session key for error flash message */ #define SESS_FLASH_ERR "flash_err" /** Session key for a "logged in" flag. Value is 1 if logged in. */ #define SESS_AUTHED "authed" // .. /** * Redirect to /login form if unauthed, * but also retrieve the session key-value store for further use */ #define HTTP_GET_AUTHED_SESSION(kvstore, r) do { \ kvstore = httpd_req_find_session_and((r), SESS_GET_DATA); \ if (NULL == kvstore || NULL == sess_kv_map_get(kvstore, SESS_AUTHED)) { \ return httpd_redirect_to((r), "/login"); \ } \ } while(0) /** * Start or resume a session without checking for authed status. * When started, the session cookie is added to the response immediately. * * @param[out] kvstore - a place to store the allocated or retrieved session kvmap * @param[in] r - request * @return success */ esp_err_t HTTP_GET_SESSION(sess_kv_map_t **kvstore, httpd_req_t *r); /** * Redirect to the login form if unauthed. * This is the same as `HTTP_GET_AUTHED_SESSION`, except the kvstore variable is * not needed in the uri handler calling this, so it is declared internally. */ #define HTTP_REDIRECT_IF_UNAUTHED(r) do { \ sess_kv_map_t *_kvstore; \ HTTP_GET_AUTHED_SESSION(_kvstore, r); \ } while(0) #endif // HTTPD_UTILS_SESSION_H