Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pratheep's avatar

authentication session problem

$customer = Customer::find(6);
        Auth::login($customer);
        if (Auth::check()) {
            return Auth::user()->name;
        }

It's return user name but auth session not work because when redirect another page I can't find auth user any body please help

Working perfect for User instance

$user= User::find(6);
        Auth::login($user);
        if (Auth::check()) {
            return Auth::user()->name;
        } 
0 likes
6 replies
Pratheep's avatar

Thank you for replay

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Customer extends Authenticatable
{
  use Notifiable;
  protected $guard = 'customer';
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  protected $fillable = [
    'customerid', 'name', 'email', 'provider',
  ];

  /**
   * The attributes that should be hidden for arrays.
   *
   * @var array
   */
  protected $hidden = [
    'password', 'remember_token',
  ];

  /**
   * The attributes that should be cast to native types.
   *
   * @var array
   */
  protected $casts = [];
}

This is my customer model

MichalOravec's avatar
Level 75

Propably you have to set another quard like:

$customer = Customer::find(6);

Auth::guard('another_guard')->login($customer);

Just change another_guard for your guard what you use for Customer model.

1 like
Pratheep's avatar

Thank You so much it's working

 $customer = Customer::find(6);
        Auth::guard('customer')->login($customer);
        if (Auth::guard('customer')->check()) {
            return "success";
        }

It's working return success on another pages

if(Auth::guard('customer')->check()){
  echo 'success';
}else{
  echo 'fail';
}
Pratheep's avatar

It's working fine

 $customer = Customer::where('customerid', $customer->getId())->first();
        Auth::guard('customer')->login($customer);

This is determine authenticated user

Auth::guard('customer')->check();

but i cant find authenticated user using this code

Auth::check();

Please or to participate in this conversation.