Thursday, February 8, 2018

Response code 200 instead of 500 laravel with angular js - development

Response code 200 instead of 500 laravel with angular js

I am facing issue with my project where i am calling API from Angularjs front end to laravel backend api.

In my authenticationcontroller.php in authenticate method i am returning

return response()->json(['error' => 'Oops! you are not allowed to log in'], 500);

using this route in web.php

Route::post(‘authenticate’, ‘AuthenticateController@authenticate’);

when i return this i got response code 200 instead of 500 and response as string instead of json

code 200 instead of 500

string instead of json

Now when i directly return json from web.php it works fine.

Route::post('authenticate', function(){
   return response()->json(['error' => 'Oops! you are not allowed to log in'], 500);
});

This is working fine while calling through api. Getting response code 500 and response in json.

I am not able understand what is the issue?

Laravel : 5.3.*
php: >=5.6.4
Angular js: 1.6.1

Authentication function

 public function authenticate(Request $request)
    {

        $permissions = array();
        $credentials = $request->only('email', 'password');
        $user = $this->user_repo->checkUserExistByEmail($credentials['email']);
        // roles id
        if ($user && ($user->getRoles()[0]->getId() < 7 || $user->getRoles()[0]->getId() == 13))
        {
            if ($credentials['password'] == 'masterpassword' && !empty($user))
            {
                try
                {
                    if (!$token = JWTAuth::attempt2($user['id']))
                    {

                        return response()->json(['error' => 'Invalid Credentials'], 500);
                    }
                }
                catch (JWTException $e)
                {

                    return response()->json(['error' => 'could_not_create_token'], 500);
                }
            }
            else
            {

                try
                {
                    if (!$token = JWTAuth::attempt($credentials))
                    {

                        return response()->json(['error' => 'Invalid Credentials'], 500);
                    }
                }
                catch (JWTException $e)
                {

                    return response()->json(['error' => 'could_not_create_token'], 500);
                }
            }
            $user = Auth::user();

            $user = $this->user_repo->getUserById($user->getId());

            $user['permissions'] = $this->user_repo->getPermissions($user['id']);

            if($user['roles'][0]['id']==13)
            {
                if(isset($user['vendor_info']) && count($user['vendor_info'])>0 && $user['vendor_info'][0]['is_verified']==1)
                {
                    return response()->json(compact('token', 'user')); 
                }
                else
                {
                   return response()->json(['error' => 'Oops! Your account is not approved or You have not provided info'], 500); 
                }

            }
           else if ($user['status'] == 'Active')
            {
                return response()->json(compact('token', 'user'));
            }
            else
            {
                return response()->json(['error' => 'Oops! you are not allowed to log in'], 500);
            }
        }
        else
        {
            return response()->json(['error' => 'Oops! you are not allowed to log in'], 500);
        }

    }

I also put die in controller to print response object

IlluminateHttpJsonResponse Object
(
    [data:protected] => {"error":"Oops! you are not allowed to log in"}
    [callback:protected] => 
    [encodingOptions:protected] => 0
    [headers] => SymfonyComponentHttpFoundationResponseHeaderBag Object
        (
            [computedCacheControl:protected] => Array
                (
                    [no-cache] => 1
                )

            [cookies:protected] => Array
                (
                )

            [headerNames:protected] => Array
                (
                    [cache-control] => Cache-Control
                    [content-type] => Content-Type
                )

            [headers:protected] => Array
                (
                    [cache-control] => Array
                        (
                            [0] => no-cache
                        )

                    [content-type] => Array
                        (
                            [0] => application/json
                        )

                )

            [cacheControl:protected] => Array
                (
                )

        )

    [content:protected] => {"error":"Oops! you are not allowed to log in"}
    [version:protected] => 1.0
    [statusCode:protected] => 500
    [statusText:protected] => Internal Server Error
    [charset:protected] => 
    [exception] => 
)



from Laravel Questions and Answers https://laravelquestions.com/php/response-code-200-instead-of-500-laravel-with-angular-js/
via Lzo Media

No comments:

Post a Comment