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
withdevise_ios_rails_for ModelName
(ModelName is usually User) - authentication is handled by user token which is generated for each user during the registration process.
To make it work you need to run migration that adds 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
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 commandbundle 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='no-reply@no-reply.com'
then you can run your tests by typing rspec
.
Contribution
First, thank you for contributing!
Here's a few guidelines to follow:
- we follow Ruby Style Guide.
- you can use rubocop which can be easily integrated with popular editors. (our rubocop config)
- write tests
- make sure the entire test suite passes
- make sure rubocop passes our config
- open a pull request on GitHub
- squash your commits after receiving feedback
You can also read our blog post announcing devise-iOS for simplified auth.
Copyright 2014-2015 © Netguru, released under the New BSD License