Comp-490-SeniorProject / site

MIT License
0 stars 1 forks source link

Add basic API for user registration #45

Closed MarkKoz closed 2 years ago

MarkKoz commented 2 years ago

Resolves #42

I described the endpoints in the following comments: https://github.com/Comp-490-SeniorProject/site/pull/43#issuecomment-1041014620 https://github.com/Comp-490-SeniorProject/site/pull/43#issuecomment-1045227851

This is really basic for now, but I figured we should merge more frequently rather than holding things up trying to implement the more advanced features like password recovery and email verification.

I did manage to implement support for logging in with an e-mail. This required a custom field that converts empty strings to NULL values when they get stored in the database. This is needed since emails need to be unique if they're used to log in, but I also wanted to keep emails optional. So I also had to write a custom migration, which is reversible. I tested the migration and reversing it; it does indeed convert to null and empty strings as needed.

Try out the endpoints if you can while reviewing this. Following from the code in the comments linked above, here is some sample code for testing adding and querying a device:

// add a device
(async () => {
    let response = await fetch(
        "/api/devices/",
        {
            method: "POST",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'X-CSRFToken': getCookie('csrftoken'),
            },
            mode: 'same-origin',
            body: JSON.stringify({
                name: "device_1",
                description: "My first device!",
                // Currently, this can be any user ID.
                // This is fixed by PR #46
                owner: 1,
            }),
        }
    );
    let content = await response.json();
    console.log(content);
})();

// query current user's devices
// For some reason, this doesn't need the CSRF token.
// I'll investigate why later.
(async () => {
    let response = await fetch(
        "/api/devices/",
        {
            method: "GET",
            headers: {
                'Accept': 'application/json',
            },
        }
    );
    let content = await response.json();
    console.log(content);
})();