-
Notifications
You must be signed in to change notification settings - Fork 0
/
gun-p2p-auth.html
154 lines (126 loc) · 4.22 KB
/
gun-p2p-auth.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<link rel="import" href="../polymer/polymer.html">
<script src="../crypto-js/crypto-js.js" type="text/javascript"></script>
<script src="../kjur-jsrsasign/jsrsasign-latest-all-min.js" type="text/javascript"></script>
<script src="js/gun-p2p-auth.js" type="text/javascript"></script>
<!--
`gun-p2p-auth`
Authorize with GunDB
Just put `gun-p2p-auth` in your page/element
<gun-p2p-auth id="auth"></gun-p2p-auth>
and call its 'login' or 'register' functions
@demo demo/index.html
-->
<dom-module id="gun-p2p-auth">
<template>
<style>
:host {
display: block;
}
</style>
</template>
<script>
Polymer( {
is: 'gun-p2p-auth',
/*
Don't forget that 'camelCase' properties need to be set
as 'camel-case' on the element
<gun-p2p-auth gun-var="">
*/
properties: {
/**
* Let the user provide its own variable if he wishes
* @type {String}
* @default ['gun']
*/
gunVar: {
type: String,
value: 'gun'
},
gun: {
type: Object,
value: null
},
/**
* [useGunChain description]
* @type {Boolean}
*/
useGunChain: {
type: Boolean,
value: false
},
/**
* [noPut description]
* @type {Boolean}
*/
noPut: {
type: Boolean,
value: false
},
state: {
type: Object,
value: {
status: ''
}
}
},
observers: [
'_dataChanged(username)',
'_dataChanged(password)'
],
_dataChanged: function( changes ) {
if( this.state.status !== 'loggedin' && this.username && this.password ) {
this.login( this.username, this.password );
}
},
login: function( userName, password ) {
var gun = this.gun || window[ this.gunVar ];
var cb = ( this.loginCb ).bind( this );
var opt = {
useGunChain: this.useGunChain,
noPut: this.noPut
};
this.set( 'state.status', 'checking' );
gun.p2pAuthLogin( userName, password, opt, cb );
},
/**
* User can override this callback from the outside<br>
* ```
* gunp2pauth.loginCB = function(res) {
* // do something with the res
* }
* ```
* @param {Object} res [result from gun-p2p-auth.js]
*
*/
loginCb: function( res ) {
// console.log( 'loginCb', res );
this.set( 'state.status', res.status );
this.set( 'state.prkey', res.prkey );
this.set( 'state.usernode', res.usernode );
},
register: function( userName, password ) {
// using vars for readability
var gun = window[ this.gunVar ];
var cb = this.registerCb;
var opt = {
useGunChain: this.useGunChain,
noPut: this.noPut
};
gun.p2pAuthRegister( userName, password, opt, cb );
},
/**
* User can override this callback from the outside<br>
* ```
* gunp2pauth.registerCb = function(res) {
* // do something with the res
* }
* ```
* @param {Object} res [result from gun-p2p-auth.js]
*
*/
registerCb: function( res ) {
alert( 'Register result: ' + res.status );
}
} );
</script>
</dom-module>