1

I am trying to make auth based on third party api in laravel. not storing or using data from my local db. Here I am keeping api response data as an array just for visualization . It shows error Argument 1 passed to Illuminate\Auth\SessionGuard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, string given, called in vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php . How can I fix that. I also made R&D on it. Thanks in advance

    $user = [
        'status' => '200',
        'token' => 'aWsIpvOEZfv4sfSRUGS2dDeGw7',
        'id' => '12454545412',
        'user' => [
            'name' => 'xyz',
            'email' => 'xyz@gmail.com',
            'phone' => '12344787',
        ],
    ];

    $user = json_encode($user);
    Auth::login($user);
    return redirect( '/home' );
Mithun
  • 255
  • 1
  • 7
  • 17
  • Why do you need to authenticate when you are not validating user data? And if you are validating the user, you should have a eloquent model for the users with extending Authenticatable class. – Shailesh Matariya May 28 '20 at 10:14
  • Thanks for your reply. Actually I did not mention ** validating user data** section. When It get match data , that time api responses mentioned json data which I keep as an array for visualization . How can I make auth with this data ? – Mithun May 28 '20 at 10:26
  • Are you storing users in the database? Also, if you want to authenticate this data, you should create one helper class with extending Authenticatable class and assign these properties to it. – Shailesh Matariya May 28 '20 at 10:29
  • No I am not keeping this in user db and I don't want this – Mithun May 28 '20 at 10:34
  • Can you please make this solution as answer. I am not getting your solution – Mithun May 28 '20 at 10:35
  • Did you try `Auth::login($user['user']);`? – Makdous May 28 '20 at 12:35
  • Yes but not works ........ – Mithun May 28 '20 at 13:56
  • @Mithun Please see the below answer. As per my thought, it should be best fit for you. – Shailesh Matariya May 28 '20 at 14:12

1 Answers1

0

You should create User class as a dummy model if you prefer.

namespace App\Helpers; // depends on you 

use Illuminate\Foundation\Auth\User as Authenticatable;

class AuthUser extends Authenticatable
{
    protected $guarded = [];
}

And you can use like this:

$user = [
    'id' => '12454545412',
    'name' => 'xyz',
    'email' => 'xyz@gmail.com',
    'phone' => 1234564897,
    'token' => 'aWsIpvOEZfv4sfSRUGS2dDeGw7' // if you need in your user Object
];
$user = new AuthUser($user);
Auth::login($user);
dd(auth()->user());