A User Collection can be accessed in the same ways as a Collection, both with dpd.js and HTTP. It also adds new methods to the API for authentication.
The examples below use a User Collection called /users with the following schema:
idusernamepassworddisplayNameLog in a user with their username and password. If successful, the browser will save a secure cookie for their session. This request responds with the session details:
{
"id": "s0446b993caaad577a..." //Session id
"path": "/users" // The path of the User Collection - useful if you have different types of users.
"uid": "ec54ad870eaca95f" //The id of the user
}
If the username or password is incorrect, Deployd will respond with a generic error:
{
"status": 401,
"message: "bad credentials"
}
For security reasons, users should not be informed which of their credentials (username, password, or both) were incorrect.
To authenticate a user, use the .login(credentials, fn) function, including the username and password properties in the request body.
dpd.users.login({
username: "johnsmith",
password: "password"
}, function(result, error) {
// Do something
});
You can also use the .exec('login', credentials, fn) function. This is useful if you have accessed the collection by using dpd() as a function and the login() function is unavailable.
dpd('users').exec('login', {
username: "johnsmith",
password: "password"
}, function(result, error) {
// Do something
});
To authenticate a user, send a POST request to /login with username and password properties in the request body.
POST /users/login
{
"username": "johnsmith",
"password": "password"
}
Logging out will remove the session cookie on the browser and destroy the current session. It does not return a result.
To log out a user, use the .logout(fn) function.
dpd.users.logout(function(result, error) {
// Do something
});
result will always be null.
You can also use the .exec('logout', fn) function. This is useful if you have accessed the collection by using dpd() as a function and the logout() function is unavailable.
dpd('users').exec('logout', function(result, error) {
// Do something
});
To log out a user, send a POST request to /logout. Include the sid cookie to identify your session.
POST /users/logout
Cookie: sid=6009c5b070d834a2d336224a93...
The response body will always be empty unless there was an error.
This will return the current user.
{
"id": "2975ff2778493818",
"username": "johnsmith",
"displayName": "John Smith"
}
If there is no current user, it will not return a value.
To get the current user, use the .me(fn) function.
dpd.users.me(function(result, error) {
// Do something
});
If there is no current user, result will be null.
You can also use the .get('me', fn) function. This is useful if you have accessed the collection by using dpd() as a function and the me() function is unavailable.
dpd('users').get('me', function(result, error) {
// Do something
});
To get the current user, send a GET request to /me. Include the sid cookie to identify your session.
GET /users/me
Cookie: sid=6009c5b070d834a2d336224a93...
If there is no current user, the response body will be blank:
204 No Content
Let us know if you have any ideas to improve our docs. Open an issue on github, send us an email, or tweet us.
This entire site, including documentation written in markdown is available on github. Pull requests are appreciated!