我已经建立了一个前端角度系统和后端laravel系统。一切都运行良好但是在登录过程中没有生成令牌。注册过程运行良好,并将用户详细信息存储在数据库中。
routes.php文件
Route::post('/api/register', 'RegisterController@register');
Route::post('api/authenticate', 'LoginController@authenticate');
Route::get('api/authenticate/user/email/{email}/password/{password}', 'LoginController@getAuthenticatedUser');
RegisterController.php
public function register(Request $request){
$newuser= $request->all();
$password=Hash::make($request->input('password'));
$newuser['password'] = $password;
return Register::create($newuser);
}
LoginController.php
/*public function authenticate(Request $request){*/
public function authenticate($email, $password){
/*$credentials = $request->only('email', 'password');*/
$credentials = array('email' => $email, 'password' => $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($email, $password){
try{
if(! $user = JWTAuth::parseToken()->authenticate($email, $password)){
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'));
/*return \Response::json(array('user' => 'Steve', 'state' => 'CA'));*/
}
authController.js
reglogApp.controller('AuthController', function($scope, $http, $auth, $rootScope, $state){
$scope.email='';
$scope.password='';
$scope.newUser={};
$scope.loginError=false;
$scope.loginErrorText='';
$scope.login = function(){
var credentials = {
email: $scope.email,
password: $scope.password
}
console.log('Entered Login Function', credentials);
$auth.login(credentials).then(function(){
return $http.get('http://reglog.xyz.com/api/authenticate/user/email/puppy@gmail.com/password/ABHVF123456789000');
},function(error){
$scope.loginError = true;
$scope.loginErrorText = error.data.error;
console.log('Login Error', $scope.loginErrorText);
}).then(function(response){
$rootScope.currentUser = response.data.user;
$scope.loginError = false;
$scope.loginErrorText = '';
console.log('Current User', $rootScope.currentUser);
$state.go('dashboard');
});
}
$scope.register = function(){
$scope.name = $scope.newUser.name;
$scope.email = $scope.newUser.email;
$scope.password = $scope.newUser.password;
console.log($scope.name, $scope.email, $scope.password);
$http.post('http://reglog.xyz.com/api/register', $scope.newUser).success(function(data){
console.log('Registered');
$scope.email = $scope.newUser.email;
$scope.password = $scope.newUser.password;
$scope.login();
});
}
});
答案 0 :(得分:3)
您需要在标头中发送授权令牌。 您可能需要修改Apache设置以允许发送授权标头。
授权:持票人{yourtokenhere}
您可以使用以下命令修改.htaccess文件:
RewriteCond%{HTTP:授权} ^(。*)
RewriteRule。* - [e = HTTP_AUTHORIZATION:%1]
答案 1 :(得分:2)
JWTAuth::parseToken()
要求使用以下格式在标头中发送令牌:
Authorization: Bearer {yourtokenhere}
我相信,你不这样做。所以,你的getAuthenticatedUser
失败了。
答案 2 :(得分:1)
您可以尝试在php.ini中设置always_populate_raw_post_data = -1
这对我有用
答案 3 :(得分:0)
我想LoginController::getAuthenticatedUser()
似乎要验证用户的凭据(电子邮件和密码)。
但是,JWTAuth::parseToken()->authenticate($email, $password)
方法中还有getAuthenticatedUser()
。 JWTAuth::parseToken()
负责验证来自请求1的令牌。客户端必须使用令牌向 GET / api / authenticate 端点发送请求。
在我看来,LoginController::authenticate()
通过JSON Web Token识别并向客户回复用户的信息。 email
中不需要password
和LoginController:: getAuthenticatedUser()
参数,因为令牌能够识别用户。
LoginController.php
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'));
/*return \Response::json(array('user' => 'Steve', 'state' => 'CA'));*/
}
答案 4 :(得分:-2)
简单编辑.htaccess文件。即在/public/.htaccess里面
选项-MultiViews
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]