惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
V
V2EX
Jina AI
Jina AI
爱范儿
爱范儿
M
MIT News - Artificial intelligence
量子位
L
LangChain Blog
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
腾讯CDC
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss

Nic Lin's Blog

謝明真 - 高效領導力的課後筆記 NFT 開發實戰!基礎智能合約入門 (3) NFT 開發實戰!基礎智能合約入門 (2) NFT 開發實戰!基礎智能合約入門 (1) 如何自我檢測 log4j CVE 漏洞 Rails 如何在資料寫入時記錄來源 IP 位置 如何經營工程師 Youtube 頻道 - Part 8 營收篇 如何經營工程師 Youtube 頻道 - Part 7 酸民文化篇 如何經營工程師 Youtube 頻道 - Part 5 設備器材篇 如何經營工程師 Youtube 頻道 - Part 4 後製剪輯篇 如何經營工程師 Youtube 頻道 - Part 3 文案企劃篇 如何經營工程師 Youtube 頻道 - Part 2 設備器材篇 如何經營工程師 Youtube 頻道 - Part 1 制訂頻道方向篇 如何經營工程師 Youtube 頻道 - Part 0 Rails 中避免 race condition 的最佳實踐(二) Rails 中避免 race condition 的最佳實踐(一) 10 分鐘整合 google sheet 做自動化開發功能週報 經營 Side Project 300 天所帶來的收穫及挑戰 我的 Youtube 影片製作流程 API 設計時必須注意的 HTTP header 底線問題 如何提升你的程式可讀性之實務技巧(三) 如何提升你的程式可讀性之實務技巧(二) 如何提升你的程式可讀性之實務技巧(一) Ruby 中使用 freeze 優化效能的時機 避免 React 中的 useEffect 無限 render 在 Rails 內輕量使用 Vue Component 的最佳實踐 如何在區域網路用 Docker 架設有 SSL 的 Gitlab 從被問到問人,那些我常問的面試問題 [Rails] 如何漂亮寫出可維護的 query (Maintainable Rails Query) 在已知長度情況下優化 slice 的性能
Devise使用Google實作登入
Nic Lin · 2016-03-12 · via Nic Lin's Blog

Step1: 申請 Google API

啟用”Web Fonts Developer API” and “Google+ API”

前往 https://console.developers.google.com 申請一個 Web Fonts Developer API

然後千萬注意要將 Google+ API 這個服務也開啟 不然在最後都成功送 key出去後 還會回傳一個 “Authentication failure! invalid_credentials” 錯誤喔!! 新增一個憑證: OAuth 2.0 用戶端 ID 專案 選擇:網路應用程式 以及在 已授權的重新導向 URI 填入回傳導向的 URL 如果暫時是用本機端的server的話,就要打: http://localhost:3000/users/auth/google_oauth2/callback

Step2: 安裝Gem及前置設定

gem 'settingslogic' #管理我們的秘密key
gem 'omniauth-google-oauth2'

bundle install

新增檔案:

class Settings < Settingslogic
  source "#{Rails.root}/config/application.yml"
  namespace Rails.env
end

前往 config/application.yml 填入剛剛申請的 Google api key! 包含了 用戶端ID以及用戶端密碼

新增檔案:

defaults: &defaults
  app_name: "demotest"
  google_oauth2_app_id: "自己申請的key"
  google_oauth2_secret: "自己申請的key"

development:
  <<: *defaults
  domain: "http:/localhost:3000" #最後 "/" 要拿掉

test:
  <<: *defaults

production:
  <<: *defaults

為了安全起見,我們必須把含有key的檔案加入gitignore,避免push的時候連帶key給別人。

/config/application.yml

Step3: Rails/controller && model設定

新增欄位 rails g migration AddColumnsToUsers provider:string uid:string name:string 記得跑`rake db:migrate’ 之後重開 rails server

然後前往 新增以下:

config.omniauth :google_oauth2, Settings.google_oauth2_app_id, Settings.google_oauth2_secret, {access_type: "offline",prompt: "select_account consent"}

寫 Callback method

rails g controller Users::OmniauthCallbacks

前往:

app/controllers/users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController   
    
  def google_oauth2
     
    @user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)
 
    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.google_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

前往新增:


class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, :omniauth_providers => [:google_oauth2]
       
  def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
    data = access_token.info
    user = User.where(:provider => access_token.provider, :uid => access_token.uid ).first
    if user
      return user
    else
      registered_user = User.where(:email => access_token.info.email).first
      if registered_user
        return registered_user
      else
        user = User.create(name: data["name"],
          provider:access_token.provider,
          email: data["email"],
          uid: access_token.uid ,
          password: Devise.friendly_token[0,20]
        )
      end
    end
  end
  
end

Step4: Rails/routes && view設定

修改routes.rb,加入callback routes

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

在你view裡面加入google登入連結

<%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %>