All Projects → netguru → Devise Ios Rails

netguru / Devise Ios Rails

Licence: other
Devise iOS Rails Backend. Gem that updates devise to work with external clients. Specially created to work with devise for iOS.

Programming Languages

ruby
36898 projects - #4 most used programming language

Devise iOS Rails Backend

Gem that updates devise to work with external clients. Specially created to work with devise for iOS.

It currently implements authentication only with a "simple token authentication".

Requirements

Name Version
Ruby 2.1.5
Rails 4.1.8
Devise
Simple Token Authentication
Active Model Serializers

Setup

With a working devise environment, the only thing you need to do is:

  • add gem to the Gemfile gem 'devise-ios-rails'
  • run bundler to install the gem bundle install
  • setup devise like you would normally do (check the installation guide)
  • in your routes change devise_for ModelName with devise_ios_rails_for ModelName (ModelName is usually User)
  • authentication is handled by user token which is generated for each user during the registration process.

Add the following line to your ModelName model

# app/models/model_name.rb
class ModelName < ActiveRecord::Base
  acts_as_token_authenticatable
  ...
end

Add a migration for adding authentication_token column to your Devise model. If your ModelName is User then the migration should look like this:

class AddUniqueTokenToUser < ActiveRecord::Migration
  def change
    add_column :users, :authentication_token, :string
    add_index :users, :authentication_token, unique: true
  end
end

Don't forget about rake db:migrate.

-To protect actions to only registered users, add acts_as_token_authentication_handler_for User in your controller:

class SecretSpacesController < ApplicationController
  acts_as_token_authentication_handler_for User
end
  • If you want to skip authentication for some actions add skip_before_filter :authenticate_user_from_token!, only: [:action] in your controller
class SecretSpacesController < ApplicationController
  acts_as_token_authentication_handler_for User
  skip_before_filter :authenticate_user_from_token!, only: [:new]
end

Facebook

To sign in using Facebook include DeviseIosRails::Oauth in your model after the devise method:

class User < ActiveRecord::Base
  acts_as_token_authenticatable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  include DeviseIosRails::OAuth
end

Add required fields to the model (keep in mind that you might not get e-mail address from the provider):

class AddOauthToUsers < ActiveRecord::Migration
  def change
    add_column :users, :uid, :string
    add_column :users, :provider, :string
    add_column :users, :oauth_token, :string

    change_column :users, :email, :string, :null => true

    add_index :users, [:uid, :provider], unique: true
  end
end

And update routes:

devise_scope :user do
  post 'auth/facebook', to: 'devise_ios_rails/oauth#facebook'
end

Example app

Testing

In order to run tests, first you need setup the gem locally.

  • clone the repo to your machine git clone https://github.com/netguru/devise-ios-rails.git
  • go inside gems directory cd devise-ios-rails and run bundle command bundle install
  • now you need to setup your environment variables. You can simply just copy over .env.sample to .env. It should look more or less like this:
DOMAIN_NAME='localhost:3000'
DOMAIN_URL='http://localhost:3000'
SECRET_KEY_BASE='a_very_long_string'
DEFAULT_SENDER='[email protected]'

then you can run your tests by typing rspec.

Contribution

First, thank you for contributing!

Here's a few guidelines to follow:

You can also read our blog post announcing devise-iOS for simplified auth.

Copyright 2014-2015 © Netguru, released under the New BSD License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].