Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get user id in controller and Auth::check() is not working - Laravel 5.8

I am new to laravel 5.8 , am not able to use Auth in api controllers, below are the details :

  1. I have changed default users table from users to User

User.php

<?php

    namespace App;

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

    class User extends Authenticatable
    {
        use Notifiable;
        protected $table = 'User';

        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'firstName', 'lastName', 'email', 'password',
        ];

        /**
         * 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 = [
            'email_verified_at' => 'datetime',
        ];
    }
  1. Did not change anything in auth.php

    <?php 
      return 
    
    ['defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
    
    
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],
    
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
    
        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],
    
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ], 
    ];
    

My question : How can I make Auth::check() work in this controller and get user id. This returns Not logged

<?php

namespace App\Http\Controllers\api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Session;
use DB;
use Response;

use Auth;

class AccountsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
         //
        $userID = Auth::id();

        if (Auth::check()) {
            return "User logged , user_id : ".$userID ;
        }else{
            return "Not logged"; //It is returning this
        }
    }

}

I have tried several answers of similar questions ( Q1 Q2 ) but it didn't work.

like image 835
ikuchris Avatar asked Jan 01 '23 02:01

ikuchris


2 Answers

There would be no logged in user if using the API routes. Authentication for API's are done by tokens, you can read about it in the Laravel docs.

The session middleware is not called unless you use the web routes so the session is not started and therefore its not possible to use Auth.

What is it your trying to achieve as its not normal to use Auth with API routes.

like image 193
ColinMD Avatar answered Jan 05 '23 11:01

ColinMD


Use can try this for setting auth user in the session. This will set the user after authentication and set Auth::user().

Auth::attempt(['email' => request('email'), 'password' => request('password')])

like image 45
Vikas Rinvi Avatar answered Jan 05 '23 10:01

Vikas Rinvi