简体   繁体   English

使用 Ruby on Rails API 和 React 登录 Google

[英]Login with Google, using a Ruby on Rails API and React

I'm current trying to implement a login with google as a option to make easier for the user to login and create user.我目前正在尝试使用 google 实现登录作为一个选项,以便用户更轻松地登录和创建用户。

I have a Rails API that uses devise to login, and a front using React that makes requests to that Rails 5 API.我有一个使用 devise 登录的 Rails API,以及一个使用 React 向 Rails 5 API 发出请求的前端。

Im not sure if I have to do all this login on back end or at front end, my greatest problem is create a user when login with google, because I cannot create a user without a password.我不确定我是否必须在后端或前端进行所有这些登录,我最大的问题是在使用 google 登录时创建用户,因为没有密码我无法创建用户。

I never created this kinda of login system so I'm not sure how works, I believe I have to implement all in back end, using the google oauth gem, but I wanna know if someone has tips how to do it.我从来没有创建过这种登录系统,所以我不确定它是如何工作的,我相信我必须在后端实现所有功能,使用 google oauth gem,但我想知道是否有人有如何做的提示。

Should I use react components only?我应该只使用反应组件吗? Rails gems only?只有 Rails 宝石? or both is the correct answer?或者两者都是正确答案?

Thank you.谢谢。

I could give you the direction to this.我可以给你方向。

First thing, you have to figure out which grant flow you are going to use in OAuth provider.首先,您必须弄清楚您将在 OAuth 提供程序中使用哪个授权流程。

There are four ways to do it, I only list the most common two ways.有四种方式,我只列出最常用的两种方式。

  1. Code Grant Flow (Used in website, omniauth-google or similar gem using this way)代码授予流程(在网站、omniauth-google 或使用这种方式的类似 gem 中使用)

  2. Implicit Grant Flow (Native app, it means you have to do the OAuth flow in front-end)隐式授权流程(本机应用程序,这意味着您必须在前端执行 OAuth 流程)

If you are going to implement API server, you should consider the second way.如果你要实现 API 服务器,你应该考虑第二种方式。

Because API server should be stateless, the first way need session to save the state of OAuth flow.因为 API server 应该是无状态的,所以第一种方式需要 session 来保存 OAuth 流的状态。

Besides, devise is not a good idea to implement API server, you should consider jwt or something token-based authentication.此外, devise不是实现 API 服务器的好主意,您应该考虑jwt或基于令牌的身份验证。

Although, you could enable session store within rails API to use devise or omniauth-series .虽然,你可以使轨API内使用会话存储deviseomniauth-series

If you want to do all the OAuth in front-end, go with the second way.如果您想在前端完成所有 OAuth,请使用第二种方式。

Or you could simply add button link to your OAuth callback url in your react component, then your web server will deal with your OAuth flow(This is the first way).或者您可以简单地将按钮链接添加到您的反应组件中的 OAuth 回调 url,然后您的 Web 服务器将处理您的 OAuth 流程(这是第一种方式)。

After considering your situation, I suggest you use the first way.在考虑了你的情况后,我建议你使用第一种方式。

Because there are many resource about devise and omniauth-series you could find on internet.因为你可以在互联网上找到很多关于deviseomniauth-series资源。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM