Laravel Socialite Bitbucket Login

 

To implement Bitbucket login using Laravel Socialite, follow these steps:


Step 1: Install Laravel Socialite

In your terminal, run the following command to install the Socialite package:


composer require laravel/socialite


Step 2: Configure Bitbucket OAuth Credentials

Visit the Bitbucket Developer Console (https://bitbucket.org/account/settings/applications), create a new OAuth consumer, and obtain the Key and Secret.


Step 3: Configure Services in Laravel

Open `config/services.php` and add the Bitbucket OAuth credentials:


'bitbucket' => [

    'client_id' => env('BITBUCKET_KEY'),

    'client_secret' => env('BITBUCKET_SECRET'),

    'redirect' => env('BITBUCKET_REDIRECT_URI'),

],


Step 4: Create Routes

In your `routes/web.php`, create routes for social login:


Route::get('auth/bitbucket', 'Auth\SocialController@redirectToBitbucket');

Route::get('auth/bitbucket/callback', 'Auth\SocialController@handleBitbucketCallback');


Step 5: Create Controller

Generate a controller using the following command:


php artisan make:controller Auth/SocialController


In `Auth/SocialController.php`, implement the following methods:


use Laravel\Socialite\Facades\Socialite;


public function redirectToBitbucket()

{

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

}


public function handleBitbucketCallback()

{

    $user = Socialite::driver('bitbucket')->user();


    // $user contains user details received from Bitbucket


    // You can now authenticate the user or perform other actions


    return redirect()->route('home'); // Redirect after successful login

}


Step 6: Update .env File

Add your Bitbucket OAuth credentials to your `.env` file:


BITBUCKET_KEY=your_bitbucket_key

BITBUCKET_SECRET=your_bitbucket_secret

BITBUCKET_REDIRECT_URI=http://your-app-url/auth/bitbucket/callback


Replace `your_bitbucket_key` and `your_bitbucket_secret` with your actual credentials.


Step 7: Implement Login Button

In your view, create a link/button to initiate the Bitbucket login process:


<a href="{{ url('auth/bitbucket') }}">Login with Bitbucket</a>


That's it! Users can now click the "Login with Bitbucket" link/button, and they will be redirected to Bitbucket for authentication. Upon successful authentication, they will be redirected back to your application with user data.


Remember to handle user data appropriately, possibly by creating a new user or authenticating an existing user in your database. Also, customize the redirection and user management as per your application's requirements.