How to do confirmation with devise authentication in Ruby on Rails - ruby-on-rails-3

I am using devise in my application for authentication. When I try to register, I get the following error:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
I am using :comfirmable and had uncommented t.confirmable in the migration

In order to use confirmable module you need to configure ActionMailer that is used by devise for sending confirmation emails. First step for solving your problem is setting up mailer host in you environment.rb or in the corresponding file for a particular environment like that:
config.action_mailer.default_url_options = { :host => “example.com” }
For further steps have a look at this rails guide and answers to this question.

Related

Does "devise_token_auth" gem support web-based authentication?

This gem ("devise_token_auth") is used for token authentication for applications using JSON APIs for front-end development.
Can we use this gem for server side rendering? If yes, then how to add the token from a previous response to the current request?
I don't know if this is still a pressing matter for you, but I'd like to throw in some advice.
For your API you can throw in devise_token_auth and it will do what everything you need for authentication there.
And if you need authentication with server-side rendering of pages (such as login forms, reset password forms, etc.) just throw in regular devise too. It will work with your exact same User model and table, and there will be little friction to get things up and running with the same resources you use with devise_token_auth.
Gemfile
#autentication and authorization
gem 'devise', '~> 3.5', '>= 3.5.6'
gem 'devise_token_auth', '0.1.37'
Then run
bundle
Run the installer for devise:
rails generate devise:install
Then generate your user model:
rails generate devise User
Install devise_token_auth now:
rails g devise_token_auth:install User "auth"
And make sure your database is migrated:
rake db:migrate
I think devise_token_auth may overwrite your user model, I'm not certain, but if it does, keep the migrations for devise_token_auth only and ignore the migrations for Devise.
Then make sure your routes.rb matches this:
Rails.application.routes.draw do
devise_for :users
root "home#index"
namespace :api, defaults: { format: :json } do
namespace :v1 do #I namespace my routes
mount_devise_token_auth_for "User", at: "auth"
end
end
end
devise_for must come before mount_devise_token_auth.
Then just refer to the official devise and devise token auth documentation to get both solutions working for you.
Hope this helps anyone who reaches this point and has a need to authenticate users on mobile app and on browser web app.

How to integrate batch requests in a Rails API?

I'm building an API with Rails 4 and I really want to create a Batch request method to not overload my app when doing a bunch of ajax requests.
The Railscasts shows how to do it in a simple way but there's a lot of stuff missing.
I also tried the batch_api gem but I wasn't successful integrating it with my application.
Any ideas?
I know it's being late to answer this question but I recently used batch_api gem with my Rails API (rails 5.0.0 and Ruby 2.0) and it works find with me.
What you need to do is follow the instruction of this document:
https://github.com/arsduo/batch_api
Shortly:
1- You need to add the batch_api gem to your application GemFile.
2- You need to add the required middleware configuration in you application.rb file:
config.middleware.use BatchApi::RackMiddleware do |batch_config|
# you can set various configuration options:
batch_config.verb = :put # default :post
batch_config.endpoint = "/batchapi" # default /batch
batch_config.limit = 100 # how many operations max per request, default 50
# default middleware stack run for each batch request
batch_config.batch_middleware = Proc.new { }
# default middleware stack run for each individual operation
batch_config.operation_middleware = Proc.new { }
end
3- Then restart your rails server.
Make sure to insert the new middleware in the appropriate location, in my case I needed to include it before "ActionDispatch::RequestId" middleware.
config.middleware.insert_before "ActionDispatch::RequestId", BatchApi::RackMiddleware
because I wanted to include X-Request-ID header in each request in the Batch request and this ID will be returned in each response so that I could know the response for each request in the Batch (note that the responses will be executed sequentially depending on the sequence each request in the Batch).
Apparently the batch_api gem doesn't work with rails 4 yet, but there is a fork that was started to update it to rails 4 and ruby 2.0.
https://github.com/easyPEP/batch_api/tree/feature_ruby_2

Devise and Stateless tokens in Rails

I got an API that I have developed using Rails 3 and Devise. I am using tokens (token_authenticatable) for authentication for requests made to the API from a client. I want to be able to switch between users in the requests just be replacing the token.
I heard about a setting called :stateless_token (boolean) but I cannot figure out where to put this setting. Is there another way?
If found the token_authenticatable here:
https://github.com/plataformatec/devise/blob/master/lib/devise/strategies/token_authenticatable.rb
If found info about the stateless_token here:
http://rdoc.info/github/plataformatec/devise/master/Devise/Models/TokenAuthenticatable
stateless_token is deprecated as of now. This is the new form (it allows more auth strategies to be stateless):
# config/initializers/devise.rb
config.skip_session_storage = [:token_auth]
You can also edit the file /config/initializers/devise.rb and put (or uncomment, if already there) the following line:
config.stateless_token = true
It should be an option in your devise_for line in the routes file.
devise_for :users, :stateless_token => true
Let me know if that works,
In this page of documentation for devise it says that "TokenAuthenticatable adds the following options to devise_for:" with stateless token being one of them.
Also here is a link to the devise_for documentation

Setting Up Devise & Sendgrid on Heroku

My site is hosted on Heroku and I installed the Sendgrid Add-On as it looked almost too good to be true - but so far none of the email functionality is working. I have read the documentation and it clearly says just add-the add on - is more configuration required to get Devise working?
When I select 'send me new password' I get a 404 page which makes me think there is more to this. Like how does Sendgrid know/where to use the pre-installed Devise templates?
Thx.
I just set up Devise and SendGrid this morning and have no problems. I'm going to resume the steps I took.
First, install Devise and SendGrid. Congratulations, you've already done that ;)
Then, for production, add this to your files:
config/initializers/devise.rb :
config.mailer_sender = "mail-to-send#from.com"
Set up Rails ActionMailer to use SendGrid
config/environments/production.rb
config.action_mailer.default_url_options = { :host => 'your.websitedomain.com' }
ActionMailer::Base.smtp_settings = {
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:address => "smtp.sendgrid.net",
:port => 587,
:enable_starttls_auto => true,
:authentication => :plain,
:domain => "yourdomain.com"
}
And everything's working great with that. Sign up confirmations, password recovery...
Also, you should use Logging Expanded (it's Free!) and check your logs with heroku logs --tail (for real time).
If you still get errors, post your logs.
Have a good day !
I've used the sendgrid Add-On and it really should just work. Like you said, even the docs say so:
Rails apps using ActionMailer will just work, no setup is needed after the add-on is installed.
So, this makes me think something else is going on. Have you tried using the heroku logs command to see if your application is logging any errors?

Rails 3 devise i need help with confermation

I have watched the railscast on devise for some reason he pulled out the confrmation e-mail part so i have no idea how it works i have it up and running but what i need to know is dose it send an e-mail in development or not?
is there a way to force it to send the mail just to test it or can you enplane how to do it in the console!
it doesn't send email on development environment. You need to edit your config/environments/development.rb and add :
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Then look in your console running your server when it try to send an email you will see actually all what your need there (headers, title and body email)