Skip to content

Commit

Permalink
update inertia to pull in and save profile photo's from provider avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Butcher committed Feb 4, 2021
1 parent 0c8e25b commit 68db04e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
14 changes: 13 additions & 1 deletion routes/socialstream.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@
use Illuminate\Support\Facades\Route;
use JoelButcher\Socialstream\Http\Controllers\Inertia\PasswordController;
use JoelButcher\Socialstream\Http\Controllers\Inertia\RemoveConnectedAccountsController;
use JoelButcher\Socialstream\Http\Controllers\Inertia\UpdateUserProfilePhotoController;
use JoelButcher\Socialstream\Http\Controllers\OAuthController;
use JoelButcher\Socialstream\Socialstream;
use Laravel\Jetstream\Jetstream;

Route::group(['middleware' => config('socialstream.middleware', ['web'])], function () {
Route::get('/oauth/{provider}', [OAuthController::class, 'redirectToProvider'])->name('oauth.redirect');
Route::get('/oauth/{provider}/callback', [OAuthController::class, 'handleProviderCallback'])->name('oauth.callback');

if (config('jetstream.stack') === 'inertia') {
Route::delete('/user/connected-account/{id}', [RemoveConnectedAccountsController::class, 'destroy'])
->middleware(['auth'])
->name('connected-accounts.destroy');

Route::put('/user/set-password', [PasswordController::class, 'store'])->name('user-password.set');
Route::put('/user/set-password', [PasswordController::class, 'store'])
->middleware(['auth'])
->name('user-password.set');

if (Socialstream::hasProviderAvatarsFeature() && Jetstream::managesProfilePhotos()) {
Route::put('/user/profile-photo', [UpdateUserProfilePhotoController::class, '__invoke'])
->middleware(['auth'])
->name('user-profile-photo.set');
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;

class RemoveConnectedAccountsController extends Controller
{
Expand Down
29 changes: 29 additions & 0 deletions src/Http/Controllers/Inertia/UpdateUserProfilePhotoController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace JoelButcher\Socialstream\Http\Controllers\Inertia;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class UpdateUserProfilePhotoController extends Controller
{
/**
* Update the users profile picture from the connected account's avatar.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function __invoke(Request $request)
{
$account = ($user = $request->user())->connectedAccounts
->where('user_id', $user->getAuthIdentifier())
->where('id', $request->id)
->first();

if (is_callable([$user, 'setProfilePhotoFromUrl']) && ! is_null($account->avatar_path)) {
$user->setProfilePhotoFromUrl($account->avatar_path);
}

return back(303);
}
}

0 comments on commit 68db04e

Please sign in to comment.