ngo-login-client
Version:
Auth and User services for Angular v2 and up. Requires backend REST service.
106 lines • 4.08 kB
JavaScript
import { Injectable, Inject } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { Observable, ReplaySubject } from 'rxjs';
import { cloneDeep } from 'lodash';
import { Broadcaster, Logger } from 'ngo-base';
import { AUTH_API_URL } from '../shared/auth-api';
/**
* Provides user and user list methods to retrieve current or user list details
*
* The UserService should be injected at the root of the application to ensure it is a singleton
* getUser and getAllUsers return observables that can be subscribed to for information
*/
var UserService = /** @class */ (function () {
function UserService(http, logger, broadcaster, apiUrl) {
var _this = this;
this.http = http;
this.logger = logger;
this.headers = new Headers({ 'Content-Type': 'application/json' });
this.userUrl = apiUrl + 'user';
this.usersUrl = apiUrl + 'users';
this.searchUrl = apiUrl + 'search';
this.loggedInUser = Observable.merge(broadcaster.on('loggedin')
.map(function (val) { return 'loggedIn'; }), broadcaster.on('logout')
.map(function (val) { return 'loggedOut'; }), broadcaster.on('authenticationError')
.map(function (val) { return 'authenticationError'; }))
.switchMap(function (val) {
// If it's a login event, then we need to retreive the user's details
if (val === 'loggedIn') {
return _this.http
.get(_this.userUrl, { headers: _this.headers })
.map(function (response) { return cloneDeep(response.json().data); });
}
else {
// Otherwise, we clear the user
return Observable.of({});
}
})
.multicast(function () { return new ReplaySubject(1); });
this.loggedInUser.connect();
}
/**
* Get the User object for a given user id, or null if no user is found
* @param userId the userId to search for
*/
UserService.prototype.getUserByUserId = function (userId) {
return this.http
.get(this.usersUrl + "/" + userId, { headers: this.headers })
.map(function (response) {
return response.json().data;
});
};
/**
* Get the User object for a given username, or null if no user is found
* @param username the username to search for
*/
UserService.prototype.getUserByUsername = function (username) {
return this.filterUsersByUsername(username).map(function (val) {
for (var _i = 0, val_1 = val; _i < val_1.length; _i++) {
var u = val_1[_i];
if (username === u.attributes.username) {
return u;
}
}
return null;
});
};
/**
* Get users by a search string
*/
UserService.prototype.getUsersBySearchString = function (search) {
if (search && search !== "") {
return this.http
.get(this.searchUrl + '/users?q=' + search, { headers: this.headers })
.map(function (response) {
return response.json().data;
});
}
return Observable.of([]);
};
/**
*
* Filter users by username
*
* @returns Observable<User[]>
*/
UserService.prototype.filterUsersByUsername = function (username) {
return this.http
.get(this.usersUrl + "?filter[username]=" + username, { headers: this.headers })
.map(function (response) {
return response.json().data;
});
};
UserService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
UserService.ctorParameters = function () { return [
{ type: Http, },
{ type: Logger, },
{ type: Broadcaster, },
{ type: undefined, decorators: [{ type: Inject, args: [AUTH_API_URL,] },] },
]; };
return UserService;
}());
export { UserService };
//# sourceMappingURL=user.service.js.map