I am using Devise for authenticating a Rails application. I am now able to successfully route /users/sign_in and /users/sign_out to /sign_in and /sign_out via this code in routes.rb:
devise_for :user, :as => ''
How do I map /registration/sign_up to /sign_up?
So that sign_in, sign_out, and sign_up all have the same pattern.
Note that I am using Devise only for users. No admins.
You need to add the following block to your routes.rb file:
devise_scope :user do
get "/sign_up" => "devise/registrations#new"
end
It's explained in: http://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
Related
I am trying to make custom sign_in url for my app. So I created a new route in routes.rb. my code sample is
devise_scope :user do
get 'site-admin/login', to: 'devise/sessions#new'
end
But still'users/sign_in' is also going to login page.How to redirect 'users/sign_in' to 404 page?
According to the devise documentation, all you have to do in your routes.rb is this:
devise_for :users, skip: [:sessions]
as :user do
get 'site-admin/login', to: 'devise/sessions#new', as: :new_user_session
end
I am new to Ruby on Rails,I am using Devise gem for authentication on table account_user.
When I do rake routes I get
new_account_user_session GET /account_users/sign_in(.:format)account_user/sessions#new
So my login page is xyz.com/account_users/sign_in.
I want to change the sign-in page to just xyz.com
I don't have any routes for the same in my routes.rb file, I thought devise is automatically generating routes for this.
Is there a way I can add alias/override for this devise generated routes, or redirect user to xyz.com instead of xyz.com/account_users/sign_in
set root to devise sign_in, so in your route file there should be
devise_for :account_users
devise_scope :account_user do
root to: 'devise/sessions#new'
end
this will set your root path to sign_in
or if you want to rename the route to 'login'
devise_for :account_users
devise_scope :account_user do
get 'login', to: 'devise/sessions#new'
end
more here https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
I have a Rails 4.1 Application running with Devise for authentication.
For access via mobile apps i would like to implement token auth with the recommended devise_token_auth gem. I do not use Omniauth
The functionality of the existing app should not be altered.
What i did:
Installed devise_token_auth via gemfile.
Used the generator: rails g devise_token_auth:install User auth
Changed the migration to add the required fields.
Migration failed due missing of Omniauth. So i also installed it.
Changed routes.rb
devise_for :users, :skip => [:sessions, :registrations, :omniauth_callbacks]
as :user do
get 'register' => 'users/registrations#new', :as => :new_user_registration
post 'register' => 'users/registrations#create', :as => :user_registration
get 'sign_in' => 'devise/sessions#new', :as => :new_user_session
post 'sign_in' => 'devise/sessions#create', :as => :user_session
delete '/' => 'users/sessions#destroy', :as => :destroy_user_session
end
added:
namespace :api do
scope :v1 do
mount_devise_token_auth_for 'User', at: 'auth', skip: [:omniauth_callbacks]
end
end
In User Model i have:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :timeoutable, :lockable
include DeviseTokenAuth::Concerns::User
Now when i try to sign_up a new user it gives me the validation error:
Uid can't be blank
Does someone had the same problem and resolved it?
What i find strange is that it needs to have Omniauth installed.
Update:
I overwrite the Devise registration controller create action:
build_resource(sign_up_params)
resource.uid = resource.email
resource.provider = ''
Now when i sign_in i get:
{"errors":["Authorized users only."]}
in Browser.
Adding the following to app/models/user.rb:
before_validation do
self.uid = email if uid.blank?
end
did it for me. Also make sure the provider is set to "email" for "provider".
Well I'm currently struggling with the same thing. Trying to add devise_token_auth to Devise, and it is not working so far for me.
As far as this goes, are you talking about "sign_up" for Devise, or devise_token_auth? If it is for Devise, I supposed setting uid=email before creating the record would solve this.
This error is raised by devise_token_auth, not by devise. So essentially, devise_token_auth is trying to authenticate your normal devise routes the same way it would normally authenticate an api request. Your normal devise routes are authenticating via session, not via token, so you'll get this error:
{"errors":["Authorized users only."]}
There are a couple of things that could be happening here. First, make sure that you're only looking for token validation on the actions of your API controllers. So make sure that this line is included in your BaseAPIController, and not in your ApplicationController.
include DeviseTokenAuth::Concerns::SetUserByToken
The other possibility is that you have some namespacing issues in your routes.rb. Make sure that you have something like this. You need to have devise_for first, and the token_auth properly namespaced or it will cause validations issues on your other routes.
Rails.application.routes.draw do
devise_for :admins
namespace :api do
scope :v1 do
mount_devise_token_auth_for 'user', at: 'auth'
end
end
end
Good luck!
I user devise 3.4.0 under rails 4.1.0.
I want to add user detail page, so I made this route
get 'users/:id' => 'users#show', as: 'user'
But after this, when I access /users/sign_in path, it try to find the user show page.
How to write the right route?
What you did will actually "override" the devise routes (and i think this is the problem you are facing)
If you want to add another route in the scope of devise routes, you have to do something like :
devise_scope :user do
get '/users/:id' => 'users#show'
end
after
devise_for :users
Let me know if it solves the problem !
I use devise for authentication. I have an admin user to play with the application. Is it possible to map the urls '/login' and '/admin' to the same login form?
Yes you can. Add the following to your routes.rb file, assuming your devise model is called 'admin':
devise_scope :admin do
get 'login' => 'devise/sessions#new'
get 'admin' => 'devise/sessions#new'
end
The normal '/admins/sign_in' route will still be available as well.