anchor.diff:  -spec add_server_side_fields(Json::string()) -> string().
   add_server_side_fields(Json) ->
       lib_json:add_values(Json,[{rankings, "[]"},{notifications,"[]"},{triggers,"[]"},{subscriptions, "[]"}]).
  -
  -
  -%% @doc
  -%% Function: add_openid_fields/2
  -%% Purpose: Used to add all the fields that should be added server side
  -%% Returns: The new json with the fields added
  -%% @end
  --spec add_openid_fields(Json::string()) -> string().
  -add_openid_fields(Json) ->
  -    AccessToken  = plus_srv:get_access_token(),
  -    RefreshToken = plus_srv:get_refresh_token(),
  -    lib_json:add_values(Json, [{access_token, AccessToken}, {refreshtoken, RefreshToken}]).
  -
  
positive.code:     case {wrq:get_qs_value("code", ReqData), wrq:get_qs_value("state", ReqData)} of
          {undefined, _} ->
              {{halt,403}, wrq:set_resp_body("State missing", ReqData), State};
          {_, undefined} ->
              {{halt,403}, wrq:set_resp_body("Code missing", ReqData), State};
          {Code, AuthState} when Code =/= "", AuthState =/= "" ->
              Token = plus_srv:exchange_token(Code, AuthState),
              UserJSON = fetch_user_info(),
              Res = save_if_new(UserJSON),
              % TODO Return access_token and UserInfo
  
              N = proplists:get_value(<<"displayName">>, Person),
              Name = binary_to_list(N),
              proplists:get_value(<<"url">>, Person),
              JSON = "{\"token\": \"" ++ Name ++ "\"}",
              {true, wrq:set_resp_body(JSON, ReqData), State};
          _ ->
              {{halt,403}, wrq:set_resp_body("Unsupported field(s) on the auth request", ReqData), State}
      end.
  
  
  fetch_user_info() ->
      {ok, Person} = plus_srv:call_method("plus.people.get", [{"userId", "me"}], []).
      % TODO Transform Person to JSON
  
  
  save_if_new(UserJSON) ->
      % UserName = UserJSON.username
      case check_and_sanitize(UserName) of
          true ->
              FieldsAdded  = add_server_side_fields(UserJSON),
              FieldsAdded2 = add_openid_fields(FieldsAdded),
              case erlastic_search:index_doc_with_id(?INDEX, "user", UserName, FieldsAdded2) of
                  {error, {Code, Body}} ->
                      ErrorString = api_help:generate_error(Body, Code),
                      {error, ErrorString};
                  {ok, List} ->
                      {ok, lib_json:encode(List)}
              end
      end.
  
  
  check_and_sanitize(UserName) ->
      Lambda = fun(X) -> not lists:member(X, "abcdefghijklmnopqrstuvwxyz_-0123456789") end,
      case lists:filter(Lambda, UserName) of
          "" ->
              case erlastic_search:get_doc(?INDEX, "user", UserName) of
                  {error, {404, _}} ->
                      true;
                  {error, {Code, Body}} ->
                      {Code, Body};
                  {ok,JsonStruct} ->
                      false
              end;
          _ ->
              invalidcharacters
      end.
  
  
  %% @doc
  
negative.code:                             end;
                          Id ->
                              %% Get specific user
                              % case erlastic_search:get_doc(?INDEX, "user", string:to_lower(Id), [{<<"fields">>, <<"password,_source">>}]) of
                              %     {error, {Code1, Body1}} ->
                              %         ErrorString1 = api_help:generate_error(Body1, Code1),
                              %         {{halt, Code1}, wrq:set_resp_body(ErrorString1, ReqData), State};
                              %     {ok,JsonStruct} ->
                              %         FinalJson = api_help:get_and_add_password(JsonStruct),
                              %         {FinalJson, ReqData, State}
                              % end
                              case get_user_by_name(Id) of
                                  {error, Msg} -> {{halt, 404}, wrq:set_resp_body(Msg, ReqData), State};
                                  {ok, JSON}   -> {true, wrq:set_resp_body(JSON, ReqData), State}
                              end
                      end
              end
  