Skip to content

Commit

Permalink
separate linking feature to va-oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
Saneyan committed Sep 6, 2016
1 parent a49aba8 commit 8911452
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 72 deletions.
109 changes: 37 additions & 72 deletions va-auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

<firebase-auth id="auth"></firebase-auth>

<va-cordova-oauth id="cordovaAuth"></va-cordova-oauth>

</template>

<script>
Expand All @@ -14,8 +12,10 @@
is: 'va-auth',

properties: {

account: {
notify: true
type: Object,
notify: true
}
},

Expand All @@ -24,91 +24,56 @@
},

signOut: function () {
this.$.auth.auth.signOut();
return this.$.auth.auth.signOut();
},

signIn: function (email, password) {
return this.$.auth.signInWithEmailAndPassword(email, password);
},

signUp: function (email, password) {
return this.$.auth.createUserWithEmailAndPassword(email, password);
},

link: function (e) {
var promise;
var provider = e.detail.provider;

if (window.cordova) {
var url = this.resolveUrl('../va-cordova-oauth/va-cordova-oauth.html', null, null, true);
promise = Promise.resolve();

this.importHref(url, function () {
this.$.cordovaAuth.signIn(provider).then(function () {
var credential;

switch (provider) {
case 'google':
credential = firebase.auth.GoogleAuthProvider.credential(arguments[0], null);
break;
case 'facebook':
credential = firebase.auth.FacebookAuthProvider.credential(arguments[0]);
break;
case 'twitter':
credential = firebase.auth.TwitterAuthProvider.credential(arguments[0], arguments[1]);
break;
}

this.$.auth.signInWithCredential(credential).then(function () {
promise.resolve();
});
}.bind(this));
}.bind(this));
} else {
promise = this.$.auth.signInWithPopup(provider);
}
var that = this;

return promise;
return this.$.auth.createUserWithEmailAndPassword(email, password)
.then(function () {
return that.account.sendEmailVerification();
});
},

changeEmail: function (email) {
return this.$.auth.auth.currentUser.updateEmail(email);
resetPassword: function (email) {
return this.$.auth.auth.sendPasswordResetEmail(email);
},

changePassword: function (newPassword) {
return this.$.auth.auth.currentUser.updatePassword(newPassword);
},

reauthenticate: function () {
var promise = new Promise(function (resolve, reject) {
});
return promise;
return this.account.providerData[0].providerId === 'email';
},

hasEmailAndPasswordAccount: function () {
return this._findAccountByProvider('email');
},

hasGoogleAccount: function () {
return this._findAccountByProvider('google.com');
},

hasFacebookAccount: function () {
return this._findAccountByProvider('facebook.com');
changeEmail: function (email, password) {
var that = this;

if (email !== this.account.email) {
return [
function () { return that.account.updateEmail(email); },
function () { return that.account.sendEmailVerification(); }
].reduce(function (p, next) {
return p.then(next);
}, this.account.reauthenticate(this._emailCredential(password)));
}

return Promise.resolve();
},

hasTwitterAccount: function () {
return this._findAccountByProvider('twitter.com');
changePassword: function (newPassword, oldPassword) {
var that = this;

if (newPassword !== oldPassword) {
return this.account.reauthenticate(this._emailCredential(oldPassword))
.then(function () {
return that.account.updatePassword(newPassword);
});
}

return Promise.resolve();
},

_findAccountByProvider: function (provider) {
for (var i in this.account.providerData) {
if (this.account.providerData[i].providerId === provider) {
return true;
}
}
return false;
_emailCredential: function (password) {
return firebase.auth.EmailAuthProvider.credential(this.account.email, password);
},

_onAuthStateChanged: function (user) {
Expand Down
83 changes: 83 additions & 0 deletions va-oauth.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<dom-module id="va-oauth">

<template>

<va-cordova-oauth id="cordovaAuth"></va-cordova-oauth>

</template>

<script>
Polymer({

is: 'va-oauth',

properties: {

account: {
type: Object
}
},

link: function (provider) {
var that = this;
var promise;

if (window.cordova) {
var url = this.resolveUrl('../va-cordova-oauth/va-cordova-oauth.html', null, null, true);
promise = Promise.resolve();

this.importHref(url, function () {
that.$.cordovaAuth.signIn(provider).then(function () {
var credential;

switch (provider) {
case 'google':
credential = firebase.auth.GoogleAuthProvider.credential(arguments[0], null);
break;
case 'facebook':
credential = firebase.auth.FacebookAuthProvider.credential(arguments[0]);
break;
case 'twitter':
credential = firebase.auth.TwitterAuthProvider.credential(arguments[0], arguments[1]);
break;
}

that.account.link(credential).then(function () {
promise.resolve();
});
});
});
} else {
promise = this.$.auth.signInWithPopup(provider).then(function (result) {
that.account.link(result.credential);
});
}

return promise;
},

hasGoogleAccount: function () {
return this._findAccountByProvider('google.com');
},

hasFacebookAccount: function () {
return this._findAccountByProvider('facebook.com');
},

hasTwitterAccount: function () {
return this._findAccountByProvider('twitter.com');
},

_findAccountByProvider: function (provider) {
for (var i in this.account.providerData) {
if (this.account.providerData[i].providerId === provider) {
return true;
}
}
return false;
}

});
</script>

</dom-module>

0 comments on commit 8911452

Please sign in to comment.