Skip to content

Commit 9a2b5ec

Browse files
committed
Add macro login_url configuration
Add login_url configuration test
1 parent eb5d301 commit 9a2b5ec

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

axum-login/src/middleware.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ macro_rules! predicate_required {
166166
};
167167

168168
from_fn(
169-
|auth_session: $crate::AuthSession<_>,
169+
move |auth_session: $crate::AuthSession<_>,
170170
OriginalUri(original_uri): OriginalUri,
171171
req,
172172
next: Next| async move {
@@ -382,6 +382,49 @@ mod tests {
382382
assert_eq!(res.status(), StatusCode::OK);
383383
}
384384

385+
#[tokio::test]
386+
async fn test_login_required_with_configured_login_url() {
387+
// let tes_login_url: &'static str = Box::leak("/login".to_string().into_boxed_str());
388+
let test_login_url: &'static str = "/login";
389+
let app = Router::new()
390+
.route("/", axum::routing::get(|| async {}))
391+
.route_layer(login_required!(Backend, login_url = test_login_url))
392+
.route(
393+
"/login",
394+
axum::routing::get(|mut auth_session: AuthSession<Backend>| async move {
395+
auth_session.login(&User).await.unwrap();
396+
}),
397+
)
398+
.layer(auth_layer!());
399+
400+
let req = Request::builder().uri("/").body(Body::empty()).unwrap();
401+
let res = app.clone().oneshot(req).await.unwrap();
402+
403+
assert_eq!(res.status(), StatusCode::TEMPORARY_REDIRECT);
404+
assert_eq!(
405+
res.headers()
406+
.get(header::LOCATION)
407+
.and_then(|h| h.to_str().ok()),
408+
Some("/login?next=%2F")
409+
);
410+
411+
let req = Request::builder()
412+
.uri("/login")
413+
.body(Body::empty())
414+
.unwrap();
415+
let res = app.clone().oneshot(req).await.unwrap();
416+
let session_cookie =
417+
get_session_cookie(&res).expect("Response should have a valid session cookie");
418+
419+
let req = Request::builder()
420+
.uri("/")
421+
.header(header::COOKIE, session_cookie)
422+
.body(Body::empty())
423+
.unwrap();
424+
let res = app.oneshot(req).await.unwrap();
425+
assert_eq!(res.status(), StatusCode::OK);
426+
}
427+
385428
#[tokio::test]
386429
async fn test_login_required_with_login_url_and_redirect_field() {
387430
let app = Router::new()

0 commit comments

Comments
 (0)