Access denied, Please sign in and make sure you have proper permission.

Rails 在 Rails7 中使用 devise 的邮箱验证

imanner · March 01, 2022 · Last by testyhalftime replied at May 28, 2025 · 919 hits

本文特点:

  • 本地邮件服务器;
  • Rails7 避坑;
  • Dry。

安装 mailcatcher

# 全局 安装 mailcatcher
$ gem install mailcatcher

# 开启 mail server
$ mailcatcher
Starting MailCatcher v0.8.1
==> smtp://127.0.0.1:1025
==> http://127.0.0.1:1080
*** MailCatcher runs as a daemon by default. Go to the web interface to quit.

# 查看进程状态
$ ps -aux | grep mailcatcher

# 在浏览器 查看 mail server 状态
在浏览器打开 http://127.0.0.1:1080

创建新项目

$ rails new r7-devise-mail-confirm
$ cd r7-devise-mail-confirm
# 使用vscode 打开
$ code .

快速美化 UI

修改 全局 模板 [app/views/layouts/application.html.erb],添加样式!

<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">

安装 devise

# 添加到 gem
$ bundle add devise
# 安装 devise
$ rails g devise:install
# 创建要使用的模型
$ rails g devise user 

修改新生成的 migrate,去掉 Confirmable 块儿的注释

## Confirmable
t.string   :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string   :unconfirmed_email # Only if using reconfirmable

修改新生成的 migrate,去掉 confirmation_token 索引 的注释

add_index :users, :confirmation_token,   unique: true

执行 migrate

$ rails db:migrate

修改 user[app/models/user.rb] 模型,开启 devise 的 confirmable 功能

devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :confirmable

修改 devise 初始化配置 [config/initializers/devise.rb],添加 对 turbo_stream 的支持

config.navigational_formats = ['*/*', :html, :turbo_stream]

修改 devise 初始化配置 [config/initializers/devise.rb],确认网站的发信邮箱

config.mailer_sender = '[email protected]'

修改模板 [app/views/layouts/application.html.erb],提供通知功能

<body>
  <p><%= notice if notice %></p>
  <p><%= alert if alert %></p>
  <%= yield %>
</body>

配置 SMTP 信息

修改开发环境的配置文件:config/environments/development.rb

# Show full error reports.
config.consider_all_requests_local = true

# devise turbo
config.action_mailer.default_url_options = {
  host: 'localhost:3000',
}

config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
  address: '127.0.0.1',
  port: 1025
}
#####

重启服务器!!!!!

创建测试页

# 创建 articles#index
$ rails g controller articles index
# 修改路由
root "articles#index"

在 articles 的 index 中添加跳转到 devise 登录,注册,退出的链接

<% if current_user %>
    <%= button_to 'Sign Out', destroy_user_session_path, method: :delete %>
<% else %>
    <%= link_to "Sign Up", new_user_registration_path %>
    <br>
    <%= link_to "Login", new_user_session_path %>
<% end %>

测试

测试就不多说了,这里给出一点提示:

如果没有确认过邮箱登录会出现上面图片中红框位置的提示信息!

而 mailcatcher 中可以点击注册的连接:

大家可以自己试试“忘记密码”等其它功能!

Have fun, Enjoy yourself, ^ _ ^!

国内,一般这种邮箱验证没啥价值了,都是用手机多

刚刚发现这个,email verification, 2-step-auth, jwt, etc.... 一次满足

https://github.com/janko/rodauth-rails

我喜歡你博客的地方在於它始終如一的深思熟慮。你花時間探索每個主題的各個角度,並以既引人入勝又具教育意義的方式呈現出來。每一篇文章都感覺像是精心編排的,townscaper 我總是可以依賴你的內容提供有價值的東西。繼續加油!

Experience limitless creativity where there are no boundaries, no scores to chase, and solar smash no conditions to meet—just you and the power of your imagination unleashed.

You need to Sign in before reply, if you don't have an account, please Sign up first.