I have a trouble while attempting to create login for rest API using Laravel 5.3.
I created a student model which extends User, it has email and password (in migration).
This is the guards (I have changed the provider to students)
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'students'
],
],
And this is the providers
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'students' => [
'driver' => 'eloquent',
'model' => App\Student::class,
],
'teachers' => [
'driver' => 'eloquent',
'model' => App\Teacher::class,
],
],
I want to make a login API (rest) using student's credentials. If login is successful the server should return personal information of student. I have searched laracasts a lot but didn't find something similar. And there are a lot changes in new version. so I am confused.
Student login function
public function login(Request $request){
if (Auth::check(['email' => $request->email, 'password' => $request->password])) {
//$student = Student::where('email',$request->email)->first();
$student = Auth::user();
$student->api_token = str_random(60);
$student->save();
return response([
'status' => Response::HTTP_OK,
'response_time' => microtime(true) - LARAVEL_START,
'student' => $student
],Response::HTTP_OK);
}
return response([
'status' => Response::HTTP_BAD_REQUEST,
'response_time' => microtime(true) - LARAVEL_START,
'error' => 'Wrong email or password',
'request' => $request->all()
],Response::HTTP_BAD_REQUEST);
}
neither the Auth::check() method nor the Auth::validate() work in this case. What can be the problem? How do I implement login for rest api?