Arpith Siromoney 💬

You say username, I say Username

When a user signs up for the app I’m building, one of the things to be checked is that the username is available. This is done by creating a file (on S3) every time a user signs up, and checking if this file exists before proceeding with the signup process.

checkUsername(username) {
  var username = username.toLowerCase();
  this.setState({username: username});
  var url = URL + '/' + username;
    return fetch(url).then((res) => {
    if (res.status !== 404) {
      this.setState({
        isUsernameAvailable: false,
        heading: 'Try another username',
        subheading: 'This one\'s taken!'
      });
    } else {
      this.setState({
        isUsernameAvailable: true,
        heading: 'Almost Done!',
        subheading: 'What\'s your email address?'
      });
    }
  });
}

Note that we’re checking if the lower case version of the username is available, this is to prevent mixups like one user signing up as Username and another as username. This behaviour is replicated on the server, the API converts the username that has been supplied to lowercase before (for example) creating a post — in case the app has stored the username as Username.

function create(username, token, post) {
  var username = username.toLowerCase();
  delete post.token;
  return auth(username, token).then(function() {
    var bucket = 'constellational-store';
    post.created = new Date().toISOString();
    post.updated = post.created;
    if (!post.id) post.id = randomString();
    post.key = post.created + post.id;
    return putJSON(bucket, username + '/' + post.key, post).then(function(data) {
     post.url = post.key + '?VersionId=' + data.VersionId;
      return post;
    });
  });
}

This fixes issue #4.