Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How have 2 different cookies/sessions for both '/' and '/admin'? #475

Open
mamcx opened this issue Oct 24, 2024 · 1 comment
Open

How have 2 different cookies/sessions for both '/' and '/admin'? #475

mamcx opened this issue Oct 24, 2024 · 1 comment

Comments

@mamcx
Copy link

mamcx commented Oct 24, 2024

How can I have different cookies for different parts of a site?

The root:/ is for a public-facing site that log regular users, and '/admin' is only for employees.

I tried:

       App::new()
..
            .wrap(
                IdentityMiddleware::builder()
                    .id_key("bestseller.user_id")
                    .last_visit_unix_timestamp_key("bestseller.last_visit")
                    .login_unix_timestamp_key("bestseller.login")
                    .build(),
            )
            .wrap(
                SessionMiddleware::builder(CookieSessionStore::default(), Key::from(&[123; 64]))
                    .cookie_name("bestseller_session".into())
                    .build(),
            )
            .wrap(TracingLogger::default()) //always last
            .service(fs::Files::new("/static", static_path()))
            .configure(api)
            .configure(back_end)
            .configure(back_end2)
            //Public
            .wrap(
                IdentityMiddleware::builder()
                    .id_key("bestseller_store.user_id")
                    .last_visit_unix_timestamp_key("bestseller_store.last_visit")
                    .login_unix_timestamp_key("bestseller_store.login")
                    .build(),
            )
            .wrap(
                SessionMiddleware::builder(CookieSessionStore::default(), Key::from(&[123; 64]))
                    .cookie_name("bestseller_store".into())
                    .build(),
            )
            .configure(front_end)
    });

But I only see the first cookie stored.

@Necoo33
Copy link

Necoo33 commented Nov 9, 2024

You can add the seperate router with web::Scope for do authorization with cookies, here an example:

use actix_web::{web, Scope};

extern crate dummyctrl;

// that code will create a seperate router in your server acts like seperate website, it also has seperate cookies for it's base path:

pub fn dummy_router() -> Scope {
// take the controllers from whenever you want
    web::scope("/dummy")
        .route("", web::get().to(dummyctrl::dummy_controller)) // this is for "/dummy" route
        .route("/", web::get().to(dummyctrl::dummy_controller)) // and this for "/dummy/" router
        .route("/dummy2", web::get().to(dummyctrl::dummy_controller_2)) 

// and so on
}

add that router to your server with .service() method. Then you can check the cookie value and do whatever you want by depending on that value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants