Admin is not getting logout in Multi Authentication - php

I am trying to make Multi Authentication for Admin and User.
User is getting logged in and logged out without any problem.
But Admin is logged in but not getting logged out. Do I require double
logout function or I am making up mistake.
AdminLoginController.php
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class AdminLoginController extends Controller
{
public function __construct()
{
$this->middleware('guest:admin', ['except' => 'logout']);
}
public function showLoginForm(){
return view('auth.admin-login');
}
public function login(Request $request)
{
//Validate the form data
$this->validate($request,[
'email' => 'required|email',
'password' => 'required|min:6'
]);
//Attempt to log th user in
if (Auth::guard('admin')->attempt(['email' => $request->email,'password' => $request->password],$request->remember)){
//if successful, then redirects to their intended location
return redirect()->intended(route('admin.dashboard'));
}
// if unsuccessful, then redirect back to thee login with the form data
return redirect()->back()->withInput($request->only('email','remember'));
}
}
Login Controller.php
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function logout(Request $request) {
Auth::logout();
return redirect('/');
}
}

Related

Undefined array key "email"

I can't seems to find the reason why it gives me the error Undefined array key "email"
I'm sorry in advance if the info that I'm providing is not enough please tell me what info you guys need.
here is my admin controller
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AdminController extends Controller
{
public function dashboard()
{
return view('admin.dashboard');
}
public function login(Request $request)
{
if ($request->isMethod('post')) {
$data = $request->all();
// echo "<pre>";
// print_r($data);
// die;
if (Auth::guard('admin')->attempt(['email' => $data['email'], 'password' => $data['password'], 'status' => 1])) {
return redirect('admin/dashboard');
} else {
return redirect()->back()->with('error_message', 'Invalid Email or Password');
}
}
return view('admin.login');
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
use HasFactory;
protected $guard = 'admin';
}
I already try changing the ['email' => $data['email'] to $data['email'] = 'example#example.com'
but it just give me the error that i have to migrate again which i'm not aiming for, should i migrate?

How to change the redirect when entering the Login Route via a back button after authentication in Laravel?

I have a middleware that redirects to the Login page and everything works fine:
*If I try to enter a page restricted by this middleware I am sent to login page and then I'm sent to that page.
*If I type /login directly, I will be redirected to /about.
I'm using a custom guard called "client".
Here is my code:
<?php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
use Closure;
class ClientLoggedIn
{
public function handle($request, Closure $next)
{
if (!Auth::guard('client')->check()) {
session(['url.intended' => url()->full()]);
return redirect()->route('login');
}
return $next($request);
}
}
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected function redirectTo()
{
$intendedURL = request()->session()->get('url.intended');
if (isset($intendedURL)) {
return $intendedURL;
}
return '/about';
}
public function __construct()
{
$this->middleware('guest:client')->except('logout');
}
protected function guard()
{
return Auth::guard('client');
}
public function showLoginForm()
{
return view('auth.login');
}
}
Whenever I click the back button after the authentication, however, a redirect to the site home (localhost:8000/) occurs (the back button refers to /login)
My question is: is there some configuration or callback that sets it to the home? I thought it would be the redirectTo, but it doesn't seem to be it since it is already set to /about. For what I know, when I click back doesn't occur a request to the server, so, how it works and how to change it?
Thanks in advance.

Laravel - Auth::user data return null except Login Controller

I am creating user authentication using a custom table. In my login controller authentication is working fine and redirected to dashboard. But when I am going to create another url using a new controller, user auth data not showing for that controller.
I want to get user data through auth facade in constructor. How will that possible?
Here is my code:
web.php:
<!---Routes for login and dashboard-->
Route::get('/login','CustomLogin#index');
Route::post('/login','CustomLogin#checklogin');
Route::get('/','CustomLogin#SuccessLogin');
Route::get('/logout','CustomLogin#logout');
<!---Routes for other controller where user auth not working-->
Route::get('/add-creditor', 'AddCreditor#index');
CustomLogin.php (controller):
<?php
namespace App\Http\Controllers;
use App\library\My_functions;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\User;
use Illuminate\Support\Facades\Auth;
use Redirect;
use View;
use Session;
use Cookie;
class CustomLogin extends Controller
{
public function __construct()
{
$this->_myFun = new My_functions;
}
public function index()
{
if(!Auth::check()) {
return view('CustomLogin.CustomLogin');
}
else{
Redirect::to(SITE_URL)->send();
}
}
public function username()
{
return 'user_name';
}
function checklogin(Request $request)
{
$this->validate($request, [
'input-username' => 'required',
'input-password' => 'required'
]);
$user_data = array(
'user_name' => $request->get('input-username'),
'password' => $request->get('input-password')
);
if(Auth::attempt($user_data)){
return redirect('/');
}
else
{
return back()->with('error','Wrong Login Details');
}
}
function SuccessLogin(){
if (!$this->_myFun->DoLogIn()) {
Redirect::to(SITE_URL.'login')->send();
}
else {
$data=array();
return View::make('include.dashboard',$data);
}
}
function logout(Request $request){
Auth::logout();
return redirect('/login');
}
}
Function DoLogIn() (app/library)
<?php namespace App\library {
use Illuminate\Routing\Controller as BaseController;
use App\library\CreateCrmGuid; // Get custom function
use App\library\FunctionForContact; // Get custom function
use Illuminate\Http\Request;
use Session;
use DB;
use Hash;
use Auth;
use App\User;
class My_functions{
public function DoLogIn()
{
//dd(Auth::user()); /*returns data if run from Login controller but null from Add Creditor controller*/
if(Auth::check())
{
$user_id = Auth::user()->id;
define('authenticated_user_id' ,$user_id);
return true;
}
else
{
return false;
}
}
}
AddCreditor Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Cookie;
use Config;
use App\library\my_functions; // Get custom function
use Redirect;
use DB;
use Session;
class AddCreditor extends Controller
{
protected $_myFun;
function __construct(){
dd(Auth::user()); // this returns null
$this->_myFun = new My_functions;
if (!$this->_myFun->DoLogIn()) {
Redirect::to(SITE_URL.'login')->send();
}
}
}
Add auth Middleware in your routes
Route::middleware(['auth'])->get('/add-creditor', 'AddCreditor#index');
Still, after this, you might not get user data through Auth facade in the controller constructor, But in your Route method i.e. AddCreditor#index you will get the user data either through the following methods
Auth::user();
or
request()->user();

Laravel 5.6: Auth::check() fails directly after successful Auth::login

I am using version 5.6 of Laravel.
I try to manually write a login controller which on success leads to the dashboard and on fail back to the login.
The problem is when i try to login the MainController allways redirects back to the login. So i checked if the user got logged in properly in the LoginController by checking if Auth::check works afer logging in and found out that it fails which makes it seem like for some reason the user doesn't get logged in properly even though i don't get any error messages.
I searched for hours and tried many things to fix this issue but with no success. So i hope someone can give me a pointer to how to solve this problem.
My setup looks like this:
web.php
<?php
Route::get('/', 'MainController')->name('dashboard');
Route::get('/login', 'LoginController')->name('login');
Route::post('/login/authenticate', 'LoginController#authenticate')->name('authenticate');
MainController.php
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class MainController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function __invoke()
{
return view('maintenance');
}
}
LoginController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
class LoginController extends Controller
{
$user = User::where('userid', $request->input('username'))
->where('user_pass', $request->input('password'))
->where('state', 0)
->first();
if($user) {
Auth::login($user);
if (Auth::check()) {
dd('Auth::check() failed');
}
//return redirect()->route('dashboard');
} else {
return redirect()->back()->withInput();
}
}
}
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $primaryKey = 'account_id';
protected $table = 'login';
public $timestamps = false;
}
if (Auth::check()) {
dd('Auth::check() failed');
}
User is logged in when Auth::check() returns true.
So other way around as it is in your code. Eg:
if (!Auth::check()) {
dd('Auth::check() failed');
}
You can try this code:
if (Hash::check('plain-text', $hashedPassword)) {
// The passwords match...
}
Try to use like that
public function store()
{
$credentials = array('user_displayname'=>Input::get('user_displayname'),
'user_password'=> Input::get('user_password'));
if (Auth::attempt($credentials))
{
return Auth::user();
}
return 'not logged';
}

Class App\Http\Controllers\Auth\Request does not exist. Laravel 5.3

I am using Laravel 5.3 My ForgotPasswordController looks like that:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Base\BaseController;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
class ForgotPasswordController extends BaseController
{
use SendsPasswordResetEmails;
public function __construct()
{
$this->middleware('guest');
}
public function showLinkRequestForm()
{
$title = $this->title;
$appName = $this->appName;
$action = $this->action;
return view('password.forgotPassword')->with(compact('title', 'appName', 'action'));
}
}
ResetPasswordController code :
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Base\BaseController;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends BaseController
{
use ResetsPasswords;
public function __construct()
{
$this->middleware('guest');
}
public function showResetForm(Request $request, $token = null)
{
return view('passwords.resetPassword')->with(
['token' => $token, 'email' => $request->email]
);
}
public function reset(Request $request)
{
$this->validate($request, [
'token' => 'required',
'password' => 'required|confirmed|min:6',
]);
// Here we will attempt to reset the user's password. If it is successful we
// will update the password on an actual user model and persist it to the
// database. Otherwise we will parse the error and return the response.
$response = $this->broker()->reset(
$this->credentials($request), function ($user, $password) {
$this->resetPassword($user, $password);
}
);
// If the password was successfully reset, we will redirect the user back to
// the application's home authenticated view. If there is an error we can
// redirect them back to where they came from with their error message.
return $response == Password::PASSWORD_RESET
? $this->sendResetResponse($response)
: $this->sendResetFailedResponse($request, $response);
}
}
My Admin Route :
Route::group(['namespace' => 'Auth'], function() {
Route::get('/forgotpassword/reset', 'ForgotPasswordController#showLinkRequestForm');
Route::post('/forgotpassword/email', 'ForgotPasswordController#sendResetLinkEmail');
Route::get('/password/reset/{token}', 'ResetPasswordController#showResetForm');
Route::post('/password/reset', 'ResetPasswordController#reset');
});
BaseController Code :
<?php
namespace App\Http\Controllers\Base;
use App\Http\Controllers\Controller;
class BaseController extends Controller
{
protected $appName = 'Stackoverflow';
protected $title = 'Welcome to Stackoverflow';
protected $action;
}
I can send the link to my email, but once I click the link/button.
It throws an error like above. Any idea ?
You are not using the required namespace, try to use the following in your controller:
use Illuminate\Http\Request;
You are getting the error due to the fact that your script tries to load the Request class from the current namespace :App\Http\Controllers\Auth
Request docs for Laravel 5.3

Categories