Skip to content

Commit

Permalink
Released v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yankeeinlondon committed Feb 4, 2017
1 parent 8944955 commit a6a01ef
Show file tree
Hide file tree
Showing 6 changed files with 1,569 additions and 335 deletions.
20 changes: 17 additions & 3 deletions addon/services/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ let app;
* start listening for Auth State changes and dispatch
* events when they occur
*/
function onAuthStateChanged(dispatch) {
function onAuthStateChanged(context) {
const dispatch = context.get('redux.dispatch');
app.auth().onAuthStateChanged(
(currentUser) => dispatch({type: 'CURRENT_USER_CHANGED', currentUser}),
(user) => {
dispatch({type: 'CURRENT_USER_CHANGED', user});
context.set('isAuthenticated', user ? true : false);
},
(e) => dispatch({type: 'ERROR_DISPATCHING', message: e.message}));
}

Expand All @@ -35,6 +39,7 @@ function onConnectedChanged(dispatch, url) {

const fb = Ember.Service.extend({
redux: service(),
_currentUserProfile: false,

init() {
const {redux} = this.getProperties('redux');
Expand All @@ -49,7 +54,7 @@ const fb = Ember.Service.extend({
debug(`Connected to Firebase DB: ${config.firebase.databaseURL}`);
const modulePrefix = get(config, 'modulePrefix');
app = window.firebase.initializeApp(config.firebase, modulePrefix || DEFAULT_NAME);
onAuthStateChanged(redux.dispatch);
onAuthStateChanged(this);
onConnectedChanged(redux.dispatch, config.firebase.databaseURL);
},

Expand All @@ -69,6 +74,15 @@ const fb = Ember.Service.extend({
return unwatch(app);
},

currentUserProfile(path) {
if (!path && path === false) {
this._currentUserProfile = '/users';
} else {
this._currentUserProfile = path;
}
},
isAuthenticated: false

});

export default fb;
64 changes: 43 additions & 21 deletions addon/services/firebase/auth.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
import Ember from 'ember';
const { RSVP } = Ember;
let loggedInUser = null;

const auth = (context, app) => {
return {
// AUTH
emailAndPassword: (email, password) => {
const {redux} = context.getProperties('redux');
emailAndPassword(email, password) {
const { redux, firebase } = context.getProperties('redux', 'firebase');
return new RSVP.Promise((resolve, reject) => {

redux.dispatch({
type: 'AUTH_REQUEST',
kind: 'emailAndPassword',
email
});
app.auth().signInWithEmailAndPassword(email, password)
.then(user => {
loggedInUser = user.uid;
redux.dispatch({
type: 'AUTH_SUCCESS',
user
});
if(context._currentUserProfile) {
firebase.watch.node(`${context._currentUserProfile}.${user.uid}`, 'USER_PROFILE_UPDATED');
}
resolve();
})
.catch(e => {
redux.dispatch({
type: 'AUTH_FAILURE',
code: e.code,
message: e.message,
email
});
reject(e);
});

}); // end PROMISE
}, // end emailAndPassword
signOut() {
const { redux } = context.getProperties('redux');
app.auth().signOut();
redux.dispatch({
type: 'AUTH_REQUEST',
kind: 'emailAndPassword',
email
type: 'AUTH_LOG_OFF',
uid: loggedInUser
});
app.auth().signInWithEmailAndPassword(email, password)
.then(user => {
redux.dispatch({
type: 'AUTH_SUCCESS',
user
});
})
.catch(e => {
redux.dispatch({
type: 'AUTH_FAILURE',
code: e.code,
message: e.message,
email
});
});
loggedInUser = null;
},
// END AUTH

};
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"ember-cli-inject-live-reload": "^1.4.1",
"ember-cli-jshint": "^2.0.1",
"ember-cli-qunit": "^3.0.1",
"ember-cli-release": "^0.2.9",
"ember-cli-release": "^1.0.0-beta.2",
"ember-cli-sass": "^6.1.1",
"ember-cli-shims": "^1.0.2",
"ember-cli-sri": "^2.1.0",
Expand Down
3 changes: 3 additions & 0 deletions tests/dummy/app/controllers/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export default Ember.Controller.extend({
this.get('firebase').ref('hello').once('value', (snapshot) => {
console.log(snapshot.val());
});
},
signOut() {
this.get('firebase').auth().signOut();
}
}
});
20 changes: 20 additions & 0 deletions tests/dummy/app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@
<p>
You can see that using the configured username/password successfully authenticates whereas other combinations report back an error.
</p>

<h6 class="row col-12">Logging Out</h6>
<p>
Logging a user out is straight-forward, in code it is just:
</p>
<pre class="row col-12"><code> firebase.auth().signOut();</code></pre>
<p>
You can press the button to execute that here: {{ui-button 'sign out' onClick=(action 'signOut')}}
</p>
</div>

<div class="row downer">
Expand Down Expand Up @@ -209,6 +218,17 @@
</p>
</div>

<div class="row">
<h4>Current User Profile</h4>
<p>
A very common requirement for an application is to have the logged in user's profile available (and kept up-to-date when changed) as a handy reference to application code. The Firebase Auth system has a limited user profile that it defines and if that's all you need there is nothing else needed as this will be returned as part of both the AUTH_SUCCESS and CURRENT_USER_CHANGED actions. Many applications though have their own user profiles which structurally fit into the limited properties that Firebase allows. In order to provide easy access to this richer user profile data you may opt-in with:
</p>
<pre class=><code> firebase.currentUserProfile(path)</code></pre>
<p>
The {{tag 'path'}} parameter indicates the path in the Firebase DB where the list of User Profiles reside. It also assumes that the User Profiles are keyed by 'uid' (which is what the Authentiation system uses). If you leave the path parameter blank then it will default to "/users". Once enabled, the CURRENT_USER_CHANGED action will also include "profile" property off of the "user" property which will contain the broader user profile.
</p>
</div>

</div>

{{outlet}}
Loading

0 comments on commit a6a01ef

Please sign in to comment.