-
Notifications
You must be signed in to change notification settings - Fork 0
/
paperfire-anon.html
105 lines (94 loc) · 2.54 KB
/
paperfire-anon.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../polymerfire/firebase-auth.html">
<!--
`paperfire-anon`
an element to help manage anonymous authentication
@demo demo/index.html
-->
<dom-module id="paperfire-anon">
<template>
<style>
:host {
display: hidden;
}
</style>
<firebase-auth id="auth" user="{{_user}}"></firebase-auth>
</template>
<script>
Polymer({
is: 'paperfire-anon',
properties: {
// true if user is authenticated
authed: {
type: Boolean,
value: false,
notify: true,
reflectToAttribute: true
},
//logs to console
debug: Boolean,
// If true this will automatically check user and if null will login in anonymously.
login: Boolean,
// true if users logged in anonymously
isAnonymous: {
type: Boolean,
value: false,
notify: true,
reflectToAttribute: true
},
// The current user's uid
uid: {
type: String,
value: null,
notify: true,
reflectToAttribute: true
},
_user: {
type: Object,
observer: '_onSync'
}
},
ready: function () {
console.log('PAPERFIRE_ANON', 'READY', this.debug);
var vm = this;
var fb = this.$.auth.__data__.app;
if (this.login) {
fb.auth().onAuthStateChanged(function (user) {
if (vm.debug)
vm._debug('auth_state_changed', user);
if (user) {
console.log('ANON_USER', user);
vm.uid = user.uid;
vm.authed = true;
vm.isAnonymous = user.isAnonymous;
} else {
console.log('NO USER');
vm.authed = false;
vm.uid = null;
vm.isAnonymous = null;
firebase.auth().signInAnonymously().catch(function (error) {
console.error('ANON_SIGNIN:', error);
});
}
});
}
},
_onSync: function (user) {
console.log('_onSync', user);
if (user) {
this.uid = user.uid;
console.log('paperfire-anon', user);
if (user.isAnonymous) {
console.log('anonymous');
this.isAnonymous = true;
} else {
false;
}
}
},
_debug: function (message, data) {
console.log(message, data);
}
});
</script>
</dom-module>