Current user

From NSB App Studio
Revision as of 23:23, 26 May 2017 by Sarah (talk | contribs) (Instructions for creating and working with the current user)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Here are some functions for managing the users of your app. You'll need to initialize your app, after which you can get the information for the logged in user, modify it or delete it.

Get User information

You can get information on the currently logged on user with the $volt.user.get() function.

The syntax of the function is:

$volt.use.get(callback)

  • callback - function(error, data), required. The function in your app to call when the request to Volt is complete (or fails).
  var user;

  butGetUser.onclick = function() {
    $volt.user.get(getUserCallback);
  }

  function getUserCallback(error, data) {
    if (error) {
      if (!data) {
        data = { message: 'Network Error' };
      }
      alert(data.message);
      user = null;
    } else {
      user = data;
    }
  }
  Dim user

  Function butGetUser_onclick()
    $volt.user.get(getUserCallback)
  End Function

  Function getUserCallback(error, data)
    If error Then
      If (!data) Then data = { message: "Network Error" }
      alert(data.message)
      user = null
    Else
      user = data
    End If
  End Function

What gets returned? Here is what is in the initial release of Volt:

  {
    "id": "7PQipR",
    "email": "support@nsbasic.com",
    "confirmed": true,
    "first_name": "John",
    "last_name": "Smith"
  }

Modify User information

You can change information for the currently logged on user with the $volt.user.update() function.

The syntax of the function is:

$volt.user.get(data, callback)

  • data - object, required. An object with the user details to update.
  • callback - function(error, data), required. The function in your app to call when the request to Volt is complete (or fails).

The data object should be of the following form:

  {
    "email": "new@email.com",
    "password": {
      "old": "myOldPassword",
      "new": "myNewPassword",
      "confirmation": "myNewPassword"
    },
    "first_name": "Alan",
    "last_name": "Turing"
  }

You can pass email, password, first_name, last_name or any combination thereof. Here's an example of changing the logged in user's email:

  butUpdateUser.onclick = function() {
    $volt.user.update({ email: inpNewEmail.value }, updateUserCallback);
  }

  function updateUserCallback(error, data) {
    if (error) {
      if (!data) {
        data = { message: 'Network Error' };
      }
      alert(data.message);
    } else {
      alert('User updated.');
    }
  }
  Function butUpdateUser_onclick()
    $volt.user.update({ email: inpNewEmail.value }, updateUserCallback)
  End Function

  Function getUserCallback(error, data)
    If error Then
      If (!data) Then data = { message: "Network Error" }
      alert(data.message)
    Else
      alert("User updated.")
    End If
  End Function

Delete a User

You can provide your users with a way to close/delete their accounts using the $volt.user.delete() function.

The syntax of the function is:

$volt.user.delete(callback)

  • callback - function(error, data), required. The function in your app to call when the request to Volt is complete (or fails).
  function deleteUser() {
    $volt.user.delete(deleteUserCallback);
  }

  function deleteUserCallback(error, data) {
    if (error) {
      if (!data) {
        data = { message: 'Network Error' };
      }
      alert(data.message);
    } else {
      alert('User deleted.');
    }
  }
  Function deleteUser()
    $volt.user.delete(deleteUserCallback)
  End Function

  Function deleteUserCallback(error, data)
    If error Then
      If (!data) Then data = { message: "Network Error" }
      MsgBox data.message
    Else
      MsgBox "User Deleted"
    End If
  End Function

Reference

  • JavaScript API
 * https://docs.voltcloud.io/client/$volt.user.html#.get
 * https://docs.voltcloud.io/client/$volt.user.html#.update
 * https://docs.voltcloud.io/client/$volt.user.html#.delete
  • REST API
 * https://docs.voltcloud.io/api/#users-current-user-get
 * https://docs.voltcloud.io/api/#users-current-user-put
 * https://docs.voltcloud.io/api/#users-current-user-delete