Authorization set up for Rails app with VK - ruby-on-rails

I have application which works on Rails. I am using devise and omniauth-vkontakte.
My app can authorize user with vk.com but after user has passed authorization my navigation menu disappears.
Navigation menu code is in views/layouts/application.html.erb:
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="authentication">
<% if user_signed_in? %>
<span>Hello, <%= current_user.email %></span>
<%= button_to 'Exit', destroy_user_session_path, {:method => :delete} %>
<% else %>
<%= button_to 'sign up', new_user_registration_path,
{class: 'btn btn-default navbar-btn btn-info', id: 'sign-up-link', :method => :get} %>
<%= button_to 'sign in', new_user_session_path, {class: 'btn btn-default navbar-btn btn-info', id: 'sign-in-btn'} %>
<% end %>
</div>
</div>
</nav>
And I have one more trouble. How can I do that when user has passed authorization via vkontakte there will be a exit button that redirects to the main page?
Now it works like this: user passes auth with vkontankte and I have this message:
Users::OmniauthCallbacks#vkontakte
Find me in app/views/users/omniauth_callbacks/vkontakte.html.haml
Code in app/views/users/omniauth_callbacks/vkontakte.haml.html:
%h1 Users::OmniauthCallbacks#vkontakte
%p Find me in app/views/users/omniauth_callbacks/vkontakte.html.haml
Code in app/controllers/users/omniauth_callbacks_controller.rb:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def vkontakte
end
end

After authorization it redirects you to def vkontakte end method and you see it view.
So you can add some modification to this controller, something like redirect_to root_path. I don't now how you build your system but here example how you could implement it: link (on Russian, but you can look at code)

Related

Chaning image-button to text-button using Devise login

I have it currently so someone can click on the login button and when somebody signs in it changes that to logout. How would I be able to make it so it sets the button as the user's avatar that they have uploaded (or default one) when they login and then when they logout it changes back to "sign in"?
<div class="dropdown">
<button class="button-login">
<% if user_signed_in? %>
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "Login", new_user_session_path %>
<% end %>
</button>
<div class="dropdown-content">
<p>
<% if user_signed_in? %>
<%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %>
<% end %>
</div>
I believe you can do this by using a block:
<%= link_to destroy_user_session_path, method: :delete do %>
<%= image_tag("avatar.jpg", :alt => "user avatar image") %>
<% end %>

Extra blank <a></a> showing up

When I go to my menu to sign in it works perfectly fine and there is nothing extra but when I am signed in there is an extra blank at the top. How can I fix this?
Server Side html.erb
<div id="myDropdown" class="dropdown-content">
<% if user_signed_in? %>
<a><%= link_to('My Account', edit_user_registration_path) %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to('Log in', new_user_session_path) %>
<%= link_to('Sign up', new_user_registration_path) %></a>
<% end %>
</div>
Client Side Html (Logged In)
<a></a>
My Account
<a rel="nofollow" data-method="delete" href="/users/sign_out">Sign out</a>
What it looks like
I think you don't have to write explicit tag
<%= link_to "Home", root_path %>
# => Home
So
<%= link_to('My Account', edit_user_registration_path %>
# => My Account
To remove the extra href tag one, just remove the tag and its closing from your code. and it will render the view correctly.
More Information about link_to helper method

Bootstrap Sass broke devise

Hi I hope someone can help. I i'm building a news application on rails. I installed devise and act-as-votable for posts. I then installed bootstrap-sass (twbs/bootstrap-sass) and devise wont render to the screen.
<% if user_signed_in? %>
<div class="index_log">
Hey, your logged in as <strong><%= current_user.email %></strong>.
<% if user_signed_in? %>
<%= button_to "Log Out", destroy_user_session_path, method: :delete, class: 'btn btn-default navbar-btn' %>
<% else %>
<%= link_to "Log In", new_user_session_path, class: 'btn btn-default navbar-btn', method: :get %>
<% end %>
When I go go to the login page, enter my username and password, nothing happens.
Or else I create a new user, again nothing happens.
Any Ideas - I would add code below but I'm not sure what to add.
Looks like you have an extra if user_signed_in? tag
Hey, your logged in as <strong><%= current_user.email %></strong>.
<% if user_signed_in? %>
and that is messing up your <% else %> (which now matches the extra if and is now nested within the first if user_signed_in? and will never be rendered.
Also, unrelated, but it should be "...you're logged in as..." and the div class="index_log" isn't closed

How can I get the 'Sign Up' link in the header to disappear after the user logs in

enter image description here
I am developing my first RAILS application whilst using the Devise GEM for authentication.
I would like users who have Signed Up already and are subsequently Signed In to my application (to have the "Signed Up' link in the header not be made available to them).
Can someone kindly guide me as to how I can modify my attached syntax to provide this.
Thank you in advance of your help.
enter image description here
Using the Devise gem for user authentication, you can verify if a user is signed in using the user_signed_in? helper method:
<% if user_signed_in? %>
<%= button_to "SIGN OUT", destroy_user_session_path, :method => :delete %>
<% else %>
<%= button_to "SIGN IN", new_user_session_path, :method => "get" %>
<%end %>
Here you are checking if the user_signed_in? return true and if so, you will show the sign out path to destroy a user's session.
From the link you provided (http://i.stack.imgur.com/ZZU3P.png) your problem is that you did not wrap the <%= link_to "signup", new_user_session_path %> into the conditional statement.
So to solve your problem you should do this:
<% if user_signed_in? %>
<%= link_to "logout", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "signup", new_user_registration_path %>
<%= link_to "signin", new_user_session_path %>
<%end%>

Best way to display something based on if user logged in or not

I have an if statement that displays certain link based if a user voted or not for something. However, I'll get an error if the user is not logged in since current_user will be nil. I'm using devise for authentication.
undefined method `voted_as_when_voted_for' for nil:NilClass
<% if current_user.voted_as_when_voted_for #pin %>
<%= link_to dislike_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span> <%= #pin.get_upvotes.size %>
<% end %>
<% else %>
<%= link_to like_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
<%= #pin.get_upvotes.size %>
<% end %>
<% end %>
<%= link_to pins_path, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-step-backward"></span>
Back
<% end %>
<% if user_signed_in? %>
<%= link_to "Edit", edit_pin_path, class: "btn btn-default" %>
<%= link_to "Delete", pin_path, method: :delete, data: { confirm: "Are you sure"}, class: "btn btn-default" %>
<% end %>
What can I add to that if statement to display one of the links even if the user is not logged in without repeating link_to code. Using DRY Rails methodology. (if a user is not logged in and clicks like/dislike link, it will ask him to log in since I have my routes set up like this:
before_action :authenticate_user!, except: [:index, :show]
Try this:
<% if current_user && current_user.voted_as_when_voted_for #pin %>
... code here
<% else %>
<%= link_to like_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
<%= #pin.get_upvotes.size %>
<% end %>
<% end %>
In your case:
<% if signed_in? && current_user.voted_as_when_voted_for #pin %>
...
<% end %>
Assuming you're using devise since you included the authenticate_user! method.

Resources