简体   繁体   English

将Rails + Devise用于API时处理电子邮件的最佳方法是什么

[英]What is the best way to handle emails when using Rails + Devise for an API

I have a Rails-based API that has successfully integrated Devise. 我有一个已成功集成Devise的基于Rails的API。 One remaining question I have is how to handle emails (confirmation and reset password). 我还有一个问题是如何处理电子邮件(确认和重置密码)。 Devise automatically sends out these emails with links to its views. Devise自动发送这些电子邮件以及指向其视图的链接。 Since the API shouldn't handle views, what's the best way to deal with this? 由于API不应该处理视图,因此最好的处理方法是什么? My questions are: 我的问题是:

  1. Is it inevitable that the API would contain some html templates since Devise sends out these emails? 自从Devise发送这些电子邮件以来,API是否必然包含一些html模板? Is there a way for these templates to be on the client side? 这些模板是否可以在客户端使用?
  2. How do I do with the links, which contains information like confirmation tokens and reset_password_tokens? 如何处理包含确认令牌和reset_password_tokens之类信息的链接? I am not well-versed in client-side tech but should the links point to the client side's domain so that it can capture, rip out the tokens and make the calls in the background? 我不熟悉客户端技术,但是链接是否应该指向客户端的域,以便它可以捕获,提取令牌并在后台进行呼叫?

Thank you. 谢谢。

I've used Devise for a few API-only Rails projects. 我已经将Devise用于一些仅API的Rails项目。 Frankly, IMHO, Devise is too heavy an opinionated for such projects. 坦率地说,恕我直言,Devise对此类项目的看法过于沉重。 I love devise, but there's a lot of magic and assumptions. 喜欢设计,但是有很多魔术和假设。 One of the assumptions is that you'll be using traditional Rails views. 假设之一是您将使用传统的Rails视图。 This isn't what you asked, but FWIW you might check out some other authentication libraries, like https://github.com/thoughtbot/clearance . 这不是您要的,但是FWIW您可能会签出其他一些身份验证库,例如https://github.com/thoughtbot/clearance

1) I don't see a way for your client app to have an email template. 1)我看不到您的客户端应用程序具有电子邮件模板的方法。 This action is happening entirely outside your client. 此操作完全在客户外部进行。 Rails is sending emails, which your users then view using their email client (I'm assuming your client app isn't actually an email client). Rails正在发送电子邮件,您的用户然后可以使用他们的电子邮件客户端查看这些电子邮件(我假设您的客户端应用实际上不是电子邮件客户端)。

I think the Rails api is the proper place for email templates in this case. 我认为在这种情况下,Rails api是电子邮件模板的适当位置。 I cannot think of a way that you could involve your client app in this. 我想不出一种可以让您的客户端应用程序参与其中的方法。 Even if you could, why would you want to do so? 即使可以,为什么还要这么做?

2) There are a few ways to handle the links to password resets. 2)有几种方法可以处理指向密码重置的链接。 The first thing you could do, as you state, is simply to grab the data and write your own views. 正如您所说的,您要做的第一件事就是简单地获取数据并编写您自己的视图。 But that seems like quite a lot of work, and you might end up fighting a lot with Devise. 但这似乎需要大量工作,而且您可能最终会与Devise争斗很多。

An easy solution is to just let Devise own this stuff, and use Rails views. 一个简单的解决方案是让Devise拥有这些东西,并使用Rails视图。 It's messy, but simple and easy. 杂乱无章,但简单易行。 Devise is very opinionated, and this is the path of least resistance. 设计很固执,这是阻力最小的途径。 It seems to me that you are trying to swim upstream here, based on a principle of separation of concerns. 在我看来,您是根据关注点分离的原则,试图在这里上游游泳。 I'd argue that using Devise makes it very difficult to do that. 我认为使用Devise很难做到这一点。

If you really must have your client app render all views, including ones for this feature, then you might consider just rolling your own password reset feature and keeping Devise out of the mix. 如果确实必须让客户端应用程序呈现所有视图(包括此功能的视图),那么您可以考虑仅滚动自己的密码重置功能并将Devise排除在外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 处理在Rails中发送许多电子邮件的最佳方法是什么? - What is the best way to handle sending many emails in Rails? 在 Rails 中验证多封电子邮件和处理错误的最佳方法是什么? - What's the best way to validate multiple emails and handle errors in Rails? 使用Rails时在Ruby中处理常量的最佳方法是什么? - What is the best way to handle constants in Ruby when using Rails? 用Rails / Devise处理具有多个作用域/角色的登录和注销的最佳方法是什么? - What's the best way to handle logging in and out with multiples scopes/roles with Rails/Devise? 使用Postmarkapp在Rails应用程序中处理电子邮件异常的最佳方法是什么? - What is the best way to handle Email exceptions in a Rails app using Postmarkapp? 使用Devise for Rails禁止/阻止用户的最佳方法是什么? - What is the best way to ban/block users with Devise for Rails? 使用Rails API,devise和emberjs对用户进行身份验证的最佳实践是什么? - What is the best practise to authenticate users with rails API, devise and emberjs? 在Rails 3中使用多个字段对电子邮件进行设计邀请 - Devise Invitable using multiple fields for emails in Rails 3 用身份验证在Rails中编写api的最佳方法是什么? - What is best way to write api in rails with authentication? 在Devise中使用受干扰的用户名时的Rails最佳实践 - Rails best practice when using a munged username with Devise
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM