Skip to content

Instantly share code, notes, and snippets.

@DVG
Created July 11, 2013 12:34
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 DVG/5975064 to your computer and use it in GitHub Desktop.
Save DVG/5975064 to your computer and use it in GitHub Desktop.
EmberAuth
StripfighterEmber.ApplicationController = Ember.Controller.extend
signOut: ->
StripfighterEmber.Auth.signOut()
StripfighterEmber.Auth.destroySession()
StripfighterEmber.RegistrationController = Ember.Controller.extend
email: null
password: null
passwordConfirmation: null
sendRegistration: () ->
self = @
$.post('/users',
user:
email: @email,
password: @password,
password_confirmation: @passwordConfirmation)
.done (response) ->
# sign in
)
.fail (response) ->
# display errors
class RegistrationsController < Devise::RegistrationsController
respond_to :json
def create
user = User.new(params[:user])
if user.save
user.ensure_authentication_token!
user.remember_me!
data = {
user_id: user.id,
auth_token: user.authentication_token,
remember_token: remember_token(user)
}
render json: data, status: 201
return
else
warden.custom_failure!
render :json=> {errors: user.errors}, :status=>422
end
end
protected
def remember_token(resource)
data = resource_class.serialize_into_cookie(resource)
"#{data.first.first}-#{data.last}"
end
end
class SessionsController < Devise::SessionsController
def create
unless (params[:email] && params[:password]) || (params[:remember_token])
return missing_params
end
build_resource
resource = if params[:remember_token]
resource_from_remember_token
else
resource_from_credentials
end
return invalid_credentials unless resource
resource.ensure_authentication_token!
data = {
user_id: resource.id,
auth_token: resource.authentication_token,
}
if params[:remember]
resource.remember_me!
data[:remember_token] = remember_token(resource)
end
render json: data, status: 201
end
def destroy
return missing_params unless params[:auth_token]
resource = resource_class.find_by_authentication_token(params[:auth_token])
return invalid_credentials unless resource
resource.reset_authentication_token!
render json: {user_id: resource.id}, status: 200
end
protected
def missing_params
warden.custom_failure!
return render json: {}, status: 400
end
def invalid_credentials
warden.custom_failure!
render json: {}, status: 401
end
def remember_token(resource)
data = resource_class.serialize_into_cookie(resource)
"#{data.first.first}-#{data.last}"
end
def resource_from_remember_token
token = params[:remember_token]
id, identifier = token.split('-')
resource_class.serialize_from_cookie(id, identifier)
end
def resource_from_credentials
data = { email: params[:email] }
if res = resource_class.find_for_database_authentication(data)
if res.valid_password?(params[:password])
res
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment