Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class OpenIdController < ApplicationController
- open_id_consumer :required => [:email, :nickname]
- def begin
- # If the URL was unusable (either because of network conditions,
- # a server error, or that the response returned was not an OpenID
- # identity page), the library will return HTTP_FAILURE or PARSE_ERROR.
- # Let the user know that the URL is unusable.
- case open_id_response.status
- when OpenID::SUCCESS
- # The URL was a valid identity URL. Now we just need to send a redirect
- # to the server using the redirect_url the library created for us.
- # redirect to the server
- redirect_to open_id_response.redirect_url((request.protocol + request.host_with_port + "/"), url_for(:action => 'complete'))
- else
- flash[:error] = "Unable to find openid server for <q>#{params[:openid_url]}</q>"
- render :template => 'account/index'
- end
- end
- def complete
- case open_id_response.status
- when OpenID::FAILURE
- # In the case of failure, if info is non-nil, it is the
- # URL that we were verifying. We include it in the error
- # message to help the user figure out what happened.
- if open_id_response.identity_url
- flash[:message] = "Verification of #{open_id_response.identity_url} failed. "
- else
- flash[:message] = "Verification failed. "
- end
- flash[:message] += open_id_response.msg.to_s
- when OpenID::SUCCESS
- # Success means that the transaction completed without
- # error. If info is nil, it means that the user cancelled
- # the verification.
- flash[:message] = "You have successfully verified #{open_id_response.identity_url} as your identity."
- if open_id_fields.any?
- @user = User.find_by_identity_url(open_id_response.identity_url)
- @user ||= User.new(:identity_url => open_id_response.identity_url)
- @user.login = open_id_fields['openid.sreg.nickname'] if open_id_fields['openid.sreg.nickname']
- @user.email = open_id_fields['openid.sreg.email'] if open_id_fields['openid.sreg.email']
- if @user.save
- self.current_user = @user
- wipe_session_cache
- flash[:notice] = "Logged in successfully"
- redirect_back_or_default(:controller => 'articles', :action => 'index')
- else
- render :template => 'account/signup'
- end
- end
- when OpenID::CANCEL
- flash[:message] = "Verification cancelled."
- else
- flash[:message] = "Unknown response status: #{open_id_response.status}"
- end
- redirect_to :controller => 'account', :action => 'signup' unless performed?
- end
- end
Add Comment
Please, Sign In to add comment