Halaman login laravel dengan tambahan gmail register

Dari Dokumentasi Robie
Loncat ke navigasi Loncat ke pencarian

Setelah laravel 12 terinstall

ikuti langkah-langkah berikut untuk membuat halaman login

Masuk direktori dimana laravel 12 terinstall, lalu ketik perintah

composer require laravel/breeze

setelah itu

php artisan breeze:install lalu pilih blade

Ubah Tabel Users dan Model User

Tabel User (databases/migration/.....user)

Migration-user.jpg

Model User (app/Model/User.php)

Model-user.jpg

Lalu ketikan perintah

php artisan migrate

jika berhasil maka akan muncul tampilan seperti berikut

Perintah-php-artisan-migrate-laravel-12.jpg

Install Socialite Package

Jalankan perintah

composer require laravel/socialite

info packages yang terinstall akan muncul seperti berikut

Info-package-yang-terinstall.jpg

Buat Controller SocialController

php artisan make:controller SocialController


Tambahkan kode berikut di SocialController.php

Import Kelas

use App\Models\User;

use Illuminate\Support\Facades\Auth;

use Laravel\Socialite\Facades\Socialite;


Masukkan kode berikut ke dalam file SocialController.php

public function googleRedirect()

    {

        return Socialite::driver('google')->redirect();

    }

   

    public function loginWithGoogle()

    {

        try {

            $googleUser = Socialite::driver('google')->stateless()->user();

            $existingUser = User::where('google_id',$googleUser->id)

                ->orWhere('email',$googleUser->email)

                ->firest();

            if ($existingUser){

                if($existingUser->google_id !== $googleUser->id){

                    $existingUser->google_id = $googleUser->id;

                    $existingUser->save();

                }

                Auth::login($existingUser);

            } else {

                $createUser = User::create([

                    'name' => $googleUser->name,

                    'email' => $googleUser->email,

                    'google_id' => $googleUser->id,

                    'password' =>bcrypt('password'),

                ]);

                Auth::login($createUser);

            }

            return redirect()->to('/dashboard');

        } catch (\Throwable $th){

            throw $th;

        }

    }

Menyiapkan Route

Import kelas terleih dahulu

use App\Http\Controllers\SocialController;


Berikutnya tentukan rute untuk autentikasi Google

Route::get('auth/redirect',[SocialController::class, 'googleRedirect']);

Route::get('auth/callback',[SocialController::class, 'loginWithGoogle']);


Tambahkan Tautan Login Google ke Balde Registrasi/Login

Tambahkan-login-div.jpg

Jika muncul error saat mengakses halaman login seperti ini

Vite manifest not found at: /home/website.laravel/public_html/public/build/manifest.json

lakukan perintah berikut

apt install npm lalu

npm run build

kemudian ketika perintah berikut

npm install

npm run dev