Signin

Other methods allow users to sign in to a microservice with the provided credentials. Look at the following signin function that processes requests that are sent to the /signin path:

fn signin((req, params): (HttpRequest<State>, Form<UserForm>))
-> FutureResponse<HttpResponse>
{
let fut = post_request("http://127.0.0.1:8001/signin", params.into_inner())
.map(move |id: UserId| {
req.remember(id.id);
HttpResponse::build_from(&req)
.status(StatusCode::FOUND)
.header(header::LOCATION, "/comments.html")
.finish()
});
Box::new(fut)
}

The function has two parameters: HttpRequest and Form. The first we need to get access to a shared State object. The second we need to extract the UserForm struct from the request body. We can also use the post_request function here, but expect it to return a UserId value in its response. The UserId struct is declared as follows:

#[derive(Deserialize)]
pub struct UserId {
id: String,
}

Since HttpRequest implements the RequestIdentity trait and we plugged in  IdentityService to App, we can call the remember method with a user's ID to associate the current session with a user.

After this, we create a response with the 302 status code, as we did in the previous handler, and redirect users to  the /comments.html page. But we have to build an HttpResponse instance from HttpRequest to keep the changes of the remember function call.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.61.218