使用Ionic登录fb的Rails API

时间:2021-09-19 20:53:15

I want to build a rails api for fb login using, ionic framework. Till now I've successfully created a web base application and it's successfully authenticating with Facebook.

我想使用离子框架为fb登录构建一个rails api。直到现在我已成功创建了一个网络基础应用程序,并且它已成功通过Facebook进行身份验证。

Now I want to do the same but with ionic based app.

现在我想用基于离子的应用程序做同样的事情。

Questions?

  1. Is this possible to write a rails api using omniauth and using the fb authentication api with ionic.
  2. 是否可以使用omniauth编写rails api并使用带有离子的fb身份验证api。

  3. If it's(1) is possible than what kind of json request is to be made or returned.
  4. 如果它是(1)可能比什么样的json请求被发出或返回。

Couldn't find anything over the web about it, nor any github project of rails with ionic (fb login).

无法通过网络找到任何关于它的内容,也没有任何关于rails with ionic(gb登录)的github项目。

My Current code:

我目前的代码:

Controller:

  class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id
    redirect_to root_url


  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url
  end
end

Model:

class User < ActiveRecord::Base
  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_initialize do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end
end

Initializers:

OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, "123", "abc"
end

1 个解决方案

#1


2  

I'm not sure if its possible... but I think you shouldn't use the same logic.

我不确定它是否可能......但我认为你不应该使用相同的逻辑。

Rails facebook omniauth flow is session based while Ionic apps are mostly "access token" based i.e you expose an Rails API with authentication.

Rails facebook omniauth flow是基于会话的,而Ionic应用程序主要是“访问令牌”,即您通过身份验证公开Rails API。

Also, in Ionic you have some advantages: you can access to native Facebook Login using Ionic Native Facebook Connect Plugin which provides a better user experience to users who has installed Facebook.

此外,在Ionic中您有一些优势:您可以使用Ionic Native Facebook Connect插件访问本机Facebook登录,这为已安装Facebook的用户提供了更好的用户体验。

I had the same problem and what I finishing doing was first understand how does authentication works for single page apps. For this, I recommend you to read "Authentication for single single page apps".

我有同样的问题,我完成的工作是首先了解身份验证如何适用于单页应用程序。为此,我建议您阅读“单个单页应用程序的身份验证”。

My final solution looks like this:

我的最终解决方案如下:

使用Ionic登录fb的Rails API

The tools I used:

我使用的工具:

You can see an example rails app for retrieving a valid JWT token given a valid facebook auth token at https://github.com/muZk/rails5-api-jwt-facebook-auth

您可以在https://github.com/muZk/rails5-api-jwt-facebook-auth上看到一个示例rails应用程序,用于检索有效的JWT令牌,并提供有效的facebook身份验证令牌

After creating the backend, the Ionic part should be something like this:

创建后端后,Ionic部分应该是这样的:

this.fb.login(['public_profile', 'user_friends', 'email'])
  .then((res: FacebookLoginResponse) => post('your-rails-backend.com/api/auth/facebook', { access_token: FacebookLoginResponse.accessToken }))
  .then(res => console.log('This is your JWT for your rails API:', res.accessToken))

I know this is not a copy-paste solution but I hope I have helped you a bit with your problem.

我知道这不是一个复制粘贴解决方案,但我希望我能帮助你解决一些问题。

#1


2  

I'm not sure if its possible... but I think you shouldn't use the same logic.

我不确定它是否可能......但我认为你不应该使用相同的逻辑。

Rails facebook omniauth flow is session based while Ionic apps are mostly "access token" based i.e you expose an Rails API with authentication.

Rails facebook omniauth flow是基于会话的,而Ionic应用程序主要是“访问令牌”,即您通过身份验证公开Rails API。

Also, in Ionic you have some advantages: you can access to native Facebook Login using Ionic Native Facebook Connect Plugin which provides a better user experience to users who has installed Facebook.

此外,在Ionic中您有一些优势:您可以使用Ionic Native Facebook Connect插件访问本机Facebook登录,这为已安装Facebook的用户提供了更好的用户体验。

I had the same problem and what I finishing doing was first understand how does authentication works for single page apps. For this, I recommend you to read "Authentication for single single page apps".

我有同样的问题,我完成的工作是首先了解身份验证如何适用于单页应用程序。为此,我建议您阅读“单个单页应用程序的身份验证”。

My final solution looks like this:

我的最终解决方案如下:

使用Ionic登录fb的Rails API

The tools I used:

我使用的工具:

You can see an example rails app for retrieving a valid JWT token given a valid facebook auth token at https://github.com/muZk/rails5-api-jwt-facebook-auth

您可以在https://github.com/muZk/rails5-api-jwt-facebook-auth上看到一个示例rails应用程序,用于检索有效的JWT令牌,并提供有效的facebook身份验证令牌

After creating the backend, the Ionic part should be something like this:

创建后端后,Ionic部分应该是这样的:

this.fb.login(['public_profile', 'user_friends', 'email'])
  .then((res: FacebookLoginResponse) => post('your-rails-backend.com/api/auth/facebook', { access_token: FacebookLoginResponse.accessToken }))
  .then(res => console.log('This is your JWT for your rails API:', res.accessToken))

I know this is not a copy-paste solution but I hope I have helped you a bit with your problem.

我知道这不是一个复制粘贴解决方案,但我希望我能帮助你解决一些问题。