无法从laravel 5 application

时间:2015-11-17 20:43:19

标签: angularjs laravel laravel-5 jwt json-web-token

我正在为我的应用程序进行基于令牌的授权。我跟着 -

https://www.codetutorial.io/laravel-and-angularjs-token-based-auth-part1/

我设置了一个不同的前端文件夹,laravel用作后端。

用户能够注册并且凭据确实保存在数据库中,但是当我需要创建jwt令牌时,我收到错误: -

Tymon\JWTAuth\Exceptions\JWTException' with message '
The token could not be parsed from the request

RegisterController.php

class RegisterController extends Controller
{
public function register(Request $request)
{
    $newuser= $request->all();
    $password=Hash::make($request->input('password'));

    $newuser['password'] = $password;
    return Register::create($newuser);
}
}

LoginController.php

class LoginController extends Controller
{

public function authenticate(Request $request){
    $credentials = $request->only('email', 'password');
    try{
        if(! $token = JWTAuth::attempt($credentials)){
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    }
    catch(JWTException $e){
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    return response()->json(compact('token'));
}

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());
    }

    return response()->json(compact('user'));
}

}

routes.php文件

Route::post('/api/register', 'RegisterController@register');
Route::post('api/authenticate', 'LoginController@authenticate');
Route::get('api/authenticate/user', 'LoginController@getAuthenticatedUser');

我没有看到任何问题,它应该工作得很好,但不知道问题出在哪里。

1 个答案:

答案 0 :(得分:2)

我终于通过将.htaccess文件更新为

来解决了这个问题
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [L]

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
</IfModule>