How do i access the data returned form ruby on rails omniauth-twitter gem after authentication

61 views Asked by At

I have a separate registration and login system set up. I am building an app with omniauth2 with rails 6 to let users schedule tweets to post. I am using omniauth2 so users can authorize the application to access their accounts

I want to get the users username, and profile picture from the omniauth2 data, but it is not showing up as anything when the user authorizes, I will post the relevant routes, controller, and views code

nil shows up in the database for the categories i want to display

console output


irb(main):002:0> User.last.twitter_accounts


  id: 6,

  user_id: 7,

  name: nil,

  username: nil,

  image: nil,

  token: "[FILTERED]",

  secret: "[FILTERED]",

  created_at: Mon, 06 Dec 2021 01:56:09.804716000 UTC +00:00,

  updated_at: Mon, 06 Dec 2021 01:56:09.804716000 UTC +00:00

routes file

Rails.application.routes.draw do
    get "about", to: "about#index", as: :about


    get "password", to: "passwords#edit", as: :edit_password
    patch "password", to: "passwords#update"

    get "sign_up", to: "registrations#new"
    post "sign_up", to: "registrations#create"

    get "sign_in", to: "sessions#new"
    post "sign_in", to: "sessions#create"

    delete "logout", to: "sessions#destroy"

    get "password/reset", to: "password_resets#new"
    post "password/reset", to: "password_resets#create"
    get "password/reset/edit", to: "password_resets#edit"
    post "password/reset/edit", to: "password_resets#update"

    get "/auth/twitter/callback", to: "omniauth_callbacks#twitter"

    resources :twitter_accounts

    root to: "main#index"
end

controller file

class OmniauthCallbacksController < ApplicationController
    def twitter
        twitter_account = Current.user.twitter_accounts.where(username: auth.info.nickname).first_or_initialize
        
        twitter_account.update(
            name: auth.info.name,
            image: auth.info.image,
            token: auth.credentials.token,
            secret: auth.credentials.secret,
        )

        redirect_to twitter_accounts_path, notice: "Twitter account was successfully connected"
    end

    def auth
        request.env["omniauth.auth"]
    end
end

views file

```

Twitter Account

<% @twitter_accounts.each do |twitter_account| %>


0

There are 0 answers