Skip to content

Instantly share code, notes, and snippets.

@jfrux
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfrux/ae14b86b9fc299f6ae29 to your computer and use it in GitHub Desktop.
Save jfrux/ae14b86b9fc299f6ae29 to your computer and use it in GitHub Desktop.
CoffeeScript version with emberfire 1.4.2, firebase ~2.1.0, also attempting to add authWithPassword
`import Ember from 'ember'`
`import Firebase from 'firebase'`
`import config from '../config/environment'`
ref = new Firebase(config.firebase);
initialize = (container, application) ->
session = Ember.Object.extend
authed: false
init: () ->
_this = @
@store = container.lookup('store:main')
# on init try to login
ref.onAuth ((authData) ->
#Not authenticated
if (!authData)
_this.set('authed', false)
_this.set('authData', null)
_this.set('user', null)
return false
#Authenticated
_this.set('authed', true);
_this.set('authData', authData);
_this.afterAuthentication(authData.uid);
return
).bind(@)
return
login: (type,params = {}) ->
switch type
when "email"
@_loginWithEmail params.email,params.password
else
@_loginWithProvider type
return
logout: ->
return
_loginWithEmail: (email,password) ->
ref.authWithPassword {email: email, password: password}, (error,authData) ->
if error
console.log error
else
console.log authData
_loginWithProvider: (provider) ->
_this = this
# Ember.debug('logging in with popup');
ref.authWithOAuthPopup provider, (error, authData) ->
if error
if error.code == 'TRANSPORT_UNAVAILABLE'
# fall-back to browser redirects, and pick up the session
# automatically when we come back to the origin page
_this._loginWithRedirect provider
else if authData
# we're good!
# this will automatically call the on ref.onAuth method inside init()
else
return
_loginWithProviderRedirect: (provider) ->
ref.authWithOAuthRedirect provider, (error, authData) ->
if error
else if authData
# we're good!
# this will automatically call the on ref.onAuth method inside init()
else
return
afterAuthentication: (userId) ->
_this = this
# See if the user exists using native Firebase because of EmberFire problem with "id already in use"
# Do the right thing depending on whether the user exists
userExistsCallback = (userId, exists) ->
if exists
_this.existingUser userId
else
_this.createUser userId
return
ref.child('users').child(userId).once 'value', (snapshot) ->
exists = snapshot.val() != null
userExistsCallback userId, exists
return
existingUser: (userId) ->
_this = this
@store.find('user', userId).then ((user) ->
_this.set 'user', user
return
).bind(this)
afterUser: (user) ->
@set('user', user)
application.register('session:main', session);
application.inject('route', 'session', 'session:main');
application.inject('controller', 'session', 'session:main');
return
authinitializer =
name: 'session'
after: 'store',
initialize: initialize
`export {initialize}`
`export default authinitializer`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment