我已经建立了一个前端角度系统和后端 laravel 系统。一切正常,但在登录过程中没有生成令牌。注册过程运行良好,并将用户详细信息存储在数据库中。
路由.php
Route::post('/api/register', 'RegisterController@register');
Route::post('api/authenticate', 'LoginController@authenticate');
Route::get('api/authenticate/user/email/{email}/password/{password}', 'LoginController@getAuthenticatedUser');
注册控制器.php
public function register(Request $request){
$newuser= $request->all();
$password=Hash::make($request->input('password'));
$newuser['password'] = $password;
return Register::create($newuser);
}
登录控制器.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();
});
}
});