Last active
December 20, 2015 00:09
-
-
Save jingkaihe/6039619 to your computer and use it in GitHub Desktop.
Ominauth callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
h2 Sign up | |
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| | |
= f.error_notification | |
.form-inputs | |
= f.input :email, :required => true, :autofocus => true | |
= f.input :username, :required => true | |
.form-actions | |
= f.button :submit, "Sign up" | |
/= render "devise/shared/links" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
h2 登陆 | |
= render "devise/shared/links" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
config.omniauth :github, '28a2b6dcbc0e8f9ff508', 'dcd93b0761c98ae14dd9bd1836df6e1502b2bcd4' | |
config.omniauth :weibo, "1840943522", "c6f4524f70db2e237e7a0cc015f2f176" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OmniauthCallbacksController < Devise::OmniauthCallbacksController | |
def all | |
user = User.from_omniauth request.env["omniauth.auth"] | |
if user.persisted? | |
# logger.warn "------------------->yes" | |
sign_in_and_redirect user, notice: "欢迎您登陆" | |
else | |
# logger.warn "------------------->no" | |
session['devise.user_attributes'] = user.attributes | |
redirect_to new_user_registration_url | |
end | |
end | |
alias_method :github, :all | |
alias_method :weibo, :all | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: users | |
# | |
# id :integer not null, primary key | |
# email :string(255) default(""), not null | |
# encrypted_password :string(255) default(""), not null | |
# reset_password_token :string(255) | |
# reset_password_sent_at :datetime | |
# remember_created_at :datetime | |
# sign_in_count :integer default(0) | |
# current_sign_in_at :datetime | |
# last_sign_in_at :datetime | |
# current_sign_in_ip :string(255) | |
# last_sign_in_ip :string(255) | |
# | |
class User < ActiveRecord::Base | |
devise :database_authenticatable, :registerable, :recoverable, | |
:rememberable, :trackable, :validatable, :omniauthable | |
attr_accessible :username, :encrypted_password, :email | |
validates :username, presence: true, uniqueness: true | |
def self.from_omniauth(auth) | |
where(auth.slice(:provider, :uid)).first_or_create do |user| | |
user.provider = auth.provider | |
user.uid = auth.uid | |
user.username = auth.info.nickname | |
user.email = auth.info.email | |
end | |
end | |
# Allocate the new session for the user | |
def self.new_with_session(params, session) | |
if session["devise.user_attributes"] | |
new(session["devise.user_attributes"]) do |user| | |
user.attributes = params | |
user.valid? | |
end | |
else | |
super | |
end | |
end | |
# Tell whether the user need a password. basically it is a overidden function. | |
def password_required? | |
false | |
end | |
def sign_up_params | |
params.require(:user).permit(:email, :username) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment