Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ApiController extends Controller
- {
- public function sendRequest(Request $request){
- $rules = [
- 'username' => 'required',
- 'password' => 'required',
- //other rules
- ];
- // Initialize response array
- $response['success'] = false;
- // Validate the input
- $validator = Validator::make($request->all(), $rules);
- if ($validator->fails())
- {
- $response['errors'] = $validator->getMessageBag()->toArray();
- goto send_response;
- }
- $username = $request->input('username');
- $password = $request->input('password');
- $user = UserAuth::where('username', $username)->first();
- // Check if user exists
- if (null === $user){
- $response['errors']['username'] = 'Invalid Username';
- goto send_response;
- }
- // Check if password matches
- if (!Hash::check($password, $user->password)){
- $response['errors']['username'] = 'Invalid Password';
- goto send_response;
- }
- // other code here, at the end set $response['success'] to true
- send_response:
- return response()->json($response);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement