Redirect after login

Closed
Vanguard - Advanced PHP Login and User Management Vanguard - Advanced PHP Login and User Management October 11, 2018
Login to reply
Milos Stojanovic Support Agent
3 days ago

No problem! I'm glad that it is working now. :)

Dustin van Zyl
3 days ago

First method worked perfectly, thanks again!

Milos Stojanovic Support Agent
3 days ago

The dashboard is a default place where all users are being redirected. When you redirect a user from http://smsapp.dvz.digital/ to Vanguard's login page, you provide the redirect page via $to parameter. However, if a user clicks the "Sign Up" link it will take him to a Sign Up page and it will loose the "to" URL parameter.

There are two ways to handle this:

1) If you want, you can customize the default redirect page inside the handleUserWasAuthenticated method in "app/Http/Controllers/Web/Auth/AuthController.php" file, and replace

return redirect()->intended();

with 

return redirect()->to('your custom URL here');

2) You can modify how Vanguard redirects work and instead of looking for the "to" parameter during the redirect in "handleUserWasAuthenticated" method, you can store that parameter into the session when login page is rendered ("getLogin" method) and then pull it from the session in "handleUserWasAuthenticated" method.

Regards,
Milos

Dustin van Zyl
3 days ago

I see that after registering and then logging in for the first time the user is redirected to the Vanguard dashboard?

Dustin van Zyl
3 days ago

Awesome,


Thank you so much Milos!

Milos Stojanovic Support Agent
3 days ago

Ok, so I've updated the index.php file from /public_html/smsapp  folder to look exactly as I told you in my comments below and it is redirecting to the login page as expected... I guess that you were editing some wrong file (maybe index.html) or something like that.

Regards,
Milos

Dustin van Zyl
3 days ago

Sorry, user is now: milos@dvz.digital

password is the same

Milos Stojanovic Support Agent
3 days ago

Now I get "Login incorrect" message. Have you changed the login credentials maybe?

Dustin van Zyl
3 days ago

Please try again, you now have access, the app is in public_html/smsapp

Milos Stojanovic Support Agent
3 days ago

No problem! Thanks for creating an account for me.

Can you please make sure that I'm able to access the directory where your website is located? Currently, when I log in via FTP, I see an empty folder and cannot navigate up to change the directory...

Dustin van Zyl commented privately
Milos Stojanovic Support Agent
3 days ago

The code is looking OK for what you want to achieve but, from what I can see it is still redirecting to the "/login" instead of "/vanguard/public/login" for some reason. Have you updated the code on the server?

If you have updated it, can you please create a temporary SSH/FTP account for me so I can see what's going on? You can post the credentials here as a private reply.

Dustin van Zyl
3 days ago

updated as per your previous comment:


<?php

require_once __DIR__ . '/vanguard/extra/auth.php';


// Here we just check if user is not 

// logged in, and in that case we redirect

// the user to vanguard login page.

$to = "http://smsapp.dvz.digital/welcome.html";


if (! Auth::check()) {

    redirectTo('vanguard/public/login?to=' . $to)

}


?>


Essentially I want user to be redirected to the login page when accessing http://smsapp.dvz.digital

and then redirected to http://smsapp.dvz.digital/welcome.html  once they are logged in.

Milos Stojanovic Support Agent
3 days ago

By doing what exactly? Have you checked my previous comment?

Dustin van Zyl
3 days ago

By doing that the page redirects to http://smsapp.dvz.digital/login?to=http://smsapp.dvz.digital/welcome.html which doesn't exist without checking auth? ?



Milos Stojanovic Support Agent
3 days ago

I guess that you have updated something since I'm now being redirected to http://smsapp.dvz.digital/login?to=http://smsapp.dvz.digital/welcome.html when I try to access the http://smsapp.dvz.digital

So, since now "?to=..." part of the redirect URL exists, the only thing you need to update is the actual path to the login page. Since you placed Vanguard inside a "vanguard" folder and your login URL is at http://smsapp.dvz.digital/vanguard/public/login then your code for redirect should look like the following:

//...

if (! Auth::check()) {
    redirectTo('vanguard/public/login?to=' . $to);
}
Dustin van Zyl
3 days ago

Yes,


see source attached

Milos Stojanovic Support Agent
3 days ago

Thanks! 

Ok so your index.php page is protected, correct? Since I get redirected to the login page once I try to access the http://smsapp.dvz.digital

One thing that I don't see in the URL when I'm being redirected to the login page is the "?to=..." part. Are you sure that you have added the code you provided below with "?to=..." part as well?

- Milos

Dustin van Zyl
3 days ago

http://smsapp.dvz.digital

Milos Stojanovic Support Agent
3 days ago

Hey,

Hmm in your case you should always be redirected to google after a user is being successfully authenticated. Can you please send me the URL to your app so I can check and see how it works?

- Milos

Dustin van Zyl
3 days ago

Thanks Milos,

On login in user is still directed to the Vanguard Dashboard?

My page looks like this:

<?php

require_once __DIR__ . '/vanguard/extra/auth.php';


// Here we just check if user is not 

// logged in, and in that case we redirect

// the user to vanguard login page.

$to = "http://www.google.com";


if (! Auth::check()) {

    redirectTo('login?to=' . $to);

}


?>


Milos Stojanovic Support Agent
4 days ago

Hey Dustin,

As I said, you should put that code at the top of the file you are protecting.

The URL where Vanguard will redirect you after successful login is the one you provided in a $to variable. Inside Using Vanguard for Existing Website you can see the example of how you should define the $to variable if you want to redirect to a previous page after successful authentication.

- Milos


Dustin van Zyl
4 days ago

Hi Milos,

Thanks for the prompt reply,

I have already protected relevant pages, however, I want to redirect AFTER login to a URL

Please advise where this code should go: $to = "http://www.myurl.com";


Thank you.




if (! Auth::check()) {


    redirectTo('login?to=' . $to);


Milos Stojanovic Support Agent
4 days ago

Hey Dustin,

As it says inside the documentation article, it should be at the very top of the file/page that you want to protect. If you have a file called page.php and you want to allow only authenticated users to access that file, then you should place the code at the top of that page.php file.

Of course, you need to include the /extra/auth.php file before it so it can actually work. Just make sure that you go through the "Protecting The Website" section in Using Vanguard for Existing Website article to understand what exactly should be done.

Regards,
Milos

Dustin van Zyl
4 days ago

Hi,


I am trying to redirect after login,


please confirm where this code should be placed:


//This can be url to any page, it doesn't matter

//if it is on your existing website or not

$to = "http://www.google.com";


if (! Auth::check()) {

    redirectTo('login?to=' . $to);


Thank you.