Hej
Hej! is a simple authentication boilerplate for Socialite.
Install / Use
/learn @renoki-co/HejREADME
Hej! - a Socialite authentication flow implementation
Hej! is a simple authentication flow implementation for Socialite. Out-of-the-box, Hej! can help you login and register users using Socialite providers, or link and unlink social accounts, just by extending a controller.
- Hej! - a Socialite authentication flow implementation
🤝 Supporting
If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with Github Sponsors. 📦
<img src="https://github-content.s3.fr-par.scw.cloud/static/23.jpg" height="210" width="418" />
🚀 Installation
You can install the package via composer:
composer require renoki-co/hej
Publish the config:
$ php artisan vendor:publish --provider="RenokiCo\Hej\HejServiceProvider" --tag="config"
Publish the migrations:
$ php artisan vendor:publish --provider="RenokiCo\Hej\HejServiceProvider" --tag="migrations"
🙌 Usage
For the user (or any Authenticatable instance) you should add the HasSocialAccounts trait and the Sociable interface:
use RenokiCo\Hej\Concerns\HasSocialAccounts;
use RenokiCo\Hej\Contracts\Sociable;
class User extends Authenticatable implements Sociable
{
use HasSocialAccounts;
//
}
Out-of-the-box, it works with any Laravel application.
After you have configured Socialite, the only thing to do is to point your desired redirect and callback paths to the package controller:
Route::get('/social/{provider}/redirect', [\RenokiCo\Hej\Http\Controllers\SocialController::class, 'redirect']);
Route::get('/social/{provider}/callback', [\RenokiCo\Hej\Http\Controllers\SocialController::class, 'callback']);
Route::middleware('auth')->group(function () {
Route::get('/social/{provider}/link', [\RenokiCo\Hej\Http\Controllers\SocialController::class, 'link']);
Route::get('/social/{provider}/unlink', [\RenokiCo\Hej\Http\Controllers\SocialController::class, 'unlink']);
});
The paths can be any, as long as they contain a first parameter which is going to be the provider you try to authenticate with. For example, accessing this link will redirect to Github:
https://my-link.com/social/github/redirect
Extending Controllers
Hej! is really flexible and does a lot of things in the background to register or login using Socialite.
However, you need to extend the controller and you will then be able to replace some methods to customize the flow.
use RenokiCo\Hej\Http\Controllers\SocialController;
class MySocialController extends SocialController
{
//
}
Then you should point the routes to the new controller.
Provider whitelisting
Due to the fact that the endpoints are opened to get any provider, you can whitelist the Socialite provider names that can be used:
/**
* Whitelist social providers to be used.
*
* @var array
*/
protected static $allowedSocialiteProviders = [
//
];
For example, allowing only Facebook and Github should look like this:
protected static $allowedSocialiteProviders = [
'facebook',
'github',
];
If one of the providers accessed via the URL is not whitelisted, a simple redirect is done automatically. However, you can replace it and redirect to your custom redirect action.
Custom Socialite Redirect & Retrieval
With Socialite, you can use ->redirect() to redirect the user and ->user() to retrieve it. You can customize the instances by replacing getSocialiteRedirect and getSocialiteUser.
Here is the default configuration:
/**
* Get the Socialite direct instance that will redirect
* the user to the right provider OAuth page.
*
* @param \Illuminate\Http\Request $request
* @param string $provider
* @return mixed
*/
protected function getSocialiteRedirect(Request $request, string $provider)
{
return $this->socialite
->driver($provider)
->redirect();
}
/**
* Get the Socialite User instance that will be
* given after the OAuth authorization passes.
*
* @param \Illuminate\Http\Request $request
* @param string $provider
* @return \Laravel\Socialite\AbstractUser
*/
protected function getSocialiteUser(Request $request, string $provider)
{
return $this->socialite
->driver($provider)
->user();
}
Registering new users
When the Social account that the user logged in is not registered within the database, it creates a new authenticatable model, but in order to do this, it should fill it with data.
By default, it fills in using Socialite Provider's given data and sets a random 64-letter word password:
/**
* Get the Authenticatable model data to fill on register.
* When the user gets created, it will receive these parameters
* in the `::create()` method.
*
* @param \Illuminate\Http\Request $request
* @param string $provider
* @param \Laravel\Socialite\AbstractUser $providerUser
* @return array
*/
protected function getRegisterData(Request $request, string $provider, $providerUser): array
{
return [
'name' => $providerUser->getName(),
'email' => $providerUser->getEmail(),
'email_verified_at' => now(),
'password' => Hash::make(Str::random(64)),
];
}
Handling duplicated E-Mail addresses
Sometimes, it can happen for the users to have an account created with E-Mail address only, having no social accounts. A new social account with the same E-Mail address will trigger a new authenticatable record in the database on callback.
For this, a Redirect is made to handle this specific scenario.
Filling the Social table
After registration or login, the Socialite data gets created or updated, either the user existed or not.
By default, it's recommended to not get overwritten, excepting for the fact you want to change the table structure and extend the Social model that is also set in config/hej.php.
/**
* Get the Social model data to fill on register or login.
*
* @param \Illuminate\Http\Request $request
* @param string $provider
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Laravel\Socialite\AbstractUser $providerUser
* @return array
*/
protected function getSocialData(Request $request, string $provider, $model, $providerUser): array
{
return [
'provider_nickname' => $providerUser->getNickname(),
'provider_name' => $providerUser->getName(),
'provider_email' => $providerUser->getEmail(),
'provider_avatar' => $providerUser->getAvatar(),
];
}
Callbacks
Right before the user is authenticated or registered successfully, there exist callback that trigger and you can replace them for some custom logic.
/**
* Handle the callback after the registration process.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Model $social
* @param \Laravel\Socialite\AbstractUser $providerUser
* @return void
*/
protected function registered(Request $request, $model, $social, $providerUser)
{
//
}
/**
* Handle the callback after the login process.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Model $social
* @param \Laravel\Socialite\AbstractUser $providerUser
* @return void
*/
protected function authenticated(Request $request, $model, $social, $providerUser)
{
//
}
/**
* Handle the callback after the linking process.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Model $social
* @param \Laravel\Socialite\AbstractUser $providerUser
* @return void
*/
protected function linked(Request $request, $model, $social, $providerUser)
{
//
}
/**
* Handle the callback after the unlink process.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $provider
* @return void
*/
protected function unlinked(Request $request, $model, string $provider)
{
Related Skills
apple-reminders
345.4kManage Apple Reminders via remindctl CLI (list, add, edit, complete, delete). Supports lists, date filters, and JSON/plain output.
gh-issues
345.4kFetch GitHub issues, spawn sub-agents to implement fixes and open PRs, then monitor and address PR review comments. Usage: /gh-issues [owner/repo] [--label bug] [--limit 5] [--milestone v1.0] [--assignee @me] [--fork user/repo] [--watch] [--interval 5] [--reviews-only] [--cron] [--dry-run] [--model glm-5] [--notify-channel -1002381931352]
node-connect
345.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
oracle
345.4kBest practices for using the oracle CLI (prompt + file bundling, engines, sessions, and file attachment patterns).
