Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Merge branch 'sfeast-passwordFuncs'
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Aug 5, 2014
2 parents 59c65dc + d5d8135 commit f12cede
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 8 deletions.
73 changes: 66 additions & 7 deletions demos/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<link rel="import" href="../firebase-login.html">

<style>
input {
input:not([type]) {
width: 250px;
}
</style>
Expand All @@ -21,7 +21,7 @@

<h2>Firebase Simple Login</h2>

<firebase-login id="login" user="{{user}}" statusKnown="{{statusKnown}}" location="https://polymer-tests.firebaseio.com" provider="{{provider}}"></firebase-login>
<firebase-login id="login" user="{{user}}" statusKnown="{{statusKnown}}" location="https://polymer-tests.firebaseio.com" provider="{{provider}}" on-error="{{error}}" on-user-created="{{userSuccess}}" on-password-changed="{{userSuccess}}" on-password-reset="{{userSuccess}}" on-user-removed="{{userSuccess}}"></firebase-login>

Firebase location:
<input value="https://polymer-tests.firebaseio.com" disabled>
Expand All @@ -36,13 +36,29 @@ <h2>Firebase Simple Login</h2>
<option>twitter</option>>
<option>password</option>
</select>
<em>Only 'anonymous' and 'google' are activated for demo Firebase account</em>
<em>Only 'anonymous', 'google', and 'password' are activated for demo Firebase account</em>
<br>

Login params (JSON):
<input value="{{params}}" id="params">
<em>Required by some provider types</em>
<br><br>
<br>

<div hidden?="{{provider!='password'}}">
<br><em>Password-specific options:</em><br>
<input placeholder="email" value="{{email}}">
<input placeholder="password" value="{{password}}">
<button on-tap="{{createUser}}" disabled?="{{!email || !password}}">Create user</button>
<br>
<input placeholder="new password" value="{{newPassword}}">
<button on-tap="{{changePassword}}" disabled?="{{!email || !password || !newPassword}}">Change password</button>
<br>
<button on-tap="{{resetPassword}}" disabled?="{{!email || !password}}">Reset password</button>
<button on-tap="{{removeUser}}" disabled?="{{!email || !password}}">Remove user</button>
</div>
<br>
<div id="message"></div>
<br>

<button on-tap="{{login}}" hidden?="{{!statusKnown || user}}">Login</button>
<button on-tap="{{logout}}" hidden?="{{!statusKnown || !user}}">Logout</button>
Expand All @@ -61,19 +77,62 @@ <h3>User ID:</h3>

var scope = e.target;
var fbLogin = document.querySelector("#login");
var message = document.querySelector("#message");

scope.login = function() {
var params;
try {
this.params = JSON.parse(document.querySelector("#params").value);
params = JSON.parse(document.querySelector("#params").value);
} catch (e) {
this.params = null;
params = null;
}
if (this.provider == 'password') {
params = this.params || {};
params.email = this.email;
params.password = this.password;
}
fbLogin.login();
fbLogin.login(params);
};

scope.logout = function() {
fbLogin.logout();
};

scope.createUser = function() {
fbLogin.createUser(this.email, this.password);
};

scope.removeUser = function() {
fbLogin.removeUser(this.email, this.password);
};

scope.changePassword = function() {
fbLogin.changePassword(this.email, this.password, this.newPassword);
};

scope.resetPassword = function() {
fbLogin.sendPasswordResetEmail(this.email);
};

scope.error = function(e) {
setMessage('Error: ' + e.detail.message, true);
};

scope.userSuccess = function(e) {
setMessage(e.type + ' success!', false);
};

var msgTimeout;
function setMessage(msg, error) {
if (msgTimeout) {
clearTimeout(msgTimeout);
}
message.innerText = msg;
message.style.color = error ? 'red' : null;
msgTimeout = setTimeout(function() {
message.innerText = '';
}, 3000);
}

});
</script>
Expand Down
148 changes: 147 additions & 1 deletion firebase-login.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@
* @event error
*/

/**
* Fired when user is created (password provider type)
*
* @event user-created
*/

/**
* Fired when user changes their password (password provider type)
*
* @event password-changed
*/

/**
* Fired when password reset email is sent (password provider type)
*
* @event password-reset
*/

/**
* Fired when user is removed (password provider type)
*
* @event user-removed
*/

/**
* Firebase location URL (must have simple login enabled via Forge interface).
* @attribute location
Expand Down Expand Up @@ -88,7 +112,7 @@
this.auth = new FirebaseSimpleLogin(this.ref, function(error, user) {
if (error) {
// an error occurred while attempting login
this.fire('error', {message: error});
this.fire('error', error);
} else if (user) {
// user authenticated with Firebase
this.user = user;
Expand All @@ -114,6 +138,26 @@
}.bind(this));
},

/**
* Performs a login attempt, using the `provider` specified via attribute/property,
* or optionally via `provider` argument to the `login` function. Optionally,
* provider-specific login parameters can be specified via attribute (JSON)/property,
* or via the `params` argument to the `login` function.
*
* If the login is successful, the `login` event is fired, with `e.detail.user`
* containing the authenticated user object from Firebase.
*
* If login fails, the `error` event is fired, with `e.detail` containing error
* information supplied from Firebase.
*
* If the browswer supports `navigator.onLine` network status reporting and the
* network is currently offline, the login attempt will be queued until the network
* is restored.
*
* @method login
* @param {string} provider (optional)
* @param {string} params (optional)
*/
login: function(provider, params) {
if (navigator.onLine === false) {
this.queuedLogin = {provider: provider, params: params};
Expand All @@ -127,6 +171,20 @@
}
},

/**
* Performs a logout attempt.
*
* If the login is successful, the `logout` event is fired.
*
* If login fails, the `error` event is fired, with `e.detail` containing error
* information supplied from Firebase.
*
* If the browswer supports `navigator.onLine` network status reporting and the
* network is currently offline, the logout attempt will be queued until the network
* is restored.
*
* @method logout
*/
logout: function() {
if (navigator.onLine === false) {
this.queuedLogout = true;
Expand All @@ -143,6 +201,94 @@
this.login(this.queuedLogin.provider, this.queuedLogin.params);
this.queuedLogin = null;
}
},

/**
* Creates a "password provider"-based user account.
*
* If the operation is successful, the `user-created` event is fired.
*
* If the operation fails, the `error` event is fired, with `e.detail`
* containing error information supplied from Firebase.
*
* @method createUser
* @param {string} email
* @param {string} password
*/
createUser: function(email, password) {
this.auth.createUser(email, password, function(error, user) {
if (!error) {
this.fire('user-created', {user: user});
} else {
this.fire('error', error);
}
}.bind(this));
},

/**
* Changes the password of a "password provider"-based user account.
*
* If the operation is successful, the `user-created` event is fired.
*
* If the operation fails, the `error` event is fired, with `e.detail`
* containing error information supplied from Firebase.
*
* @method changePassword
* @param {string} email
* @param {string} oldPassword
* @param {string} newPassword
*/
changePassword: function(email, oldPassword, newPassword) {
this.auth.changePassword(email, oldPassword, newPassword, function(error, success) {
if (!error) {
this.fire('password-changed');
} else {
this.fire('error', error);
}
}.bind(this));
},

/**
* Sends a password reset email for a "password provider"-based user account.
*
* If the operation is successful, the `user-created` event is fired.
*
* If the operation fails, the `error` event is fired, with `e.detail`
* containing error information supplied from Firebase.
*
* @method sendPasswordResetEmail
* @param {string} email
*/
sendPasswordResetEmail: function(email) {
this.auth.sendPasswordResetEmail(email, function(error, success) {
if (!error) {
this.fire('password-reset');
} else {
this.fire('error', error);
}
}.bind(this));
},

/**
* Removes a "password provider"-based user account.
*
* If the operation is successful, the `user-created` event is fired.
*
* If the operation fails, the `error` event is fired, with `e.detail`
* containing error information supplied from Firebase.
*
* @method removeUser
* @param {string} email
* @param {string} password
*/
removeUser: function(email, password) {
this.auth.removeUser(email, password, function(error, success) {
if (!error) {
this.fire('user-removed');
} else {
this.fire('error', error);
}
}.bind(this));
}

});
Expand Down

0 comments on commit f12cede

Please sign in to comment.