All files / lib/resources session.js

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                                                                                                                                         
/**
 * @name Session
 * @description This module exposes functions
 *              related to the `/sessions` path.
 *
 * @module session
 **/
 
import routes from '../routes'
import request from '../request'
 
/**
 * `POST /sessions`
 * Creates a session from the given payload.
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 * @param {String} email The account's email
 *
 * @param {String} password The account's password
 *
 * @param {String} recaptchaToken The generated reCAPTCHA token
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const create = (opts, email, password, recaptchaToken, token) =>
  request.post(opts, routes.session.base, {
    email,
    password,
    recaptchaToken,
    token,
  })
 
/**
 * `DELETE /sessions/:id`
 *
 * Deletes the session with the given ID
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 * @param {String} id The session's id
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const destroy = (opts, id) =>
  request.delete(opts, routes.session.destroy(id), {})
 
/**
 * `POST /sessions/:id/verify`
 * Verifies a session's password' from the given
 * session_id and possible password.
 *
 * @example
 * client.session.verify({ id: sessionId, password: pwd})
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 * @param {String} payload The payload to be sent
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const verify = (opts, payload) =>
  request.post(opts, routes.session.verify(payload.id), payload)
 
export default {
  create,
  destroy,
  verify,
}