I'm using a guard to authenticated a user.
How can i easily redirect the user to the login page if a guard fail (redirect to /login in my example) ?
#[rocket::async_trait]
impl<'r> FromRequest<'r> for User {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> Outcome<User, ()> {
let db = request.guard::<&State<Db>>().await.succeeded().unwrap();
/* Get user_id cookie */
let c = request.cookies().get_private("user_id");
match c {
Some(c) => {
...
Outcome::Success(user)
},
None => {
Outcome::Failure((Status::BadRequest, ()))
}
}
}
}
#[get("/")]
async fn home(user: User) -> Template {
...
}
#[get("/login")]
async fn login() -> Template {
...
}
As doc illustrate: