How do you give one route unauthorized access in actix-web?

I am making a project that uses basic authentication, and to that I found this stack overflow post. I used the first answer's example to set up my app, but I now I want to be able to register users to the service without going through the basic authentication. I looked up extractors and found you could try to implement basic authentication on individual routes, but I am not sure how to do that. Any help would be appreciated. Thanks

1 Like

This is a defacto example for the use of middlewares. You would write a middleware that performs the authentication check, and use it to compose routes.

1 Like

How do you make a middleservice with actix? I am a bit new to the library. Also how you make it so only the one route would be effected

You can find the middleware documentation here: Middleware | Actix. I've written a little authentication middleware for JWT validation you can find here, if you want to have a look at that (it's a bit outdated but uses the type from the actix-web-httpauth crate that you probably want to use yourself).

You can apply middleware to only a subset of all your services using a Scope. I.e.:

use actix_web::{web, App};

let auth_scope = web::scope("/")
    .wrap(/* authentication middleware */)
    .service(/* the service you need authentication for */);

let app = App::new()
    .service(auth_scope)
    .service(/* the service you don't need authentication for */);
3 Likes

Thank you so much for the help

1 Like

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.