Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

David Boureau
David Boureau

Posted on • Edited on • Originally published atalsohelp.com

     

Generate authentication like Rails 8 will

article originally published here :https://alsohelp.com/blog/generate-authentication-like-rails-8-will/

In the need for authenticator

Rails comes with no default way to authenticate the user, like Laravel does in the PHP world.

For a long time, the Devise gem was the good-enough-way-to-go for Rails, but didn't reach 100% of adoption, for longly reddit-debated reasons.

From DHH :

We can teach Rails developers how to use the basic blocks [of authentication] by adding a basic authentication generator that essentially works as a scaffold, but for authentication.
{: .prompt-info }

The closest authentication generator for Rails 7

Theauthentication-zero gem is the closest solution so far.

I used it. I found it very enjoyable, very few lines of code, very easy to customize, full test suite to ensure that all my customisations don't generate any kind of regression. Finally, I added some custom turbo_stream on top of validation to ensure a top-notch user experience.

The tutorial here is a simplified version of my current use.

Prerequisites

For this tutorial you will need :

ruby-v# 3.3.0rails-v# 7.1.3bundle-v# 2.4.10node-v# 20.9.0git--version# 2.34.1
Enter fullscreen modeExit fullscreen mode

Build a default Rails app

Create a new Rails app like this :

rails new myappcdmyapp
Enter fullscreen modeExit fullscreen mode

So nothing fancy here, no--minimal option or whatsoever. Stick with default is sometimes the best way to ensure more integration and less bugs.

Add authentication-zero

So we follow now the official docs of the gem and add :

bundle add authentication-zerobin/rails generate authentication
Enter fullscreen modeExit fullscreen mode

You now have routes, controllers, models, migrations, tests, etc.

Ensure the whole test suite pass

bin/railstest:all
Enter fullscreen modeExit fullscreen mode

If everything is green, you can go to the next step :)

Add letter_opener gem

You need to add a way to view the sent email on your local machine, in order to play with the confirmation email (for example).

In order to do so, add in the Gemfile :

# inside Gemfilegem"letter_opener",group: :development
Enter fullscreen modeExit fullscreen mode

and run

bundleinstall
Enter fullscreen modeExit fullscreen mode

Then insideconfig/environments/development.rb add

config.action_mailer.delivery_method=:letter_openerconfig.action_mailer.perform_deliveries=true
Enter fullscreen modeExit fullscreen mode

Play with the application

By default, there are no users in the development database.

A first option is to add this inside the seed.rb file,

User.create(email:"simple@user.com",password_digest:BCrypt::Password.create("Secret1*3*5*"),verified:true)
Enter fullscreen modeExit fullscreen mode

And run

bin/rails db:seed
Enter fullscreen modeExit fullscreen mode

And relaunch your local web server with

bin/rails server
Enter fullscreen modeExit fullscreen mode

Now you can connect with the user described inside the seed file.

But that was cheating, right ?

A second option is to play with the application, first go to the home page, then click on "sign up", then fill with one easy-to-remember email and password combination.

Then go to localhost:3000/letter_opener, and click the validation link.

Great! You now have a new verified user (who said "customer"? Not yet ;) )

Take time to read code

Don't be too shy here to investigate the source code. The beauty of this gem is that there are no tons of complicated functions to read. Trying to understandDevise orRodauth is another story.

Start with routes.rb, then try to play with the application, and try to understand what each unit test does and why.

Now you have a full authentication system that you fully understand!

Conclusion

I guess this article will be deprecated, as soon as the Rails 8 authenticator will be on scene.

Waiting for this, we have a clean, elegant and customisable solution : the authentication-zero gem.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Rails & JS dev. Build products.
  • Location
    Nantes, France.
  • Joined

More fromDavid Boureau

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp