Answer the question
In order to leave comments, you need to log in
Too few arguments to work?
Got an error:
Too few arguments to function App\Http\Controllers\UserController::LoginWithVk(), 0 passed in /localhost/app/Http/Controllers/UserController.php on line 25 and exactly 1 expected
Route::get('/login/{type}', [App\Http\Controllers\UserController::class, 'login']);
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use romanzipp\Twitch\Enums\EventSubType;
use romanzipp\Twitch\Twitch;
use romanzipp\Twitch\Enums\GrantType;
class UserController extends Controller
{
public function login($type){
switch ($type) {
case "twitch":
$this->LoginWithTwitch();
break;
case "youtube":
$this->LoginWithYouTube();
break;
case "vk":
$this->LoginWithVk();
break;
default:
return redirect()->route('landing');
break;
}
}
public function LoginWithVk(Request $request){
if (empty($request->get('code'))) {
return $this->VKRedirectToLogin();
} else {
$token = $this->VKGetAuthToken($request->get('code'));
if (isset($token['access_token'])) {
$userInfo = $this->VKGetUserInfo($token);
if (isset($userInfo['response'][0]['id'])) {
$userInfo = $userInfo['response'][0];
$u = User::where('user_vk', $userInfo["id"])->first();
if(!($u)){
$data = ['user_email' => '', 'user_login' => 'vkid'.$userInfo["id"], 'user_login_show' => $userInfo['first_name'] . " " . $userInfo['last_name'], 'user_domain' => 'vkid'.$userInfo["id"], 'user_vk' => $userInfo["id"],
'user_group' => 1, 'user_wallets' => '{"qiwi":"null","webmoney":"null","yamoney":"null","bank":"null","paypal":null}', 'user_wallets_pay' => 0,
'user_paypal' => '{"clientid":"","secret":"","email":"","on":"0"}', 'user_stream_status' => 0, 'user_avatar' => $userInfo["photo_big"], 'user_donate_page' => '{"min_sum":"1","rec_sum":"50","text":"","fuck_filter":"0","fuck_name_filter":"0","fuck_words":"","bg_color":"#e0e0e0","bg_type":"1","bg_size":"auto","bg_image":"","bg_image_name":"","bg_repeat":"no-repeat","bg_position":"center","bg_header_type":"1","bg_header_image":"","bg_header_size":"auto","bg_header_repeat":"no-repeat","bg_header_position":"center","bg_header_color":"#f2f2f2","text_header_color":"#000000","btn_color":"#ff5400","btn_text_color":"#ffffff","comission":"0"}',
'user_reg_time' => now(), 'user_reg_ip' => $_SERVER['REMOTE_ADDR']];
$user = User::create($data);
$remember = true;
Auth::login($user, $remember);
return redirect()->route('dashboard');
} else {
$user = User::where('user_vk', $userInfo["id"])->first();
$user->update(['user_last_login_time' => now(), 'user_last_ip' => $_SERVER['REMOTE_ADDR']]);
$remember = true;
Auth::login($user, $remember);
return redirect()->route('dashboard');
}
}
}
}
}
/*VKAuth*/
private function VKRedirectToLogin()
{
$url = 'http://oauth.vk.com/authorize';
$params = array(
'client_id' => config('services.vkontakte.client_id'),
'redirect_uri' => config('services.vkontakte.redirect'),
'response_type' => 'code'
);
return redirect($url . '?' . urldecode(http_build_query($params)));
}
private function VKGetAuthToken($code)
{
$params = [
'client_id' => config('services.vkontakte.client_id'),
'client_secret' => config('services.vkontakte.client_secret'),
'code' => $code,
'redirect_uri' => config('services.vkontakte.redirect'),
];
return json_decode($this->get_curl('https://oauth.vk.com/access_token' . '?' . urldecode(http_build_query($params))), true);
}
private function VKGetUserInfo($token)
{
$params = [
'uids' => $token['user_id'],
'fields' => 'uid,first_name,last_name,photo_big,email',
'access_token' => $token['access_token'],
'v' => '5.103'
];
return $userInfo = json_decode($this->get_curl('https://api.vk.com/method/users.get' . '?' . urldecode(http_build_query($params))), true);
}
function get_curl($url)
{
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
return $output;
} else {
return file_get_contents($url);
}
}
/*EndVKAuth*/
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question