<commit_before>/**
 * Created by epotignano on 25/02/16.
 */
import { UidRef, ApiRef, TokenRef } from '../constants/Commons';
const LoginEndpoint = ApiRef + '/oauth/token';
import Firebase from 'firebase';
import { getUser } from '../actions/UserActions';

import {
    LOGIN_ATTEMP,
    LOGIN_FAILURE,
    LOGIN_SUCCESS,
    REGISTER_ATTEMP,
    LOGIN_ATTEMP_FACEBOOK,
   REGISTER_SUCCESS,
   REGISTER_FAILURE
} from "../constants/ActionTypes";

function LoginAttempt (credentials) {
  return {
    type: LOGIN_ATTEMP,
    isFetching: true,
    isAuthenticated: false,
    credentials
  }
}

function LoginAttempFacebook() {
  return {
    type: LOGIN_ATTEMP_FACEBOOK,
    isFetching: true,
    isAuthenticated: false
  }
}

function loginSuccess(user, loginType) {
  return {
    type: LOGIN_SUCCESS,
    isFetching: false,
    isAuthenticated: true,
    loginType,
    token: user.token
  }
}

function loginError(error) {
  return {
    type: LOGIN_FAILURE,
    isFetching: false,
    isAuthenticated: false,
    code: error.code
  }
}

function RegisterAttemp() {
 return {
    type: REGISTER_ATTEMP,
    isFetching: true,
    isAuthenticated: false
  }
}

function RegisterFailure(error) {
 return {
    type: REGISTER_FAILURE,
    isFetching: true,
    code: error.code
  }
}

function RegisterSuccess(user) {
 return {
    type: REGISTER_SUCCESS,
    isFetching: false,
    user
  }
}

export function loginUser(credentials) {
  credentials.grant_type = 'password';
  return dispatch => {
    dispatch(LoginAttempt(credentials));
    fetch(LoginEndpoint, {
      'method': 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept':'application/json'
      },
      body: JSON.stringify(credentials)
    }).then(data => {
      console.log(data);
        dispatch(loginSuccess(data));
    }).cacth(error => {
      console.log(error);
      dispatch(loginError(error));
    });
  }
}

/*export function registerUser(userData) {
  return dispatch => {
    var _modifiedEmail = userData.email.replace(/\./g, '');
    var _optins = new Firebase(FireRef + 'optins/' + _modifiedEmail);

    var _userData = {
      email: userData.email,
      password : <PASSWORD>(36).slice(-8)
    };

    _optins.set(_userData, (error)=> {
      if(error) {
        dispatch(RegisterFailure(error));
        return Promise.reject(_userData)
      }
      else {
        // Dispatch the success action
        dispatch(RegisterSuccess(_userData));
        dispatch(loginUser({username: _userData.email, password: _userData.password}));<commit_msg>Sending params as querystring<commit_after>/**
 * Created by epotignano on 25/02/16.
 */
import { UidRef, ApiRef, TokenRef } from '../constants/Commons';
const LoginEndpoint = ApiRef + '/oauth/token';

import {
    LOGIN_ATTEMP,
    LOGIN_FAILURE,
    LOGIN_SUCCESS,
    REGISTER_ATTEMP,
    LOGIN_ATTEMP_FACEBOOK,
   REGISTER_SUCCESS,
   REGISTER_FAILURE
} from "../constants/ActionTypes";

function LoginAttempt (credentials) {
  return {
    type: LOGIN_ATTEMP,
    isFetching: true,
    isAuthenticated: false,
    credentials
  }
}

function LoginAttempFacebook() {
  return {
    type: LOGIN_ATTEMP_FACEBOOK,
    isFetching: true,
    isAuthenticated: false
  }
}

function loginSuccess(user, loginType) {
  return {
    type: LOGIN_SUCCESS,
    isFetching: false,
    isAuthenticated: true,
    loginType,
    token: user.token
  }
}

function loginError(error) {
  return {
    type: LOGIN_FAILURE,
    isFetching: false,
    isAuthenticated: false,
    code: error.code
  }
}

function RegisterAttemp() {
 return {
    type: REGISTER_ATTEMP,
    isFetching: true,
    isAuthenticated: false
  }
}

function RegisterFailure(error) {
 return {
    type: REGISTER_FAILURE,
    isFetching: true,
    code: error.code
  }
}

function RegisterSuccess(user) {
 return {
    type: REGISTER_SUCCESS,
    isFetching: false,
    user
  }
}

export function loginUser(credentials) {
  credentials.grant_type = 'password';

  var _url = Object.keys(credentials).map(function(k) {
    return encodeURIComponent(k) + '=' + encodeURIComponent(credentials[k])
  }).join('&');

  return dispatch => {
    dispatch(LoginAttempt(credentials));
    fetch(LoginEndpoint, {
      'method': 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept':'application/json'
      },
      body: _url
    }).then(data => {
      console.log(data);
        dispatch(loginSuccess(data));
    })
  }
}

/*export function registerUser(userData) {
  return dispatch => {
    var _modifiedEmail = userData.email.replace(/\./g, '');
    var _optins = new Firebase(FireRef + 'optins/' + _modifiedEmail);

    var _userData = {
      email: userData.email,
      password : <PASSWORD>)
    };

    _optins.set(_userData, (error)=> {
      if(error) {
        dispatch(RegisterFailure(error));
        return Promise.reject(_userData)
      }
      else {
        // Dispatch the success action
        dispatch(RegisterSuccess(_userData));
        dispatch(loginUser({username: _userData.email, password: _userData.password}));
{'fraction_non_alphanumeric': 0.12185120093731693, 'fraction_numerical': 0.0029291154071470417, 'mean_word_length': 3.094324540367706, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3681671', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1692, 'n_words': 324}