Can I redirect to the previous page someone if not logged in? Also, is it possible to create a hierarchy system for users?
I tried this:
if(!(Auth::check())) {
header("Location: {{ route('cooperado.index') }});
}
But i not even got an error message, just doesn't work. I'm starting at laravel so it's kind of hard to fully understand how it works.
Using Constructor in controller
You also can use middleware in order to redirect unauthenticated user back or somewhere else.
public method __construct(){
$this->middleware('auth');
}
Add this code in your controller so all methods within particular controller
-direct from route defination
Route::get('/path/',controller#method)->name('cooperado.index')->middleware('auth');
Redirection
using this method unauthenticated user will redirect to login page.
in order to edit redirection page you can change '#redirectTo' method
in
app/Http/Middleware/Authenticate.php
file.
if(!(Auth::check())) {
return redirect()->route('cooperado.index');
}
You mention in your question also wants to return previous page then use following but i'm not recommended this because if your first login effort fails then login failed' page becomes your previous page and second login effort succeeds then you are redirected to login page again because it's your previous page.
return Redirect::to(URL::previous());
You can use this. Is the easiest way to do this.
And you can also pass message, will displayed on when user redirect back from main page.
if(!Auth::check) {
return Redirect::back()->with('error','Please Login');
}
Related
I am building my first Laravel app with the Metronic 8 Laravel theme. It uses Breeze for authentication. I changed a couple of things around - created a welcome page for non-logged-in users, and moved the main template that was the index to an auth protected "/dashboard". The problem is that it still tries to load the dashboard Blade template, regardless of authentication, resulting in an error.
Route
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
Here's Authenticate, where it should redirect non-authenticated users to the login page.
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
When I'm not logged in and navigate to the dashboard URL, it attempts to load the dashboard Blade template, which calls a menu function that checks the user permissions for menu items. Unfortunately, since there is no user, the application blows up from passing a null value to a method expecting a user array/object.
Any ideas on where to look for the problem? It seems to me that the auth middleware should redirect to the login page before trying to load the Blade template when not logged in.
I would put the middleware at the beginning of the route like this, though I'm sure it's not causing the problem-
Route::middleware(['auth'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Aside from that, please provide some information on the error itself like what the error is about/what is says..etc...
First of all, make sure you have a login named route defined in your routes/web.php file. It should look something like:
Route::get('/login', '<controller>#<method>')->name('login');
The important bit is ->name('login') so that the Authenticate middleware can correctly identify the route to redirect to. Change <controller>#<method> appropriately to route to the login method of your app.
Wakil's answer is irrelevant and actually opposite of the documentation. Your syntax is correct.
I figured out the issue. Keen Themes put a call to a method to build an array of menu items in the web routes file. That was making the call to the offending code. After I wrapped that in an auth check the error was fixed, and everything works as expected.
I'm using a controlled route as such
Route::controller('company', 'CompanyController');
In this Controller i have a getLogin and postLogin function, the getLogin shows the login view. To get there i need to go to company/login
Now i'm wondering how can i redirect company/ to company/login
I have the following working but is it good practice?
Route::get('company', 'CompanyController#getLogin');
Route::controller('company', 'CompanyController');
Thank you!
In this case, index methods will respond to the root URI, so what you can do is create a getIndex() function which will return Redirect::to('company/login'). You can probably do a check on this for a logged in user first as well, for example...
public function getIndex()
{
if(!Auth::check())
return Redirect::to('company/login');
// Continue with what should happen if the user is logged in.
}
This way, when someone goes to /company, it will redirect them to login if they aren't logged in already or it will continue doing whatever you want it to do or redirect them to the same page you are redirecting people to after they login.
This also means you can do away with that Route::get() that you have setup for company.
my question is trivial byt i am new and do not even know what to look for
i am using kohana framework to build my site. I have already learned how to use auth module (more or less) and created login, logout "arhitecture".
When my user is not logged i redirect to login page and when it is I use a view to show user data.
Now i am trying to do something like a page menu when i can see "login" button when user is not logged in, but "logout" button when logged. Do i make myself clear? I assume i cant redirect anywhere as this is a part of the same view. then how do I deterin a view content accordingly to a user state?
I am not looking for a ready code (although that would me apreciated) but a direction on what to look for and what to read about.
For Kohana 3.2 you could check if a user is logged in by calling
Auth::instance()->logged_in();
An option of integration might be to build a base controller where the user is redirected or set in the before action. That way you always have the redirect in 1 place and the user set if there is a valid login.
class Controller_Custom extends Controller {
protected $user;
public function before()
{
if ( ! Auth::instance()->logged_in()) {
$this->request->redirect('url/login/page');
}
$this->user = Auth::instance()->get_user();
}
}
For Kohana 3.3 the redirect request is changed a bit I believe.
I am using Laravel 4 for a web project , I am creating an admin panel
in the admin I have admin/profile/{id} to display user profile like firstname lastname etc..
in my AdminController I have :
// get the Admin Profile page
public function getProfile($id) {
// get the user from the database
$user = User::find($id);
$this->layout->content = View::make('admin.profile', array('user' => $user));
}
but what happens I keep getting errors if I just go to admin/profile without any user id? , how do i make it work?
Basically how to make if the page doesn't exist to go to the dashboard or something like that ? for example if they tried admin/test and test is not a method there , if they are login it will go ot teh dashboard , if not it will go to login page?
You're asking two questions:
I get a 404 error if I go to admin/profile without a user ID. How can I redirect to the login page?
How can I redirect to a login page if the user is not logged in.
For the first question, you can do it a couple of ways. One solution is to add a route that matches anything after all your other route definitions:
Route::get('{any_url}', function(){ return Redirect::route("login"); });
This must be the last route defined because it will match any URL.
Another way to do it would be to catch the NotFoundHttpException in your start/global.php file. Add this code:
App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
{
return Redirect::route("login");
});
Both of these examples redirect to a named route called login.
As for your second question, the correct way to handle this is to use the auth filter. In your filters.php file, you can add something like this:
Route::filter('auth', function($route)
{
// is the user authorized? if not, redirect to the login page
if (!user_is_authorized())
{
// redirect to the login page
return Redirect::route('login');
}
});
Where the user_is_authorized function is just shorthand for whatever check you do in your code. For information on using the auth filter, see http://laravel.com/docs/routing#route-filters.
You can simply add a missing handler (handles 404) like this:
App::missing(function($e){
// Log the missing url
Log::error($e);
// You may redirect to home
return Redirect::to('/');
// Or redirect to a 404 route that is declared
// to show a custom 404 page from that route
return Redirect::to('missing');
});
Put the code (given above) in your app/start/global.php file. For the missing url/route you need to crate add a route in your routes.php file like this:
Route::get('missing', function(){
// show the view: errors.missing
$this->layout->content = View::make('errors.missing');
});
Create a view as views/errors/missing.blade.php and in your missing.blade.php view, show a fancy message to inform the visitor that, the requested page/url is not available and add a link to your home page in that 404 page.
Read more on Laravel website about Errors & Logging, check Handling 404 Errors.
I'm not looking for the whole ACO-ARO implementation... I just want to use Auth, and check against the user's role....
What do I put where in order to simply deny users from a given controller unless they have a certain role.
I'm trying to use the $this->Auth->authorize = 'controller';
... but I don't even know where to put that??
Any help would be awesome!
Thanks in advance.
Short answer: Sounds like you need to create and app_controller.php and put your code in the beforeFilter method.`
Longer Answer: Create an app_controller.php file in you app directory and put the following code in beforeFilter().
if (isset($this->params[Configure::read('Routing.admin')])) { //User is trying to access a page using the admin route
if ($this->Session->check('someSessionVariable')) { //Check user has some session variable set.
// User is accessing an admin page and has permission, do something, or in most cases do nothing.
} else { //No sessions set for user, redirect to login page.
$this->redirect('/yourLoginPage'); //Redirect
}
}
This is no substitution for proper user of the Auth component, but should do what you need. Make sure you check its secure before you put it into production.