Building frontend code on command
Code on demand is where the backend server directly executes code on the frontend. This constraint is optional and not widely used. However, it can be useful as it gives the backend server the right to decide as and when code is executed on the frontend. Getting JavaScript to run in the browser from our server when the API endpoint is called can be achieved by sending a script in the HTML that we return. We can do this with the following code:
// nanoservices/auth/networking/actix_server/
// src/auth/logout.rs
use actix_web::HttpResponse;
pub async fn logout() -> HttpResponse {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(
"<html>\
<script>\
localStorage.removeItem('token'); \
window.location.replace(
document.location.origin);\
</script>\
</html>"
)
}
Here, we can see that we are removing the user token...