PHPackages                             danyseifeddine/keychain - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. danyseifeddine/keychain

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

danyseifeddine/keychain
=======================

A Laravel package for simplified social authentication with multi-guard support

1.1.2(9mo ago)1392MITPHPPHP ^8.0|^8.1|^8.2|^8.3|^8.4

Since Jul 19Pushed 9mo agoCompare

[ Source](https://github.com/Danyseifedine/KeyChain)[ Packagist](https://packagist.org/packages/danyseifeddine/keychain)[ RSS](/packages/danyseifeddine-keychain/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (3)Used By (0)

🔐 KeyChain - Laravel Social Authentication Package
==================================================

[](#-keychain---laravel-social-authentication-package)

**The most powerful and flexible Laravel social authentication package with multi-guard support, automatic setup, and complete customization control.**

KeyChain provides an elegant solution for implementing social login across multiple user types (Users, Restaurants, Admins, etc.) with automatic guard registration, dynamic user creation, polymorphic token storage, and infinitely customizable authentication flows.

[![Latest Version on Packagist](https://camo.githubusercontent.com/86f0dedfa4955eed6ae494b197494d1a3d2fb953f99874793bc417963d36313e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64616e7973656966656464696e652f6b6579636861696e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/danyseifeddine/keychain)[![Total Downloads](https://camo.githubusercontent.com/3209a31ea74e587b37bea76bf8ab52f57f063328185e4372737e28860cd79817/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64616e7973656966656464696e652f6b6579636861696e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/danyseifeddine/keychain)[![License](https://camo.githubusercontent.com/e752815dc514e14a6714df071e82b0d216d2042a4b18ab4aa4ae0ec1e4c53f7d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f64616e7973656966656464696e652f6b6579636861696e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/danyseifeddine/keychain)

🚀 Features
----------

[](#-features)

- 🔐 **Multi-Guard Support** - Support unlimited authentication guards (User, Restaurant, Admin, etc.)
- 🎯 **Automatic Setup** - Guards, providers, and routes registered automatically from config
- 🪄 **Magic Methods** - Dynamic method generation for guard-specific user creation/update
- 📦 **Laravel Socialite Integration** - Built on Laravel's official social authentication
- 🔄 **Token Management** - Polymorphic social token storage with advanced expiration control
- 🎨 **Fully Customizable** - Override any part of the authentication flow
- 🛡️ **Security First** - Secure token handling, validation, and cleanup
- 📊 **Management Commands** - Artisan commands for token management and statistics
- 🎭 **Publishable Assets** - Controllers, views, config, and migrations
- 🔧 **Zero Configuration** - Works out of the box with sensible defaults

📋 Table of Contents
-------------------

[](#-table-of-contents)

1. [Installation](#installation)
2. [Basic Configuration](#basic-configuration)
3. [Simple Usage](#simple-usage)
4. [Multi-Guard Setup](#multi-guard-setup)
5. [Publishing Assets](#publishing-assets)
6. [Controller Customization](#controller-customization)
7. [Magic Methods](#magic-methods)
8. [Token Management](#token-management)
9. [Helper Functions](#helper-functions)
10. [Commands](#commands)
11. [Advanced Configuration](#advanced-configuration)
12. [Examples](#examples)
13. [Troubleshooting](#troubleshooting)

---

🚀 Installation
--------------

[](#-installation)

### 1. Install via Composer

[](#1-install-via-composer)

```
composer require danyseifeddine/keychain
```

### 2. Publish Configuration and Assets

[](#2-publish-configuration-and-assets)

```
# Publish everything (recommended for first time)
php artisan vendor:publish --provider="Danyseifeddine\Keychain\KeyChainServiceProvider"

# Or publish specific assets
php artisan vendor:publish --tag=keychain-config
php artisan vendor:publish --tag=keychain-migrations
php artisan vendor:publish --tag=keychain-controller
```

### 3. Run Migrations

[](#3-run-migrations)

```
php artisan migrate
```

### 4. Add Social Provider Credentials

[](#4-add-social-provider-credentials)

Add your OAuth credentials to `.env`:

```
# Google OAuth
KEYCHAIN_GOOGLE_CLIENT_ID=your-google-client-id
KEYCHAIN_GOOGLE_CLIENT_SECRET=your-google-client-secret

# GitHub OAuth
KEYCHAIN_GITHUB_CLIENT_ID=your-github-client-id
KEYCHAIN_GITHUB_CLIENT_SECRET=your-github-client-secret

# Facebook OAuth
KEYCHAIN_FACEBOOK_CLIENT_ID=your-facebook-client-id
KEYCHAIN_FACEBOOK_CLIENT_SECRET=your-facebook-client-secret

# Custom redirect URLs (optional)
KEYCHAIN_GOOGLE_REDIRECT_URL=http://127.0.0.1:8000/auth/google/callback
KEYCHAIN_GITHUB_REDIRECT_URL=http://127.0.0.1:8000/auth/google/callback
```

---

⚙️ Basic Configuration
----------------------

[](#️-basic-configuration)

Update `config/keychain.php` to enable providers:

```
return [
    // Enable social providers
    'providers' => [
        'google' => true,   // Enable Google OAuth
        'github' => true,   // Enable GitHub OAuth
        'facebook' => false, // Disable Facebook OAuth
    ],

    // Basic routes configuration
    'routes' => [
        'enabled' => true,
        'prefix' => 'auth',
        'middleware' => ['web'],
    ],

    // User management
    'user' => [
        'auto_create' => true,
        'auto_link' => true,
        'update_on_login' => true,
    ],
];
```

---

🎯 Simple Usage
--------------

[](#-simple-usage)

### Basic Social Login (Single Guard)

[](#basic-social-login-single-guard)

KeyChain automatically generates these routes:

```
// Redirect to provider
GET /auth/{provider}/redirect

// Handle callback
GET /auth/{provider}/callback

// Unlink account
DELETE /auth/{provider}/unlink
```

**In your Blade template:**

```
{{-- resources/views/auth/login.blade.php --}}

    Login with Social Media

    @if(config('keychain.providers.google'))

            Continue with Google

    @endif

    @if(config('keychain.providers.github'))

            Continue with GitHub

    @endif

    @if(config('keychain.providers.facebook'))

            Continue with Facebook

    @endif

```

### User Model Setup

[](#user-model-setup)

Add the `HasSocialAccounts` trait to your User model:

```
