1

I am working for an application that will have a Laravel based back-end and laravel + angularJs based front-end.

I have completed about 40% in back-end, now I need to implement the front-end, but I am not getting a best practice for that.

I have tried with login page but I am unable to get the errors and responses in json. I have checked in request, it shows me the login page with error in response.

I have searched a lot for that, and find the solution JWT for that.

Is there any other way to do that, so that I can get all responses and errors in json?

1 Answer 1

1

Your title suggests that you don't want using JWT because you can't catch exceptions :-)

You have to abstract your problem, or better: convert it to a challenge.

Let's begin:

The JWT package has different exceptions, e.g. token blacklisted, enz. So what's your task? To catch that exception! Yeah, so what? Yes, you can return that json to your view.

Below a good example from the JWT site

public function getAuthenticatedUser()
{
    try {

        if (! $user = JWTAuth::parseToken()->authenticate()) {
            return response()->json(['user_not_found'], 404);
        }

    } catch (Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {

        return response()->json(['token_expired'], $e->getStatusCode());

    } catch (Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {

        return response()->json(['token_invalid'], $e->getStatusCode());

    } catch (Tymon\JWTAuth\Exceptions\JWTException $e) {

        return response()->json(['token_absent'], $e->getStatusCode());
    }
}

The method above returns json. To display this information in your view, it's an AngularJS issue and not a JWT issue.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestion, but I am looking for any inbuilt functionality in laravel 5 by using that I can get all errors and responses in json form if possible. I do not want to use any third party tool for that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.