Hello. I have a devise gem installed, I want to, after authorization on the devise gem, I can log in via vk. Please help with the implementation, especially with the routing. Skype is welcome. Thank you very much in advance!
1 answer
Actually, everything is about the same as with Facebook, following the example of the Devise wiki .
Inside it works quite simply:
- You follow the link from your site, it redirects to VC.
- VK asks for your permission, then redirects you back to the site, adding an access code to the link.
- The site uses the access code to get the requested data from the provider.
In the center of everything: the user allows the site to access its information. And this information gives OmniAuth a pair of id providers, sufficient for authentication.
To ensure all this, you need (in a fresh project):
- Add
deviseandomniauth-vkontaktetoGemfile, install. - Install Devise:
rails g devise:installand edit the createddevise.rb:- At this stage, you will need to enter data about your application , which serves as a "representation" of your site in the VC. You will need an "application ID" and "secure key". Closer to the bottom of
devise.rbis an example of specifying the OmniAuth provider forgithub: you need to write a similar line forvkontakte. - Keep in mind that the key and application ID should not be published, VC considers that only you know them, so if you publish the source code, pull this data from the outside using solutions like
figaroorconfig, as I did here .
- At this stage, you will need to enter data about your application , which serves as a "representation" of your site in the VC. You will need an "application ID" and "secure key". Closer to the bottom of
- Create a
Usermodel and add theprovideranduidfields there, plus make changes to the model file itself:rails g devise User uid providerwill do the necessary migration, but you should double-check it for fields you do not need.- Add the
deviseto the arguments in the model:omniauthable, omniauth_providers: [:vkontakte]
Almost all! It remains to make a return point from VC to the site, which will accept the access code and retrieve the necessary data with its help.
- Create a new controller that inherits from
Devise::OmniauthCallbacksController - Make there a
vkontaktemethod Go to the routes (
routes.rb) and specify this controller there:devise_for :users, # <- внимание, запятая, вызов ещё не кончился controllers: { omniauth_callbacks: "users/omniauth_callbacks" } # У меня контроллер Users::OmniauthCallbacksController
- Create a new controller that inherits from
Done! Yes seriously. True, in this form there is no practical benefit from this, but you have everything you need:
- In the
vkontaktemethod added above, all the information coming from the VC is available atrequest.env["omniauth.auth"]- you probably want to do / find the user with this information and log in for it (and you can be sure that this information is correct). - The whole process from the user’s side is initiated by navigating to the address generated by the user_omniauth_authorize_path
user_omniauth_authorize_path(:vkontakte), which is reasonable to pack into a link and show only to guests.
Then you should do some cleaning (isolate application data from the source code, for example) and "failures processing" (what if the user refuses to give data? ).
Here is a sketched hastily example on a githaba . The vkontakte method in it does nothing but call the debugger session, allowing you to inspect the context in which the program finds itself. This, in general, is enough to make sure that it works.
- Thank you so much for such a detailed guide! I will try and I am sure that there will be questions arising in the process. Do not say goodbye :) - basania
- Where to get application ID and secure key? - basania
- @basania in the control panel for developers in VK. - D-side
- Thank. If I already have a similar migration, but not the one I need, and from that migration, the fields got into schema.rb. How to remove these fields from schema.rb without consequences? - basania
- @basania schema.rb is only a copy of the state of the database and does not necessarily reflect the consequences of the application of all migrations (although in a good way, it should). You can do quite a lot in migration. - D-side
omniauth-vkontakteset? - D-side