Advertisement
Guest User

Untitled

a guest
May 5th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. class ApiController extends Controller
  2. {
  3.  
  4. public function sendRequest(Request $request){
  5. $rules = [
  6. 'username' => 'required',
  7. 'password' => 'required',
  8. //other rules
  9. ];
  10.  
  11. // Initialize response array
  12. $response['success'] = false;
  13.  
  14. // Validate the input
  15. $validator = Validator::make($request->all(), $rules);
  16.  
  17. if ($validator->fails())
  18. {
  19. $response['errors'] = $validator->getMessageBag()->toArray();
  20. goto send_response;
  21. }
  22.  
  23. $username = $request->input('username');
  24. $password = $request->input('password');
  25.  
  26. $user = UserAuth::where('username', $username)->first();
  27.  
  28. // Check if user exists
  29. if (null === $user){
  30. $response['errors']['username'] = 'Invalid Username';
  31. goto send_response;
  32. }
  33.  
  34. // Check if password matches
  35. if (!Hash::check($password, $user->password)){
  36. $response['errors']['username'] = 'Invalid Password';
  37. goto send_response;
  38. }
  39.  
  40. // other code here, at the end set $response['success'] to true
  41.  
  42. send_response:
  43. return response()->json($response);
  44.  
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement