Hi I have loaded the auth component and included the following;
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->allow('register', 'verify');
}
If I navigate to register it works correctly, however, if I go to verify it redirects me to login?
Any ideas?
Related
I am using Blazor Authentication and I scaffolded the Logon Page. I wish to redirect on the Layout page to it when the user is not authenticated, but onfortunately it does not work.
Here is my code:
[Inject] private NavigationManager nav { get; set; }
protected override async Task OnInitializedAsync()
{
var authState = await auth.GetAuthenticationStateAsync();
var user = authState.User;
if (!user.Identity.IsAuthenticated)
{
nav.NavigateTo("/Identity/Account/Login");
}
}
What is actually happening is that Blazor think the page does not exist and renders the NotFound: <p>Sorry, there's nothing at this address.</p>.
That said; without doing anything else, when I refresh the page it actually opens the login page. This does not make sense to me.
Can anyone kindly help me to understand what is happening here.
Regards,
Chris
You should use the authorize page attribute. If you add it to the page, the blazor framework will redirect to the login page.
Is it a Blazor WASM app?
I have a login page and I need to go to my home page after successful login. That login functionality login to a ldap server and send a response whether the authentication is success or not. I don't want that to keep in localstorage since this app has only two pages. login and home. When login success it should redirect to home page, if not it should redirect to again to the login page.
And please the console.log in the browser, "inside auth guard true" prints thousands times..
The only code I have in my app.component.html is <router-outlet></router-outlet>
In the canActivate(), all you need to do is return true or false. When you are redirecting it to home from inside the function you are entering into an infinite loop.
This is because on redirecting to home the canActivate() gets called and its expects a boolean return value. If the value is false, it won't load the component.
canActivate() {
if (this.authService.loggedIn) {
console.log('Inside Auth Gaurd');
return true;
}
console.log('auth gaurd false path');
return false;
}
For more detail on canActivate() refer this
I'm wondering if it is possible to make the authentication redirect differently for each of my controllers? Currently, everything redirects to /home. This is intended for my HomeController. But for ClientController, I want it to redirect to /client (if authenticated) and not /home. Do I have to make a new middleware for each of my controllers or is there a way to accomplish this by reusing auth?
RedirectIfAuthenticated.php
if (Auth::guard($guard)->check()) {
return redirect('/home'); //anyway to change this to /client if coming from ClientController?
}
I have this on my ClientController.php
public function __construct()
{
$this->middleware('auth');
}
Thanks in advance! Fairly new to Laravel and Middleware.
Just use this in User model:
protected $redirectTo = '/client';
You can also achieve this by changing Laravel's core file. If you are using Laravel 5.2 go to project_folder\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RedirectsUsers.php
You can find the following code:
public function redirectPath()
{
if (property_exists($this, 'redirectPath')) {
return $this->redirectPath;
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; //Change the route in this line
}
Now, change /home to /client. However, I recommend not to change core files. You can use the first one.
Never mind, I was able to make things work with proper routing.
Added ClientController under web middle which is responsible for all of the authentication.
Route::group(['middleware' => ['web']], function () {
Route::resource('client', 'ClientController');
}
And in
ClientController.php, add to use auth middleware.
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('client');
}
I have a logout function like this
public function logout() {
Auth::logout(); // logout user
return Redirect::to('login'); //redirect back to login
}
When logout function is triggered through routes which looks like
Route::get('logout', array(
'uses' => 'userController#logout'
));
user get redirected to the login page. But when goes back using browser, dashboard view gets opened which i don't want to. What can be the best way to prevent users from going back to dashboard once they logged out? Though there are some discussion on this topic, but didn't helped me.
Since you're using the Auth, you can utilise the existing Middleware to stop the back button putting them on dashboard.
Wrap the routes you want to protect with a Route::group
Route::group(['middleware' => 'auth'], function () {
Route::get('dashboard', function () {
// Uses Auth Middleware
});
});
Any attempt to access dashboard without login will put them back on the home page (default location in the middleware). Modify the middleware (found in app/Http/Middleware/Authenticate.php) to change the redirect url.
you need to add a middleware in your dashboard so that even if they press back the button they can go back to the page but can't do anything unless they login again.
I've followed this tutorial for authentication and it works:
http://www.playframework.com/documentation/2.2.x/JavaGuide4
But I was wondering if there was another way to do it so that the user can't just go to myapp.com/login and get the login screen even if they're already logged in. Is there a way to do this so I can go to the login screen but still have myapp.com as the route?
This is easy. Just have the controller method for route "/" display the login page instead of your landing page when the user is not logged in.
Say / routes to Application.index() and it has a function isLoggedIn() to check login status, you could do something like this:
public static index() {
if (isLoggedIn()) {
return ok (index.render());
}
else {
return ok (login.render());
}
}