Class: AuthController

Inherits:
ApplicationController
  • Object
show all
Defined in:
lib/generators/azure_auth/steps/templates/auth_controller.rb

Instance Method Summary collapse

Instance Method Details

#authenticateObject

redirect the user to peerialism authentication Hub



4
5
6
7
8
9
10
# File 'lib/generators/azure_auth/steps/templates/auth_controller.rb', line 4

def authenticate
  authenticatorURL = "https://peerialism.azurewebsites.net/"
  return_url = "#{root_url}azure_auth"
  query_params = (request.query_string.nil? || request.query_string.empty?) ? "" : "#{request.query_string}&"
  url = "#{authenticatorURL}?#{query_params}peerauth-return=#{return_url.encode}"
  redirect_to url
end

#azure_authObject

Handle the redirected user request after the authentication process



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/azure_auth/steps/templates/auth_controller.rb', line 13

def azure_auth
  if (email = authenticated?)
    email_pat = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
    unless email =~ email_pat
      flash[:alert] = "Unknown email: #{email}"
      redirect_to root_path
      return
    end
    user = User.where(:email => email).first
    unless user
      password = Devise.friendly_token[0,8]
      user = User.create(
                         email:email,
                         password:password,
                         password_confirmation:password
                         )
    end
    
    if user.email_confirmed?
       user, :event => :authentication
    else
      redirect_to root_path, :notice => I18n.t(:conf_msg, :scope => [:messages, :controllers, :invitations])
    end
  else
    flash[:alert] = "No email found."
    redirect_to root_path
  end
end