vendredi 14 juillet 2017

Ruby on Rails API + Devise

I'm working with a friend on a projet with RoR + Devise for authentication.

My friend is doing an application with React Native.

I'm actually trying to build with my existing RoR project an API.

  namespace :api do
    namespace :v1 do
      # Users
      get '/users' => 'users#index'
      get '/users/:id' => 'users#show'
    end
  end

  # BaseController
  class Api::V1::BaseController < ApplicationController

  protect_from_forgery with: :null_session
  before_action :destroy_session

  rescue_from ActiveRecord::RecordNotFound, with: :not_found!

  def destroy_session
    request.session_options[:skip] = true
  end

  def not_found!
    return api_error(status: 404, errors: 'Not found')
  end

  def api_error(status: 500, errors: [])
    unless Rails.env.production?
      puts errors.full_messages if errors.respond_to? :full_messages
    end
    head status: status and return if errors.empty?

    render json: jsonapi_format(errors).to_json, status: status
  end
end

# UsersController
class Api::V1::UsersController < Api::V1::BaseController

  before_action :find_user, only: [:show]

  def index
    @users = User.all
    render json: @users
  end

  def show
    render json: @user
  end

  private
  def find_user
    @user = User.find(params[:id])
  end

  def user_params
    params.require(:user).permit(:username, :email, :password)
  end
end

I configured my Serializers too.

My question is :

How can i handle with Devise and API configuration, registrations / sessions controllers ?

Aucun commentaire:

Enregistrer un commentaire