#My Problem in Socialite for login with google

2 messages · Page 1 of 1 (latest)

ebon thunder
#

this is my controller (``` <?php

namespace App\Http\Controllers;

// use Exception;

use App\Models\User;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use GuzzleHttp\Promise\Create;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Laravel\Socialite\Facades\Socialite;

class SocialiteController extends Controller
{
/**
* Display a listing of the resource.
*/
public function redirect()
{
return Socialite::driver("google")->redirect();
}

/**
 * Show the form for creating a new resource.
 */
public function callback()
{
    $googleUser = Socialite::driver("google")->user();

    $user = User::updateOrCreate(
        ['google_id' => $googleUser->id],
        [
            'name' => $googleUser->name,
            'email' => $googleUser->email,
            'password' => Str::password(12),
            'email_verified_at' => now()
        ]
        );

    Auth::login($user);

    return redirect(config("user.dashboard.index"). "/user/home");

    
    
} ```)
#

this is my model User (``` <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */

/**
 * The attributes that are mass assignable.
 *
 * @var list<string>
 */
protected $fillable = [
    'name',
    'email',
    'password',
    'role',
    'google_id',
    'email_verified_at'
];

/**
 * The attributes that should be hidden for serialization.
 *
 * @var list<string>
 */
protected $hidden = [
    'password',
    'remember_token',
];

/**
 * Get the attributes that should be cast.
 *
 * @return array<string, string>
 */
protected function casts(): array
{
    return [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
    ];
}

public function user()
{
    return $this->belongsTo(Role::class , 'role_id' , 'id');
}

}