Support the ongoing development of Laravel.io →
Authentication Requests Configuration

Hello everyone!

I have a small problem that i cant figure out.

I have a AuthController with two functions, Login() and GetGroupMembers().

Login function is working and logs in the user BUT, the Auth::check in the GetGroupMembers always return null, it seems like the user is only logged in in the Login() but not anywhere else?

Btw every input from the user is coming in to the server by AJAX, is this maybe te problem?

What am i doing wrong?

use Illuminate\Http\Request; use Illuminate\Http\Response; use Auth; use App\Http\Requests; use DB;

class AuthController extends Controller {

 public function Login(Request $request){
   
     if(Auth::attempt(['email'=>$request->email,'password'=>$request->password])){
           return response()->json('Authenticated!',200);
     }else{
         return response()->json("Not authenticated..",401);
     }

}



public function GetGroupMembers(){

  if(Auth::check()){ // this is always FALSE

        $user_group_id = DB::table('groups')
                            ->select('group_id')
                            ->join('users', 'users.user_id', '=', 'groups.user_id')
                            ->where('email','=','bazinga@gmail.com') 
                            ->get();
                    
        $user_group = DB::table('groups')
                ->select('group_id','users.name','users.phone','users.email')
                ->join('users', 'users.user_id', '=', 'groups.user_id')
                ->where('group_id','=',$user_group_id[0]->group_id)
                ->get();      
                
        return response()->json($user_group,200);
  }
      
   return response()->json('Not Authenticated',401); 

}

}

Route::get('/contacts','AuthController@GetGroupMembers'); Route::post('/login','AuthController@Login');

Last updated 2 years ago.
0

Thumb rule These all routes should be encapsulated in the web middleware which are uses Auth(). Documentation

Correction:

Route::group(['middleware' => 'web'], function () {

    Route::auth();

    Route::get('/contacts','AuthController@GetGroupMembers'); 
    Route::post('/login','AuthController@Login');
});
Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

spoofy spoofy Joined 27 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.