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 can I setup this middleware? #206

Closed
CodeUmair opened this issue May 18, 2023 · 1 comment
Closed

How can I setup this middleware? #206

CodeUmair opened this issue May 18, 2023 · 1 comment

Comments

@CodeUmair
Copy link

Hello all, I am trying to achieve this, but this is something not possible correct me if I am doing something wrong here.
I have routes like this.
1: domain.com/admin
2: domain.com/admin/login
3: domain.com/admin/.* < all other pages of admin panel.

This is what I am doing right now.
//Check if admin is already logged in, redirect to admin dashboard/admin panel home page.
$router->before('GET|POST', '/admin/login', function () {
if (Session::is_Admin() === true) {
header('location: /admin');
exit();
}
});
//Check if admin is NOT logged in, redirect to admin Login Page which is "domain.com/admin/login".
$router->before('GET|POST', '/admin', function () {
if (Session::is_Admin() === false) {
header('location: /admin/login');
exit();
}
});

Till here it's working fine, But all other pages under the admin panel are not working, this is what I am using.
$router->before('GET|POST', '/admin/.*', function () {
if (Session::is_Admin() === false) {
header('location: /admin/login');
exit();
}
});

I don't want to create middleware for every single admin panel page.
Is there any way I can achieve this?
Thank you all.

@CodeUmair
Copy link
Author

Thank you all, Looks Like I found the solution.

$router->before('GET|POST', '/admin/(?!login)(.*)', function () { if (Session::is_Admin() === false) { header('location: /admin/login'); exit(); } });

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

1 participant