M
M
Matvey Slotvinsky2021-03-10 12:16:49
PHP
Matvey Slotvinsky, 2021-03-10 12:16:49

How to rewrite vk open api authorization check on NODE JS?

I welcome everyone.
The bottom line is the following: I decided to tie authorization to the site through VK, the server part is spinning on node js.
Everything is clear with the client part, but how to check the authorization on the server?
The VK documentation gives an example of a function in php, in fact it was possible to implement all this in php, but there is a problem on the node ...
If someone has a solution to this ailment, please share.

Here is the function that VK gives:

function authOpenAPIMember() {
    $session = array();
    $member = FALSE;
    $valid_keys = array('expire', 'mid', 'secret', 'sid', 'sig');
    $app_cookie = $_COOKIE['vk_app_'.APP_ID];
    if ($app_cookie) {
      $session_data = explode ('&', $app_cookie, 10);
      foreach ($session_data as $pair) {
        list($key, $value) = explode('=', $pair, 2);
        if (empty($key) || empty($value) || !in_array($key, $valid_keys)) {
          continue;
        }
        $session[$key] = $value;
      }
      foreach ($valid_keys as $key) {
        if (!isset($session[$key])) return $member;
      }
      ksort($session);
  
      $sign = '';
      foreach ($session as $key => $value) {
        if ($key != 'sig') {
          $sign .= ($key.'='.$value);
        }
      }
      $sign .= APP_SHARED_SECRET;
      $sign = md5($sign);
      if ($session['sig'] == $sign && $session['expire'] > time()) {
        $member = array(
          'id' => intval($session['mid']),
          'secret' => $session['secret'],
          'sid' => $session['sid']
        );
      }
    }
    return $member;
  }
  
  $member = authOpenAPIMember();
  
  if($member !== FALSE) {
    /* Пользователь авторизован в Open API */
  } else {
    /* Пользователь не авторизован в Open API */
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2021-03-10
@Zak0

www.passportjs.org/packages/passport-vkontakte

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question