Reputation: 131
I am trying to build a simple blog, and all of the rails tutorials(I've looked at somewhere around 15 tutorials) show how you can use Devise and allow many users to log into their account and create, update and delete posts.
I want to use Devise for learning purposes, and I want to just have one single user, which will be me, to gain access to links and urls that allow create, edit and destroying posts.
I found the webpage linked below which is from the people who made Devise and I think that this is the solution to my problem but I'm not sure.
https://github.com/plataformatec/devise/wiki/How-To:-Set-up-devise-as-a-single-user-system
How can I achieve this using Devise?
Upvotes: 1
Views: 702
Reputation: 3398
You can do this on your application controller:
before_action :authenticate_user!
So all your crud controllers will inherit it from application controller and require the user log in before any action. Also, since you're gonna have only one user, don't think you'll need an user management page, or something like that. You could create that user manually on rails console.
Also, devise have a setup on your user model, where you insert the behaviors you allow to your login/signup. Something like:
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
(see all possible configurations on https://github.com/plataformatec/devise).
So, in your case, you can remove :registerable
, for example, so devise won't allow visit users to sign up to your system (that way, only users created manually on the console can log in).
Also, those configurations reflect directly on devise views (so it won't have a "sign up" link once you remove the registerable option). But you can also override the views and do it like you want. For generating those views, just run:
rails generate devise:views
Devise is a pretty good authentication gem and may have everything you want. Take a good look on documentation.
Hope this helps!
Upvotes: 1