Answer the question
In order to leave comments, you need to log in
Why does it redirect to home after pressing the login button, laravel + vue?
After I click the "Login" button on the site, I am redirected to the main page of the site, instead of further authorization through VK.
Button Skin:
<a v-if="$root.user === null" href="/auth/vkontakte" class="autorization-vk"><i class="fab fa-vk"></i></a>
Route::group(['prefix' => '/auth'], function () {
Route::post('/{provider}', '[email protected]');
Route::get('/{provider}/callback', '[email protected]');
});
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use Socialite;
use Illuminate\Http\Request;
class LoginController extends Controller
{
public function login($provider)
{
return Socialite::driver($provider)->redirect();
}
public function callback($provider)
{
$user = json_decode(json_encode(Socialite::driver($provider)->user()));
if (isset($user->returnUrl)) return redirect('/');
$user = $user->user;
$user = $this->createOrGetUser($user, $provider);
Auth::login($user, true);
return redirect()->intended('/');
}
public function createOrGetUser($user, $provider)
{
$existUser = User::where('network_id', $user->id)->first();
if ($provider == 'vkontakte') {
if ($existUser) {
$username = $user->first_name . ' ' . $user->last_name;
User::where('network_id', $user->id)->update([
'name' => $username,
'avatar' => $user->photo_200
]);
$user = $existUser;
} else {
$username = $user->first_name .' ' .$user->last_name;
$user = User::create([
'network' => 'vkontakte',
'network_id' => $user->id,
'name' => $username,
'avatar' => $user->photo_200,
'ref_code' => $this->randw(5)
]);
}
}
return $user;
}
}
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