UNPKG

5.04 kBJavaScriptView Raw
1"use strict";
2// Wrapper for client interface to feathers-authenticate-management
3var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5 return new (P || (P = Promise))(function (resolve, reject) {
6 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9 step((generator = generator.apply(thisArg, _arguments || [])).next());
10 });
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13const options_1 = require("./options");
14const defaultOptions = {
15 path: options_1.defaultPath
16};
17function makeClient(app, _options) {
18 const options = Object.assign({}, defaultOptions, _options);
19 const { path } = options;
20 const authManagement = app.service(path);
21 const client = {
22 checkUnique: (identifyUser, ownId, ifErrMsg) => __awaiter(this, void 0, void 0, function* () {
23 yield authManagement.create({
24 action: 'checkUnique',
25 value: identifyUser,
26 ownId,
27 meta: { noErrMsg: ifErrMsg }
28 });
29 }),
30 resendVerifySignup: (identifyUser, notifierOptions) => __awaiter(this, void 0, void 0, function* () {
31 yield authManagement.create({
32 action: 'resendVerifySignup',
33 value: identifyUser,
34 notifierOptions
35 });
36 }),
37 verifySignupLong: (verifyToken) => __awaiter(this, void 0, void 0, function* () {
38 yield authManagement.create({
39 action: 'verifySignupLong',
40 value: verifyToken
41 });
42 }),
43 verifySignupShort: (verifyShortToken, identifyUser) => __awaiter(this, void 0, void 0, function* () {
44 yield authManagement.create({
45 action: 'verifySignupShort',
46 value: {
47 token: verifyShortToken,
48 user: identifyUser
49 }
50 });
51 }),
52 sendResetPwd: (identifyUser, notifierOptions) => __awaiter(this, void 0, void 0, function* () {
53 yield authManagement.create({
54 action: 'sendResetPwd',
55 value: identifyUser,
56 notifierOptions
57 });
58 }),
59 resetPwdLong: (resetToken, password) => __awaiter(this, void 0, void 0, function* () {
60 yield authManagement.create({
61 action: 'resetPwdLong',
62 value: {
63 password,
64 token: resetToken
65 }
66 });
67 }),
68 resetPwdShort: (resetShortToken, identifyUser, password) => __awaiter(this, void 0, void 0, function* () {
69 yield authManagement.create({
70 action: 'resetPwdShort',
71 value: {
72 password,
73 token: resetShortToken,
74 user: identifyUser
75 }
76 });
77 }),
78 passwordChange: (oldPassword, password, identifyUser) => __awaiter(this, void 0, void 0, function* () {
79 yield authManagement.create({
80 action: 'passwordChange',
81 value: {
82 oldPassword,
83 password,
84 user: identifyUser
85 }
86 });
87 }),
88 identityChange: (password, changesIdentifyUser, identifyUser) => __awaiter(this, void 0, void 0, function* () {
89 yield authManagement.create({
90 action: 'identityChange',
91 value: {
92 user: identifyUser,
93 password,
94 changes: changesIdentifyUser
95 }
96 });
97 }),
98 authenticate: (email, password, cb) => __awaiter(this, void 0, void 0, function* () {
99 let cbCalled = false;
100 const authResult = yield app.authenticate({ type: 'local', email, password });
101 const user = authResult.data;
102 try {
103 if (!user || !user.isVerified) {
104 yield app.logout();
105 return cb(new Error(user ? 'User\'s email is not verified.' : 'No user returned.'));
106 }
107 if (cb) {
108 cbCalled = true;
109 return cb(null, user);
110 }
111 return user;
112 }
113 catch (err) {
114 if (!cbCalled && cb) {
115 cb(err);
116 }
117 }
118 })
119 };
120 return client;
121}
122exports.default = makeClient;
123if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
124 module.exports = makeClient;
125}