slouch/examples/authentication.js
Dominik Nussbaumer c79b6eaf89 Remove Todo since issue #10 is closed (#117)
The issue that the authentication is not working is closed.
2019-10-29 23:31:29 -07:00

57 lines
1.3 KiB
JavaScript

'use strict';
var Slouch = require('../');
var slouch = new Slouch('http://localhost:5984');
var adminUsername = 'admin';
var adminPassword = 'admin';
var nonAdminUsername = 'ada';
var nonAdminPassword = 'secrect';
// The role and dbName will be the same name as the nonAdminUsername
var role = nonAdminUsername;
var dbName = nonAdminUsername;
// Login as the admin so that we can create the user and DB
slouch.user.logIn(adminUsername, adminPassword).then(function () {
// Create a new user
slouch.user.create(nonAdminUsername, nonAdminPassword, [ role ]);
}).then(function () {
// Create the database
return slouch.db.create(dbName);
}).then(function () {
// Set the security so that only the nonAdminUsername can edit this DB
return slouch.security.onlyRoleCanView(dbName, role);
}).then(function () {
// Log out as admin
return slouch.user.logOut();
}).then(function () {
// Log in as nonAdminUsername
return slouch.user.logIn(nonAdminUsername, nonAdminPassword);
}).then(function () {
// Create a doc
return slouch.doc.create(dbName, { _id: '1', foo: 'bar' });
}).then(function (doc) {
// Update the doc
return slouch.doc.upsert(dbName, { _id: '1', foo: 'yar' });
}).then(function () {
// Log out as nonAdminUsername
return slouch.user.logOut();
});