Add avatar and name to OAuth

This commit is contained in:
Max Wofford 2025-02-16 12:26:22 -05:00
parent 394339fcf6
commit 5f6b322ebe
5 changed files with 20 additions and 4 deletions

View file

@ -8,3 +8,8 @@
*
* Consider organizing styles into separate files for maintainability.
*/
.avatar {
border-radius: 50%;
}

View file

@ -1,6 +1,7 @@
class User < ApplicationRecord
validates :email, presence: true, uniqueness: true
validates :slack_uid, presence: true, uniqueness: true
validates :username, presence: true
def self.authorize_url(redirect_uri)
params = {
@ -8,7 +9,7 @@ class User < ApplicationRecord
# scope: "identity.basic,identity.email",
redirect_uri: redirect_uri,
state: SecureRandom.hex(24),
user_scope: "identity.basic,identity.email"
user_scope: "identity.basic,identity.email,identity.avatar"
}
r = "https://slack.com/oauth/v2/authorize?#{params.to_query}"
@ -39,6 +40,8 @@ class User < ApplicationRecord
user = find_or_initialize_by(slack_uid: user_data["user"]["id"])
user.email = user_data["user"]["email"]
user.username = user_data["user"]["name"]
user.avatar_url = user_data["user"]["image_192"] || user_data["user"]["image_72"]
user.save!
user
rescue => e

View file

@ -26,8 +26,8 @@
<div class="header">
<% if current_user %>
<div class="user-info">
<%# image_tag current_user.image if current_user.image %>
<%# current_user.name %>
<%= image_tag current_user.avatar_url, size: "32x32", class: "avatar" if current_user.avatar_url %>
<%= current_user.username if current_user.username %>
<%= link_to "Sign Out", signout_path, method: :delete, data: { "turbo-method": :delete } %>
</div>
<% else %>

View file

@ -0,0 +1,6 @@
class AddProfileFieldsToUsers < ActiveRecord::Migration[8.0]
def change
add_column :users, :username, :string
add_column :users, :avatar_url, :string
end
end

4
db/schema.rb generated
View file

@ -10,12 +10,14 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2024_01_01_000000) do
ActiveRecord::Schema[8.0].define(version: 2024_03_20_000000) do
create_table "users", force: :cascade do |t|
t.string "slack_uid", null: false
t.string "email", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.string "avatar_url"
t.index ["slack_uid"], name: "index_users_on_slack_uid", unique: true
end
end