1public function login(Request $request){
2 $fields = $request->validate([
3
4 'email'=>'required|string|email',
5 'password'=>'required|string'
6 ]);
7
8 //Check email
9
10 $user= User::where('email', $fields['email'])->first();
11
12 //Check Password
13 if(!$user || !Hash::check($fields['password'], $user->password) ){
14 return response([
15 'message'=>'Invalid Credentials'
16 ], 401);
17 }
18
19 $token = $user->createToken('myapptoken')->plainTextToken;
20
21 $response= [
22 'user' => $user,
23 'token'=> $token
24 ];
25
26 return response($response, 201);
27 }
28
1$data = $request->all();
2Auth::attempt(['email'=>$data['email'] , 'password'=>$data['password']])