feathers-authentication-management
Version:
Adds sign up verification, forgotten password reset, and other capabilities to local feathers-authentication
126 lines (125 loc) • 5.04 kB
JavaScript
;
// Wrapper for client interface to feathers-authenticate-management
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const options_1 = require("./options");
const defaultOptions = {
path: options_1.defaultPath
};
function makeClient(app, _options) {
const options = Object.assign({}, defaultOptions, _options);
const { path } = options;
const authManagement = app.service(path);
const client = {
checkUnique: (identifyUser, ownId, ifErrMsg) => __awaiter(this, void 0, void 0, function* () {
yield authManagement.create({
action: 'checkUnique',
value: identifyUser,
ownId,
meta: { noErrMsg: ifErrMsg }
});
}),
resendVerifySignup: (identifyUser, notifierOptions) => __awaiter(this, void 0, void 0, function* () {
yield authManagement.create({
action: 'resendVerifySignup',
value: identifyUser,
notifierOptions
});
}),
verifySignupLong: (verifyToken) => __awaiter(this, void 0, void 0, function* () {
yield authManagement.create({
action: 'verifySignupLong',
value: verifyToken
});
}),
verifySignupShort: (verifyShortToken, identifyUser) => __awaiter(this, void 0, void 0, function* () {
yield authManagement.create({
action: 'verifySignupShort',
value: {
token: verifyShortToken,
user: identifyUser
}
});
}),
sendResetPwd: (identifyUser, notifierOptions) => __awaiter(this, void 0, void 0, function* () {
yield authManagement.create({
action: 'sendResetPwd',
value: identifyUser,
notifierOptions
});
}),
resetPwdLong: (resetToken, password) => __awaiter(this, void 0, void 0, function* () {
yield authManagement.create({
action: 'resetPwdLong',
value: {
password,
token: resetToken
}
});
}),
resetPwdShort: (resetShortToken, identifyUser, password) => __awaiter(this, void 0, void 0, function* () {
yield authManagement.create({
action: 'resetPwdShort',
value: {
password,
token: resetShortToken,
user: identifyUser
}
});
}),
passwordChange: (oldPassword, password, identifyUser) => __awaiter(this, void 0, void 0, function* () {
yield authManagement.create({
action: 'passwordChange',
value: {
oldPassword,
password,
user: identifyUser
}
});
}),
identityChange: (password, changesIdentifyUser, identifyUser) => __awaiter(this, void 0, void 0, function* () {
yield authManagement.create({
action: 'identityChange',
value: {
user: identifyUser,
password,
changes: changesIdentifyUser
}
});
}),
authenticate: (email, password, cb) => __awaiter(this, void 0, void 0, function* () {
let cbCalled = false;
const authResult = yield app.authenticate({ type: 'local', email, password });
const user = authResult.data;
try {
if (!user || !user.isVerified) {
yield app.logout();
return cb(new Error(user ? 'User\'s email is not verified.' : 'No user returned.'));
}
if (cb) {
cbCalled = true;
return cb(null, user);
}
return user;
}
catch (err) {
if (!cbCalled && cb) {
cb(err);
}
}
})
};
return client;
}
exports.default = makeClient;
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = makeClient;
}