Advertisement
Guest User

Untitled

a guest
Oct 7th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.93 KB | None | 0 0
  1. # in /controllers/api/sessions_controller.rb
  2. module Api
  3.   class SessionsController < Devise::SessionsController
  4.     skip_before_action :verify_authenticity_token, only: [:create, :facebook]
  5.     respond_to :json
  6.  
  7.     # ...
  8.  
  9.     def create
  10.       @user = warden.authenticate(scope: resource_name, recall: "#{controller_path}#new")
  11.  
  12.       if @user.nil?
  13.         invalid_login("Invalid email or password")
  14.       elsif @user.provider == "facebook"
  15.         invalid_login("Must sign in via Facebook")
  16.       else
  17.         @user.auth_tokens.create!
  18.         render "api/users/show"
  19.       end
  20.     end
  21.    
  22.     # ...
  23.  
  24. end
  25.  
  26.  
  27. # in routes.rb
  28. Rails.application.routes.draw do
  29.  
  30.   # ...
  31.  
  32.   namespace :api, defaults: { format: "json" } do
  33.     devise_scope :user do
  34.       post "/users/sign_in", to: "sessions#create"
  35.       post "/users/fb_auth", to: "sessions#facebook"
  36.       post "/users/password", to: "passwords#create"
  37.     end
  38.  
  39.   # ...
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement