2

After posting a question Lumen + Dingo + JWT is not instantiable while building about Lumen and Dingo here in SO I got a nice detailed answer on how to set up such a system.

Within his setup there is a small authentication example, which uses Eloquent. Now we are loading an custom framework within Lumen, which has its own models etc, and has its own database connection etc.

What I can not seen to figure out is how to completely remove Eloquent, and do the authentication using our own framework.

What I have done so far:

  • Removed $app->withEloquent(); from our bootstrap\app.php

Other edits I think that need to be done is editing config\auth.php, or maybe even completely removing this file. I am not really sure.

Lastly, within App\Api\v1\Controllers\AuthController@postLogin there is made a call to a validate function. This function needs to communicate with my framework and not via Eloquent. How this is done neatly in Lumen I am also not sure.

Git repo: https://github.com/krisanalfa/lumen-dingo

Community
  • 1
  • 1
Saif Bechan
  • 16,551
  • 23
  • 83
  • 125

1 Answers1

2

You may read this. So in your case, in App\Api\v1\Controllers\AuthController@postLogin:

/**
 * Handle a login request to the application.
 *
 * @param \Illuminate\Http\Request $request
 *
 * @return \Illuminate\Http\Response
 */
public function postLogin(Request $request)
{
    try {
        $this->validate($request, [
            'email' => 'required|email|max:255',
            'password' => 'required',
        ]);
    } catch (HttpResponseException $e) {
        return response()->json([
            'message' => 'invalid_auth',
            'status_code' => IlluminateResponse::HTTP_BAD_REQUEST,
        ], IlluminateResponse::HTTP_BAD_REQUEST);
    }

    $credentials = $this->getCredentials($request);

    try {
        // Attempt to verify the credentials and create a token for the user
        // You may do anything you like here to get user information based on credentials given
        if ($user = MyFramework::validate($credentials)) {
            $payload = JWTFactory::make($user);

            $token = JWTAuth::encode($payload);
        } else {
            return response()->json([
                'message' => 'invalid_auth',
                'status_code' => IlluminateResponse::HTTP_BAD_REQUEST,
            ], IlluminateResponse::HTTP_BAD_REQUEST);
        }
    } catch (JWTException $e) {
        // Something went wrong whilst attempting to encode the token
        return response()->json([
            'message' => 'could_not_create_token',
        ], IlluminateResponse::HTTP_INTERNAL_SERVER_ERROR);
    }

    // All good so return the token
    return response()->json([
        'message' => 'token_generated',
        'token' => $token,
    ]);
}
krisanalfa
  • 6,268
  • 2
  • 29
  • 38
  • 1
    Ah, so it was actually `JWTAuth::attempt($credentials)` in your original code that checked the user in the database, I thought it was in the try/catch block above that. Thanks I will have a go at this. – Saif Bechan Mar 29 '16 at 14:10
  • I am still getting an error that a connection to the database could not be made. I am trying to figure out where this connection is made to a database other than the one defined in our framework. – Saif Bechan Mar 29 '16 at 14:16
  • Since I haven't access to dig-up your framework, I cannot provide any answer. – krisanalfa Mar 29 '16 at 14:47
  • Hello. I have one more open ticket on the repo, if you got the time maybe you can look at it. It is about using a custom `user provider`. – Saif Bechan Apr 20 '16 at 05:41