Update current user fields through JavaScript?


(Oz Ramos) #1

Hi! So I’m starting a “How To” type community and wanted to give users the ability to create forms. For example, typing:
[] Checkbox () Radio __ Text Field

would autoconvert to their respective HTML fields. The plan is to store these in a single custom user field with key’s represeting the topic ID. The idea is that people would see the same exact post except with their own data filled in.

Applications could be a Madlibs game, ingredients list, todo’s…etc.

Anyways the data would look something like:

{
    postID : {
        formID : {
            fieldID : Value
        }
    }
}

The question is, how I can update the current users custom field? I see I can update fields in the API Docs but I’m not sure how I would do that from the front end (AJAX, assuming each user loads a copy of my script). How does the server authenticate you’re you and prevent me from simply opening up the console and changing out the username?

Discourse.User.current() returns information on the current user, so I imagine I would do something like:

$.ajax({
    url : 'mysite.com/users/username.json',
    data : {
        myField : value
    },
    success : showUpdateNotification
})

I guess my real question is, what extra data needs to be passed in and how does the client find that data?

Thanks!


(Oz Ramos) #2

I haven’t been able to make much progress on this, can anyone point me in the right direction?


(Jay Pfaffman) #3

You have seen the built in poll function, right? Maybe you need something more flexible. You might have a look at that code.