Laravel jwt auth login code example

Example 1: laravel jwt

composer require tymon/jwt-auth

Example 2: jwt auth laravel auth without password field

public function authenticate(Request $request) { 
    $email = $request->input('email');
    $user = User::where('email', '=', $email)->first();
    try { 
        // verify the credentials and create a token for the user
        if (! $token = JWTAuth::fromUser($user)) { 
            return response()->json(['error' => 'invalid_credentials'], 401);
        } 
    } catch (JWTException $e) { 
        // something went wrong 
        return response()->json(['error' => 'could_not_create_token'], 500); 
    } 
    // if no errors are encountered we can return a JWT 
    return response()->json(compact('token')); 
}

Tags:

Sql Example