ionic-coreo
Version:
Ionic2 module for integration with Coreo
192 lines • 6.64 kB
JavaScript
;
var core_1 = require('@angular/core');
var ionic_native_1 = require('ionic-native');
var storage_1 = require('@ionic/storage');
var ionic_angular_1 = require('ionic-angular');
var client_1 = require('./client');
var user_1 = require('./user');
var token_context_1 = require('./token-context');
var promise_1 = require('./promise');
var AUTH_TYPE_KEY = 'coreo.authmodule';
var BasicAuth = (function () {
function BasicAuth(client, platform) {
this.client = client;
this.platform = platform;
}
BasicAuth.prototype.login = function (credentials) {
return this.client.post('/auth/login', credentials, { authentication: false })
.toPromise()
.then(function (data) {
return data;
});
};
BasicAuth.prototype.logout = function () {
return Promise.resolve();
};
return BasicAuth;
}());
exports.BasicAuth = BasicAuth;
/**
* Google OAuth
*/
var GoogleAuth = (function () {
function GoogleAuth(client, platform) {
this.client = client;
this.platform = platform;
}
GoogleAuth.prototype.login = function (config) {
var _this = this;
var deferred = new promise_1.DeferredPromise();
var isAndroid = this.platform.is('android');
if (!ionic_native_1.GooglePlus) {
return deferred.reject(new Error('Ionic native is not installed.'));
}
if (!window || !window.cordova) {
return deferred.reject(new Error('Cordova is missing.'));
}
var loginParams = {};
if (isAndroid && config.webClientId) {
loginParams.webClientId = config.webClientId;
}
ionic_native_1.GooglePlus.login(loginParams).then(function (success) {
var path = isAndroid ? 'google-id-token' : 'google-token';
var body = isAndroid ? { id_token: success.idToken } : { access_token: success.accessToken };
console.log('Using Google Login:', path, body);
// return deferred.resolve();
_this.client.post("/auth/" + path + "?survey_id=" + config.surveyId, body, { authentication: false })
.toPromise()
.then(function (data) {
deferred.resolve(data);
}, deferred.reject);
}, function (err) {
return deferred.reject(err);
});
return deferred.promise;
};
GoogleAuth.prototype.logout = function () {
var deferred = new promise_1.DeferredPromise();
ionic_native_1.GooglePlus.logout().then(deferred.resolve, deferred.reject);
return deferred.promise;
};
return GoogleAuth;
}());
exports.GoogleAuth = GoogleAuth;
/**
* Facebook OAuth
*/
var FacebookAuth = (function () {
function FacebookAuth(client, platform) {
this.client = client;
this.platform = platform;
}
FacebookAuth.prototype.login = function (config) {
var _this = this;
var deferred = new promise_1.DeferredPromise();
ionic_native_1.Facebook.login(['public_profile', 'email']).then(function (result) {
_this.client.post("/auth/facebook-token?survey_id=" + config.surveyId, { access_token: result.authResponse.accessToken }, { authentication: false })
.toPromise()
.then(function (data) {
deferred.resolve(data);
}, function (err) {
ionic_native_1.Facebook.logout();
deferred.reject(err);
});
}, function (err) { return deferred.reject(err); });
return deferred.promise;
};
FacebookAuth.prototype.logout = function () {
return ionic_native_1.Facebook.getLoginStatus().then(function (loginStatus) {
if (loginStatus.status === 'connected') {
return ionic_native_1.Facebook.logout();
}
return undefined;
});
};
return FacebookAuth;
}());
exports.FacebookAuth = FacebookAuth;
/**
* Coreo Auth Service
*/
var CoreoAuth = (function () {
function CoreoAuth(tokenContext, client, platform, user, storage) {
var _this = this;
this.tokenContext = tokenContext;
this.client = client;
this.platform = platform;
this.user = user;
this.storage = storage;
this.modules = {
basic: new BasicAuth(client, platform),
google: new GoogleAuth(client, platform),
facebook: new FacebookAuth(client, platform)
};
// Kick off the token context to load it up
tokenContext.load();
user.load();
// Load up our context
storage.get(AUTH_TYPE_KEY).then(function (res) {
if (_this.modules.hasOwnProperty(res)) {
_this.context = _this.modules[res];
}
else {
_this.context = _this.modules.basic;
}
});
}
/**
* Login method
*/
CoreoAuth.prototype.login = function (method, params) {
var _this = this;
this.context = this.modules[method];
return this.context.login(params).then(function (token) {
_this.tokenContext.set(token);
_this.storage.set(AUTH_TYPE_KEY, method);
return _this.profile().then(function (userData) {
_this.user.setProfile(userData);
_this.user.save();
return token;
});
});
};
/**
* Logout Method
*/
CoreoAuth.prototype.logout = function () {
var _this = this;
return this.context.logout().then(function () {
_this.reset();
}, function (err) {
console.error('Error logging out:', err);
_this.reset();
});
};
CoreoAuth.prototype.reset = function () {
this.user.clear();
this.tokenContext.clear();
this.storage.remove(AUTH_TYPE_KEY);
};
/**
* Get Profile
*/
CoreoAuth.prototype.profile = function () {
return this.client.request('/profile').toPromise();
};
CoreoAuth.decorators = [
{ type: core_1.Injectable },
];
/** @nocollapse */
CoreoAuth.ctorParameters = [
{ type: token_context_1.CoreoTokenContext, },
{ type: client_1.CoreoClient, },
{ type: ionic_angular_1.Platform, },
{ type: user_1.CoreoUser, },
{ type: storage_1.Storage, },
];
return CoreoAuth;
}());
exports.CoreoAuth = CoreoAuth;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = CoreoAuth;
//# sourceMappingURL=auth.js.map