ionic-coreo
Version:
Ionic2 module for integration with Coreo
234 lines • 8.32 kB
JavaScript
"use strict";
var core_1 = require('@angular/core');
var facebook_1 = require('@ionic-native/facebook');
var google_plus_1 = require('@ionic-native/google-plus');
var storage_1 = require('@ionic/storage');
var ionic_angular_1 = require('ionic-angular');
var client_1 = require('./client');
var user_1 = require('./user');
var config_1 = require('./config');
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, config) {
this.client = client;
this.platform = platform;
this.config = config;
}
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();
};
BasicAuth.prototype.signup = function (email, password, displayName, username) {
var data = {
email: email,
password: password,
displayName: displayName,
username: username
};
if (this.config.surveyId) {
data.surveyId = this.config.surveyId;
}
return this.client.post('/auth/signup', data, { authentication: false }).toPromise().then(function (data) {
return data;
});
};
return BasicAuth;
}());
exports.BasicAuth = BasicAuth;
/**
* Google OAuth
*/
var GoogleAuth = (function () {
function GoogleAuth(client, platform, google) {
this.client = client;
this.platform = platform;
this.google = google;
}
GoogleAuth.prototype.login = function (config) {
var _this = this;
var deferred = new promise_1.DeferredPromise();
var isAndroid = this.platform.is('android');
if (!google_plus_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;
}
this.google.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();
this.google.logout().then(deferred.resolve, deferred.reject);
return deferred.promise;
};
return GoogleAuth;
}());
exports.GoogleAuth = GoogleAuth;
/**
* Facebook OAuth
*/
var FacebookAuth = (function () {
function FacebookAuth(client, platform, facebook) {
this.client = client;
this.platform = platform;
this.facebook = facebook;
}
FacebookAuth.prototype.login = function (config) {
var _this = this;
var deferred = new promise_1.DeferredPromise();
this.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) {
_this.facebook.logout();
deferred.reject(err);
});
}, function (err) { return deferred.reject(err); });
return deferred.promise;
};
FacebookAuth.prototype.logout = function () {
var _this = this;
return this.facebook.getLoginStatus().then(function (loginStatus) {
if (loginStatus.status === 'connected') {
return _this.facebook.logout();
}
return undefined;
});
};
return FacebookAuth;
}());
exports.FacebookAuth = FacebookAuth;
/**
* Coreo Auth Service
*/
var CoreoAuth = (function () {
function CoreoAuth(tokenContext, client, platform, user, config, storage, google, facebook) {
var _this = this;
this.tokenContext = tokenContext;
this.client = client;
this.platform = platform;
this.user = user;
this.config = config;
this.storage = storage;
this.google = google;
this.facebook = facebook;
this.modules = {
basic: new BasicAuth(client, platform, config),
google: new GoogleAuth(client, platform, google),
facebook: new FacebookAuth(client, platform, facebook)
};
// Load up our context
storage.ready().then(function () { return 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.ready().then(function () { return _this.storage.set(AUTH_TYPE_KEY, method); });
return _this.profile().then(function (userData) {
_this.user.setProfile(userData);
_this.user.save();
return token;
});
});
};
/**
* Signup Method
*/
CoreoAuth.prototype.signup = function (email, password, displayName, username) {
return this.modules.basic.signup(email, password, displayName, username).then(function (data) {
return data;
});
};
/**
* 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 () {
var _this = this;
this.user.clear();
this.tokenContext.clear();
this.storage.ready().then(function () { return _this.storage.remove(AUTH_TYPE_KEY); });
};
/**
* Get Profile
*/
CoreoAuth.prototype.profile = function () {
return this.client.request('/profile').toPromise();
};
/**
* Update username
*/
CoreoAuth.prototype.updateUsername = function (username) {
var _this = this;
return this.client.request('/profile/username', {
method: 'put',
body: JSON.stringify({ username: username })
}).toPromise().then(function () { return _this.user.setUsername(username); });
};
CoreoAuth.decorators = [
{ type: core_1.Injectable },
];
/** @nocollapse */
CoreoAuth.ctorParameters = function () { return [
{ type: token_context_1.CoreoTokenContext, },
{ type: client_1.CoreoClient, },
{ type: ionic_angular_1.Platform, },
{ type: user_1.CoreoUser, },
{ type: config_1.CoreoConfig, },
{ type: storage_1.Storage, },
{ type: google_plus_1.GooglePlus, },
{ type: facebook_1.Facebook, },
]; };
return CoreoAuth;
}());
exports.CoreoAuth = CoreoAuth;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = CoreoAuth;
//# sourceMappingURL=auth.js.map