Use token to access a platform

2

I have a web app and I'm accessing an APP in this way:

.controller('LoginCtrl', function($scope, $ionicPopup, $state, $http) {
    $scope.data = {};

    $scope.login = function() {
        $http.post('http://localhost:3000/login', $scope.data.session_email, $scope.data.session_password).success(function(data) {
            $state.go('improvements');
        }).error(function(data) {
            var alertPopup = $ionicPopup.alert({
                title: 'Login failed!',
                template: 'User/Password is Wrong'
            });
        });
    }
});

but I'm getting the following error:

NoMethodError (undefined method '[]' for nil:NilClass):
  app/controllers/sessions_controller.rb:9:in 'create'

My controller:

def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password]) && user.permission == true
      params[:session][:remember_me] == '1' ? remember(user) : forget(user)
      remember user
      redirect_to user
    elsif user && user.permission == false
      flash.now[:notpermission] = "You do not have a permission"
      render "new"
    else
      flash.now[:error] = "Invalid password or email"
      render "new"
    end
  end

I know I need the token to access but I do not know how to do this ... can anyone give me a light?

    
asked by anonymous 09.09.2015 / 15:21

2 answers

1

You are using the POST method in the wrong way,

$http.post('http://localhost:3000/login', $scope.data.session_email, $scope.data.session_password)

When you inform that it is a post, you can use 2 parameters, 'URL' and 'OJETO', the way you are using you are passing 3 parameters, the easiest way would be to change to GET your request and pass the parameters by 'url', or create an OBJECT and pass the two parameters. $ scope.data.session_email, $ scope.data.session_password,

as properties of this object.

    
09.09.2015 / 16:37
2

It may be necessary to authorize the $http service to use cookies (which is where authentication probably preserves the bearer token ). Add the following snippet to the startup:

.config(function ($httpProvider) {
    $httpProvider.defaults.withCredentials = true;
});

Or, alternatively, specify in the call:

$http.get('url', { withCredentials: true})
    
09.09.2015 / 15:51