I made the files for authentication using the command
php artisan make:auth
I've read on the internet that register, login, as well as logout should work properly, but localhost:8080/logout doesn't work, and I don't know why.
I also read something about modifying AuthController in app, but I do not have that file.
I tried to do it by hand, which means I created a middleware LogoutRedirect:
public function handle($request, Closure $next)
{
return redirect(pages.logout);
}
In the routes I added
use App\Http\Middleware\LogoutRedirect;
Route::get('logout', function()
{
return view('pages.logout');
})->middleware(LogoutRedirect::class);
And logout.blade.php looks like
{{ Auth::logout() }}
I get the error (when trying to access localhost:8080/logout)
Use of undefined constant pages - assumed 'pages'
What could I do about it?
EDIT
I tried another approach (but with no better results):
renamed the route which redirects to '/' to 'home'
made a LogoutController in app/http/Controllers/Auth
namespace App\Http\Controllers;
use [...]
class LogoutController extends Controller
{
public function logout() {
Auth::logout();
return Redirect::route('home');
}
}
made the route
Route::post('logout', array(
'as' => 'account-sign-out',
'uses' => 'Auth\LogoutController#logout'
));
The error I get is
MethodNotAllowedHttpException in RouteCollection.php line 233:
That's the same error I get when I try to use the default logout defined in auth
You are trying to access the logout page with GET. But this doesn't work because your logout route is a post route.
Change
Route::post('logout', array(
'as' => 'account-sign-out',
'uses' => 'Auth\LogoutController#logout'
));
by
Route::get('logout', [
'as' => 'account-sign-out',
'uses' => 'Auth\LogoutController#logout'
]);
When you go to the /logout route with the method GET(The default when you go to a page) it should work.
Related
Now this route's without ( except ) it just find working but when I want to reach any route like index or store I must to login then get the data form them . So I want to make index route out of this middleware ( it's ok reach to index without login ) hope to got it :)
route's :
Route::group(['middleware' => 'auth:api'], function() {
Route::resource('cards', 'cardsController', ['except' => 'index']);
Route::resource('services', 'servicesController', ['except' => 'index']);
Route::get('getUserinfo', 'LoginController#getUser');
});
and this my route list for cards :
and this when I get all cards form postman :
You have ['except' => 'index'] in the route group. Try to take it out and check if it is working.
Edited:
if you want index showing without login, try writting another route outside of the route group
so something like this
Route::get('cards', 'cardsController#index');
Route::get('services', 'servicesController#index');
Route::group(['middleware' => 'auth:api'], function() {
Route::resource('cards', 'cardsController', ['except' => 'index']);
Route::resource('services', 'servicesController', ['except' => 'index']);
Route::get('getUserinfo', 'LoginController#getUser');
});
First you should name your Controllers properly, instead of
cardsController
name it to
CardsController
Your error occurs because you are excepting the index action in your Controller.
Instead of this:
Route::resource('cards', 'cardsController', ['except' => 'index']);
Route::resource('services', 'servicesController', ['except' => 'index']);
Do this:
Route::resource('cards', 'cardsController');
Route::resource('services', 'servicesController');
Check out the docs
Dont forget to name your Controller properly.
As you should define your route outside the middleware.... As u want to use it without login....
Route::get('cards', 'cardsController#index');
Route::get('services', 'servicesController#index');
In middleware u have to define the route as below
Route::resource('cards', 'cardsController')->except('index');
Route::resource('services', 'servicesController')->except('index');
I am trying to code a login function for my api that takes a username and password then give you a password grant token to make api requests. The login route when called gives you
{
"message": "Unauthenticated."
}
I am using passport on laravel to do secure the api. Why am I getting a 401 when the route does not have the auth:api middleware? I tried using a clousure to see if I get could get a response and the closure did not give me an error.
Route::group(['prefix' => '/v1', 'middleware' => ['auth:api'], 'namespace' => 'Api\V1', 'as' => 'api.'], function () {
Route::post('/post/like','PostLikeController#store');
});
Route::group(['prefix' => '/v1', 'namespace' => 'Api\V1', 'as' => 'api.'], function () {
Route::post('login', 'Auth\LoginController#login');
});
Does your login controller have a constructor? sometimes middleware is set in there?
Otherwise I've also had issues with having the middleware routes above the public ones.
Try putting the public routes in the file first and also checking the LoginController.php for a constructor which might be setting a middleware
It possibly due to the same prefixes, as it does not overriding but instead stacking on top of each other.
I suggest for your login route, possibly, you can use this
Route::post('login', 'Auth\LoginController#login')->withoutMiddleware([FooMiddleware::class]);
If it's still does not help try putting your login route above the middlewared route.
I had Laravel 4.2 application and updating it to Laravel 5.4. for this i have installed fresh Laravel 5.4 and migrated routes,controllers views etc.
I want to protect all pages after /warehouse e.g /warehouse/dashboard,/warehouse/accounts and so on except /warehouse/login page. I have searched and used this route but its not working properly.
Can any one let me know whats the proper way of authentication.
Route::group(['middleware' => ['auth']], function() {
// uses 'auth' middleware
Route::resource('/warehouse','WarehouseController#index');
});
My login and verify routes are
Route::get('/warehouse/login', array('as' => 'WarehouseAdminLogin', 'uses' => 'WarehouseController#login'));
Route::post('/warehouse/verify', array('as' => 'WarehouseAdminVerify', 'uses' => 'WarehouseController#verify'));
For Route:resource there is no need to add function name after controller.
So try this:
Route::resource('/warehouse','WarehouseController');
And for Auth middlware you can do this :
Route::middleware(['auth']->group(function() {
// Auth routes
});
And it's obvious that login route should no be inside auth middleware!
How can a new guest user see login page?
Use Auth routes outside the auth middleware :
Route::get('login', 'Auth\LoginController#showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController#login');
Route::get('logout', 'Auth\LoginController#logout')->name('logout');
I have a working password reset process with the following routes:
Route::group(['middleware' => [], 'namespace' => 'Auth'], function () {
Route::get('/password/reset/{token?}', ['as' => 'site.password.showResetForm', 'uses' => 'PasswordController#showResetForm']);
Route::post('/password/email', ['as' => 'site.password.sendResetLinkEmail', 'uses' => 'PasswordController#postEmail']);
Route::post('/password/reset', ['as' => 'site.password.reset', 'uses' => 'PasswordController#reset']);
});
My problem arises if someone is currently already logged in on the machine. In that case when A user clicks on the link in the email, PasswordController#showResetForm is never executed and the their home page opens in a new tab. Is there a way to force the current user to be logged out so that the password reset can proceed?
Call Auth::logout(); in one of your controllers.
If the showResetForm is never displayed due to user being logged in, you will need to create a temporary page where you call the above function and then redirect to Password Reset page:
public function do_password_reset()
{
Auth::logout();
return redirect()->route('PasswordController#showResetForm');
}
(Remember to add the relevant route for this function.)
Rather than creating additional routes I overwrote the showResetForm function used to route the user to the auth.password.reset view, found in
vendor/laravel/framework/src/Illuminate/Foundation/Auth/ResetsPasswords.php
To do so, within your ResetPasswordController add the following:
public function showResetForm(Request $request, $token = null)
{
$this->guard()->logout();
$request->session()->flush();
$request->session()->regenerate();
return view('auth.passwords.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
All being well that should be the only code you need to add.
As for editing the ResetPasswordController suggested by Lawrence, you might want to remove guest middleware from it's constructor:
$this->middleware('guest');
This middleware redirects authenticated uses to the root page, so it would prevent an already logged in user from logging out.
I am writing a Laravel 5.2 application. I need to manually login the user for which I am using \Auth::login($user). I am doing it in following way.
if ($user = User::where('phone',session('phone'))->first())
{
\Auth::login($user);
// \Auth::loginUsingId($user->id);
// Auth::attempt(['email' => $user->email, 'password' => 'password']);
$data = \Auth::user(); //returning correct results
}
I have tried all the options namely Auth::login($user), Authh:loginUsingId($user->id) and attempt method. These methods are working fine as the $data variable is storing the object of correct user. But the problem is when I move to other route say '/home' the user remain no more authenticated.
What might be the wrong here? How could I do it correctly?
Since Laravel 5.2, you have to attach all your routes that need session with the 'web' middleware. See your app/Http/Kernel.php, the 'web' middleware contains the \Illuminate\Session\Middleware\StartSession.
In routes you have to use web in laravel 5.2
Route::group(['middleware' => ['web', 'auth']], function () {
Route::get('/', 'HomeController#index');
Route::get('/profile', 'HomeController#profile');
});