-
Notifications
You must be signed in to change notification settings - Fork 19
/
login-fire-social-behavior.html
127 lines (118 loc) · 3.74 KB
/
login-fire-social-behavior.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!--
@license
Copyright (c) 2016 Convoo All Rights Reserved.
Use of this source code is governed by a MIT license that can be found in the
LICENSE file or at https://github.com/convoo/login-fire/blob/master/LICENSE.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="./login-fire-common-behavior.html">
<script>
(function() {
'use strict';
window.Convoo = window.Convoo || {};
/**
* A behavior that allows an element to use Firebase Auth to sign-in and sign-up
* a user with federated identity providers such as Google, Facebook, Twitter and GitHub.
* If your element must be able to use email address and password authentication
* only, you must implement the `LoginFireFormBehavior` instead.
*
* @polymerBehavior Convoo.LoginFireSocialBehavior
*/
Convoo.LoginFireSocialBehaviorImpl = {
properties: {
/**
* The scopes that you want to access.
*
* Available scopes:
* Facebook -> https://developers.facebook.com/docs/facebook-login/permissions
* GitHub -> https://developer.github.com/v3/oauth/#scopes
* Google -> https://developers.google.com/identity/protocols/googlescopes
*/
scopes: {
type: String,
value: ''
},
/**
* OAuth provider instance created by Firebase. It uses the defined
* `scopes`.
*
* @type {Object}
*/
_oauthProvider: {
type: Object,
computed: '_computeOAuthProvider(config.provider, scopes)'
}
},
/**
* Starts the sign-in process using a federated identity provider.
*/
signIn: function() {
// Skip processing of this method when we already have started a sign or signout process
if (this.inProgress) {
if (this.debug) {
console.log('Ignored signIn when already in progress');
}
return;
}
if (!this.signedIn && !this.noSignIn) {
if (this.debug) {
console.log('Signing in...');
}
this._setInProgress(true);
this.__injectFirebaseAuth()
.then(auth => {
if (!!auth.user) {
return auth.user;
}
if (this.provider === 'anonymous') {
return auth.signInAnonymously();
}
return this.redirect ?
auth.signInWithRedirect(this._oauthProvider) :
auth.signInWithPopup(this._oauthProvider);
})
.then(() => this._setInProgress(false))
.catch(resp => this._fireError(resp));
}
},
/**
* Signs in or signs out a user. When a user is currently logged in the
* `signOut` method is called. When there is no user logged in, the `signIn`
* method is called.
*/
signInOrOut: function() {
if (typeof this.user === 'undefined' || this.user == null) {
this.signIn();
} else {
this.signOut();
}
},
/**
* Creates a new federated identity provider instance according to the given
* ID.
*
* @param {String} provider the provider that will be used for authentication
* @param {String} scopes to be set
* @return {Object} federated identity provider instance according
* to the given ID.
*/
_computeOAuthProvider: function(provider, scopes) {
var oauthProvider;
if (provider && provider.isSocialNetwork) {
oauthProvider = new firebase.auth[provider.name + 'AuthProvider']();
if (provider.scopes) {
oauthProvider.addScope(provider.scopes);
}
}
return oauthProvider;
}
};
/**
* @polymerBehavior
*/
Convoo.LoginFireSocialBehavior = [
Convoo.LoginFireCommonBehavior,
Convoo.LoginFireSocialBehaviorImpl
];
})();
</script>