0

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();
        }
    }
}
1
  • A good practice to use Dependency Injection. Do not create class My_functions with 'new' Operator. Receive it via the Contractor parameter. Laravel will inject your class and resolve any dependency if needed. Commented Dec 31, 2019 at 8:49

1 Answer 1

2

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();

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you bro. Its working fine. But I want to get user data through Auth facade in the controller constructor. Isn't it possible?
Hi sumit. Isn't there any way not use auth middleware in each and every route?
That can be achieved by adding middlewares to the group of routes, so all your authenticated routes should lie in a group that uses the middleware. laravel.com/docs/5.8/routing#route-groups

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.