Files
ember-cli-dunlop/app/components/login-form.coffee
T
2017-12-21 11:42:41 +01:00

44 lines
1.8 KiB
CoffeeScript

import Ember from 'ember'
#import config from '../config/environment'
export default Ember.Component.extend
session: Ember.inject.service('session')
identification: ''
password: ''
actions:
submitLogin: ->
unless email=@get('identification')
return @$('.login-email').focus()
unless password=@get('password')
return @$('.login-password').focus()
## STRATEGY 1 HARD TOKEN FROM API
#uri = "#{config.apiHost}/#{config.apiNamespace}/users/obtain_token"
#Ember.$.post uri, email: email, password: password, (result) =>
# result = JSON.parse(result) if typeof result is 'string'
# unless result.valid
# @set 'globals.flash_message', 'Invalid login'
# return false
# localStorage.setItem 'authentication_token', result.authentication_token
# localStorage.setItem 'user_id', result.user_id
# @get('router').transitionTo('index').then (index_route) ->
# # do not set on globals of current route, since that one will be 'destroyed' in trasition
# index_route.store.findRecord('user', result.user_id).then (current_user) -> index_route.set('router.globals.current_user', current_user)
## STRATEGY 2 DEVISE SESSION BASED
#uri = "#{config.apiHost}/users/sign_in.json"
#Ember.$.post uri, email: email, password: password, (result) =>
# debugger
#.fail (error) =>
# # if error.status is 401
# @set 'globals.flash_message', 'Invalid login'
# STRATEGY 2 DEVISE EMBER SIMPLE AUTH BASED
@get('session').authenticate('authenticator:devise', email, password).catch (reason) =>
message = if typeof(reason) is 'string' then reason else reason.error
message ||= reason.message
message ||= 'Invalid login'
@set 'globals.flash_message', message
false