2

Currently, Laravel redirects the user to a different URL if he isn't logged in (eg /auth/login). /auth/login contains logic to check if a user has supplied login credentials, in which case they are redirected to another page (eg /home).

(Is this correct? I'm still learning.)

I'd like to change this functionality. Ideally, when a user visits / or /index.php, Laravel would detect that he's not logged in and would return a different view (the login form) and not redirect. The user would see /#/users in the navigation bar, not /auth/login. The login form would submit to the same page.

Now, when Laravel detects that the user isn't logged in but Auth::check() works, it would log the user in and continue with the original view.

The reason I'd like to do this is so that Angular view navigation still works. Because of the redirect, the #param (/#/users) is lost and we can only redirect the user to the dashboard.

Is this possible is Laravel? I'd be satisfied with learning where all the current functionality is hidden so I can start there.

whiterook6
  • 3,270
  • 3
  • 34
  • 77
  • Not a laravel guy, nor do i know it.. But ideally what you would want to do is check to see if the user is logged in on the individual controller / action and if not render the login form view. http://laravel.com/docs/4.2/responses – Ray Jul 31 '15 at 17:06
  • 1
    @Ray You should check in a middleware, and you decide if the middleware is global or which routes / controller actions it will affect. – user2094178 Jul 31 '15 at 17:26
  • Right. Just giving the user a starting bases. I know nothing about laravel/symfony, or its servicing/dependency injection model. – Ray Jul 31 '15 at 17:56
  • This is pretty simple in Laravel and there is already a discussion going on (actually it's on the list of the most relevant questions for laravel). You can find it (and also a solution) [here](http://stackoverflow.com/questions/15389833/laravel-redirect-back-to-original-destination-after-login). In words it's basically this: Laravel can save the requested URL while serving a login form and redirect to the requested URL after login; even if the login URL (e.g. `/auth/login`) is shown in the meantime. – Namoshek Jul 31 '15 at 20:09
  • Thanks @Namoshek. I'm under the impression that PHP (and thus Laravel) doesn't have access to the Hash part of the url, so if I tried to access /#/users, it would redirect to /. Is that not the case? – whiterook6 Jul 31 '15 at 20:51
  • Ok my fault, I did not read the Angular part carefully enough. But this should not make things more complicated, as you can let Angular decide, which view you want to serve. Ofc you need some serverside security too, so that you can fetch some pages only if being authorized, but the login page should be shown by Angular I suppose. You could also use the Angular routing HTML 5 mode, which removes the hash and makes things easier (with some more thinking involved though). – Namoshek Aug 01 '15 at 06:38

1 Answers1

0

Laravel will redirect the user to auth/login, then after authentication, redirect it back to it's original view.

One way to prevent this from happening, is to use Laravel as an API resource, and have your Angular app control the user interface and actions.

For example, when a user land in '/', your Angular app will gather all the necessary data from your Laravel API via ajax calls. The first ajax call would be to check if user is authenticated, and if not, use Angular to display a modal with a login form. Then use the form to authenticate the user via Laravel API.

This way your url and current view won't change, and you have full control with your Angular app.

Just an idea...hope it helps.

HotRod
  • 126
  • 4