0
私がやってみたいのは、ユーザーを登録/ログインするためのフロントエンドの角をレールapiに接続することです。私は認証方法についての助けが必要です。 CORSの宝石がレールのapiに取り付けられました。良い定型文や良い文書があれば、リンクを送ってください!前もって感謝します!一部Angular.jsを使用してRails APIに接続しているときに、ユーザーを登録するにはどうすればよいですか?
ログイン:
<form name="regField" ng-submit="create(user, regField.$valid)" novalidate>
<fieldset>
<legend>Register</legend>
<label>Username<input type="text" ng-model="user.username" required></label>
<label>PW<input type="password" ng-model="user.password" required></label>
<label>PW confirm<input type="password" ng-model="user.pw_confirm" required></label>
<button>Submit</button>
</fieldset>
ログインコントローラー:
app.controller('loginController', ['$scope','$location','userFactory', function($scope, $location, UserFactory){
$scope.loginErr = "";
$scope.regErr = "";
// UserFactory.allAppointments();
$scope.create = function(user, isValid){
if(isValid){
UserFactory.create(user,function(res){
if(res.data.duplicate){
$scope.regErr = "Username must be unique";
} else {
$location.url('/dashboard');
}
})
} else{
$scope.regErr = "Invalid Combination";
}
}
$scope.login = function(user, isValid){
if (isValid){
UserFactory.login(user,function(){
$location.url('/dashboard');
})
} else {
$scope.loginErr = "Invalid Combination";
}
}
}])
UserFactory:
app.factory('userFactory', ['$http', function($http){
var currentUser = {};
return {
getCurrentUser: function(callback){
$http({
method: "GET",
url: "/currentUser"
}).then(function(user){
currentUser = user;
callback(user.data);
})
},
create:function(user, callback){
$http({
method:"POST",
url:"https://nameofapp.herokuapp.com/api/v1/auth",
dataType:'json',
headers: {
'Content-Type': 'application/json'
}
}).then(function(user){
currentUser = user;
callback(user);
})
},
login:function(user,callback){
$http({
method:"POST",
url:"/login",
data:user
}).then(function(user){
currentUser = user;
callback(user);
})
},
logout: function(callback){
$http({
method:"GET",
url:'/logout'
}).then(callback)
}
}
}])
https://github.com/rjurado01/rails_jwt_authを試すことができます – drinor