T145
T145

Reputation: 1523

How do I get a logged in user's name through omniauth-google-apps?

Currently I work a good bit w/ Ruby and use the Google app Omniauth implementation for security purposes. I want to have a bit of a vestigial feature that shows the user who is currently logged in to the site. The only thing I can't figure out is how to get the currently logged in user's name returned and displayed.

I do not use this technique: http://matt.aimonetti.net/posts/2013/01/30/omniauth-and-google-apps/

My technique for login checking is one simple statement:

Rails.env.development? ?
    authorize({email: '[email protected]', name: 'dev'}) :
    unless logged_in?
      redirect_to '/auth/admin'
    end

P.S. Using the "unless" declaration as a modifier returns a syntax error on runtime, so that's why I have it the way it is.

Any help is greatly appreciated!

Upvotes: 0

Views: 428

Answers (1)

Olly
Olly

Reputation: 7758

When you define your routes you will have pointed the Google Apps callback at a specific controller action, for example:

post '/auth/google_apps/callback', to: 'sessions#create'

In this example, as the final part of the the OAuth process, SessionsController#create will be called.

In that action you will be able to query the request parameters that OmniAuth populates in env[omniauth.auth]. It's in this hash where you'll find the first name and last name of the user:

email      = env['omniauth.auth']['info']['email']
first_name = env['omniauth.auth']['info']['first_name']
last_name  = env['omniauth.auth']['info']['last_name']

There is a really useful Railscast on this topic that I'd recommend.

http://railscasts.com/episodes/241-simple-omniauth

Upvotes: 1

Related Questions