mercredi 17 mars 2021

Frontend user also Logged in When Admin Login in Laravel(both details are on users table) and also logout

Here I using Role Model and Roletype Model but how to stop frontend login when admin gets login? please help me!

User Model:-

protected $fillable = [
    'f_name', 'l_name','email','phone_no', 'password','user_password','api_token','status','provider_name','provider_id'
];

Role Model:-

protected $table = 'roles';
protected $fillable = ['name','display_name','description'];
protected $visible = ['id','name','display_name','description'];
public $timestamps = true;

RoleUser Model:-

protected $table = 'role_user';
protected $fillable = ['user_id','role_id'];
protected $visible = ['user_id','role_id'];
public $timestamps = true;

Wher User Login:-

public function user_login_check(Request $request){
    $msg = [
        'email.required' => 'Enter Your Email',
        'password.required' => 'Enter Your Password',
    ];
    $this->validate($request, [
        'email' => 'bail|required',
        'password' => 'bail|required'

    ], $msg);
    $email = $request->get('email');
    $pass = $request->get('password');
    $uid = User::where('email', $email)->orwhere('phone_no',$email)->value('id');
    $status = User::where('email', $email)->orwhere('phone_no',$email)->value('status');
    $role_id = RoleUser::where('user_id', $uid)->value('role_id');
    $role = Role::where('id', $role_id)->value('name');
    $remember = false;
    if($request->get('remember') !=null){
        $remember = true;
    }
    if ($role == 'user') {
        if($status=='Active'){
            if (Auth::attempt(array('email' => $email, 'password' => $pass,'status'=>'Active'), $remember) || Auth::attempt(array('phone_no' => $email, 'password' => $pass,'status'=>'Active'), $remember)) {
                if($request->session()->has('checkout')){
                    $request->session()->forget('checkout');
                    return redirect(url('/proceed-to-checkout'));
                }else{
                    return redirect(url('/'));
                }
            } else {
                return redirect()->back()->with('login_error', 'Login Failed !!! Please check Your Email/Phone and Password.');
            }
        }else{
            return redirect()->back()->with('login_error', 'Login Failed !!! Please verify your email for login or reset password.');
        }
    }else{
        return redirect()->back()->with('login_error', 'Login Failed !!! Please register first.');
    }
}

Where Admin Login:-

public function Check_login(Request $request)
{
    //  dd($request->all());

    $msg = [
        'email.required' => 'Enter Your Email',
        'password.required' => 'Enter Your Password',
    ];
    $this->validate($request, [
        'email' => 'bail|required|email',
        'password' => 'bail|required|alphaNum|min:3'

    ], $msg);

    $email = $request->get('email');
    $pass = $request->get('password');
    $uid = User::where('email', $email)->value('id');
    if ($uid == '') {
        return redirect()->back()->with('error', 'Login Failed !!! Please check Your Email and Password.');
    } else {
        $role_id = RoleUser::where('user_id', $uid)->value('role_id');
        $role = Role::where('id', $role_id)->value('name');

        if ($role == 'admin') {
            if (Auth::attempt(array('email' => $email, 'password' => $pass, 'status' => 'Active'), true)) {
                $check_email = Auth::user()->email;
                $request->session()->put('email', $check_email);
                $user_type = Auth::user()->user_type;
                $request->session()->put('user_type', $user_type);
                return redirect(route('admin::dashboard'));
            } else {
                return redirect()->back()->with('error', 'Login Failed !!! Please check Your Email and Password.');
            }
        } else if ($role == 'sub admin') {
            if (Auth::attempt(array('email' => $email, 'password' => $pass, 'status' => 'Active'), true)) {
                $check_email = Auth::user()->email;
                $request->session()->put('email', $check_email);
                $user_type = Auth::user()->user_type;
                $request->session()->put('user_type', $user_type);
                return redirect(route('admin::dashboard'));
            } else {
                return redirect()->back()->with('error', 'Login Failed !!! Please check Your Email and Password.');
            }
        }
    }
}

My Middleware:-

public function handle($request, Closure $next)
{
    if ( isset(Auth::user()->id) && (Auth::user()->hasRole(['admin', 'sub admin']))) {
        return $next($request);
    }else{
        return redirect(route('admin'));
    }
}

Admin Login Route:-

Route::get('admin', ['as' => 'admin', 'uses' => 'Admin\AdminController@index']);


Route::post('admin-login', ['as'=>'admin_login' ,'uses'=>'Admin\AdminController@Check_login']);

Route::get('logout', ['as' => 'logout', 'uses' => 'Admin\AdminController@logout']);

User Login Route:-

Route::post('user-login-check', ['as' => 'user_login_check', 'uses' => 'Frontend\LoginController@user_login_check']);


Route::get('user-logout', ['as' => 'user_logout', 'uses' => 'Frontend\LoginController@user_logout']);

I check everything before login but why this happen? why I logged in as admin user also gets login with same email ID



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire