Rocket: Serve NamedFile or Redirect

I am kinda new to Rust and I am using Rocket to write a simple server. I want the request to the homepage would redirect to the login page if there is no auth token in Cookie.
So here is my code:

async fn login() -> Option<NamedFile> {

async fn index(cookies: &CookieJar<'_>) -> Option<NamedFile> {

    match auth::auth(&cookies).await {
        Ok(_) =>{

        Err(_) => {
            // Redirect::to(uri!(login));
            // NamedFile::open("static/login.html").await.ok()

Nothing fancy really, but I just want to know what is the correct way to do the redirect to login page action.

I have 3 options:

  1. Call the function login() from function index(), which is what I am doing.
  2. Serve that login.html file directly within the index() function.
  3. Redirect to /login

I am using 1 because if I tried (3), I code would not run, as I believes it expect me to return an Option with Redirect::to(uri!(login)), but the function return Option<NamedFile>.

So, my questions are, in the three above ways to solve this problem:

  • What is the most elegant solution in general, or at least the most "Rustic" way?
  • Suppose I want to do (3), what should I do with the return type of function index()?
    Thank you.

I'm not familiar with actix-web, but generally I would expect there to be some way to return an enum with a separate variant for each kind of return condition you want to return. Consider asking on the actix-web discord server for more info.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.