Hi Yishai,
Thanks for writing in!
The omniauth framework has a common way to retrieve the results of a successful authentication sequence.
In whatever controller action you use to complete the authentication flow, you can grab the current state of authentication by retrieving a hash from the request environment:
@omniauth = request.env['omniauth.auth'].to_hash
This hash is going to look something like this, and includes details omniauth found by requesting /me:
{
"provider" => "clever",
"uid" => "551333766c0a330e003f4b0c",
"info" => {
"user_type" => "teacher",
"id" => "551333766c0a330e003f4b0c",
"district" => "551331b0ed3308010000030f",
"type" => "teacher",
"name" => nil
},
"credentials" => {
"token" => "BEARER_TOKEN_STRING",
"expires" => false
},
"extra" => {
"raw_info" => {
"type" => "teacher",
"data" => {
"id" => "551333766c0a330e003f4b0c",
"district" => "551331b0ed3308010000030f",
"type" => "teacher"
},
"links" => [
[0] { "rel" => "self", "uri" => "/me" },
[1] { "rel" => "canonical", "uri" => "/v1.1/teachers/551333766c0a330e003f4b0c" },
[2] { "rel" => "district", "uri" => "/v1.1/districts/551331b0ed3308010000030f" }
]
}
}
}
From this point, you have a few choices on what you might want to do. You may want to store the bearer token string and clever ID and user type in your database. You may want to just put this information in the session.
clever_user_id = @omniauth["uid"] # => "551331b0ed3308010000030f"
clever_user_type = @omniauth["info"]["user_type"] # => "teacher"
clever_bearer_token = @omniauth["credentials"]["token"] # => "BEARER_TOKEN_STRING"
clever_district_id = @omniauth["info"]["district"] # => "551331b0ed3308010000030f"
To make the next API call, in this case to /v1.1/teachers/551331b0ed3308010000030f, I need to use the clever_bearer_token value.
Omniauth doesn't provide a general purpose HTTP client for you, which leaves you to choose the one you're already familiar with or appeals to you.
if teacher_response.code == 200
json_response = JSON.parse(teacher_response.body)
teacher_name = json_response["data"]["name"]
puts "Hello, #{teacher_name.first} #{teacher_name.last}!"
else
# Couldn't retrieve teacher record...
end
I hope this is helpful!
Taylor Singletary
Clever developer relations