@akveo/nga-auth
Version:
@akveo/nga-auth
199 lines • 7.31 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
/**
* @license
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import { Injectable, Optional, Inject } from '@angular/core';
import 'rxjs/add/operator/do';
import { NgaTokenService } from './token.service';
import { ngaAuthProvidersToken } from '../auth.options';
var NgaAuthResult = (function () {
// TODO pass arguments in options object
function NgaAuthResult(success, response, redirect, errors, messages, token) {
this.success = success;
this.response = response;
this.redirect = redirect;
this.errors = [];
this.messages = [];
this.errors = this.errors.concat([errors]);
if (errors instanceof Array) {
this.errors = errors;
}
this.messages = this.messages.concat([messages]);
if (messages instanceof Array) {
this.messages = messages;
}
this.token = token;
}
NgaAuthResult.prototype.getResponse = function () {
return this.response;
};
NgaAuthResult.prototype.getTokenValue = function () {
return this.token;
};
NgaAuthResult.prototype.getRedirect = function () {
return this.redirect;
};
NgaAuthResult.prototype.getErrors = function () {
return this.errors.filter(function (val) { return !!val; });
};
NgaAuthResult.prototype.getMessages = function () {
return this.messages.filter(function (val) { return !!val; });
};
NgaAuthResult.prototype.isSuccess = function () {
return this.success;
};
NgaAuthResult.prototype.isFailure = function () {
return !this.success;
};
return NgaAuthResult;
}());
export { NgaAuthResult };
var NgaAuthService = (function () {
function NgaAuthService(tokenService, providers) {
if (providers === void 0) { providers = {}; }
this.tokenService = tokenService;
this.providers = providers;
}
/**
* Retrieves current authenticated token stored
* @returns {Observable<any>}
*/
NgaAuthService.prototype.getToken = function () {
return this.tokenService.get();
};
/**
* Returns true if auth token is presented in the token storage
* @returns {Observable<any>}
*/
NgaAuthService.prototype.isAuthenticated = function () {
return this.getToken().map(function (token) { return !!token; });
};
/**
* Returns tokens stream
* @returns {Observable<any>}
*/
NgaAuthService.prototype.onTokenChange = function () {
return this.tokenService.tokenChange();
};
/**
* Returns authentication status stream
* @returns {Observable<any>}
*/
NgaAuthService.prototype.onAuthenticationChange = function () {
return this.onTokenChange().map(function (token) { return !!token; });
};
/**
* Authenticates with the selected provider
* Stores received token in the token storage
*
* Example:
* authenticate('email', {email: 'email@example.com', password: 'test'})
*
* @param provider
* @param data
* @returns {Observable<NgaAuthResult>}
*/
NgaAuthService.prototype.authenticate = function (provider, data) {
var _this = this;
return this.getProvider(provider).authenticate(data)
.do(function (result) {
if (result.isSuccess() && result.getTokenValue()) {
_this.tokenService.set(result.getTokenValue()).subscribe(function () {
});
}
});
};
/**
* Registers with the selected provider
* Stores received token in the token storage
*
* Example:
* register('email', {email: 'email@example.com', name: 'Some Name', password: 'test'})
*
* @param provider
* @param data
* @returns {Observable<NgaAuthResult>}
*/
NgaAuthService.prototype.register = function (provider, data) {
var _this = this;
return this.getProvider(provider).register(data)
.do(function (result) {
if (result.isSuccess() && result.getTokenValue()) {
_this.tokenService.set(result.getTokenValue()).subscribe(function () {
});
}
});
};
/**
* Sign outs with the selected provider
* Removes token from the token storage
*
* Example:
* logout('email')
*
* @param provider
* @returns {Observable<NgaAuthResult>}
*/
NgaAuthService.prototype.logout = function (provider) {
var _this = this;
return this.getProvider(provider).logout()
.do(function (result) {
if (result.isSuccess()) {
_this.tokenService.clear().subscribe(function () { });
}
});
};
/**
* Sends forgot password request to the selected provider
*
* Example:
* requestPassword('email', {email: 'email@example.com'})
*
* @param provider
* @param data
* @returns {Observable<NgaAuthResult>}
*/
NgaAuthService.prototype.requestPassword = function (provider, data) {
return this.getProvider(provider).requestPassword(data);
};
/**
* Tries to reset password with the selected provider
*
* Example:
* resetPassword('email', {newPassword: 'test'})
*
* @param provider
* @param data
* @returns {Observable<NgaAuthResult>}
*/
NgaAuthService.prototype.resetPassword = function (provider, data) {
return this.getProvider(provider).resetPassword(data);
};
NgaAuthService.prototype.getProvider = function (provider) {
if (!this.providers[provider]) {
throw new TypeError("Nga auth provider '" + provider + "' is not registered");
}
return this.providers[provider].object;
};
return NgaAuthService;
}());
NgaAuthService = __decorate([
Injectable(),
__param(1, Optional()), __param(1, Inject(ngaAuthProvidersToken)),
__metadata("design:paramtypes", [NgaTokenService, Object])
], NgaAuthService);
export { NgaAuthService };
//# sourceMappingURL=auth.service.js.map