mardi 31 décembre 2019

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


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2SMpqo1
via IFTTT

Aucun commentaire:

Enregistrer un commentaire