So currently I'm joining together two API services using Laravel Socialite. Both work fine on their own.
I want to make it so you can sign in using the 'add to Slack' button and it will store the Slack details on the users
table without needing to bother storing things like name, email address, password etc. So far my schema looks like this:
- id
- slack_user_id
- slack_access_token
- slack_team_name
- slack_team_id
- slack_bot_user_id
- slack_bot_access_token
- Other fields for second API
I save the user and then manually authenticate them and redirect them to the dashboard like so:
Auth::loginUsingId($user->id, true);
return redirect('home');
Then I prompt them to connect to the next service. The OAuth flow on its own works perfectly, but I want to update the logged in user with the new details. My code looks like this:
$id = Auth::user()->id;
$user = User::findOrFail($id);
//Save fields to the user
$user->save();
return redirect('/home');
This fails to work as it cannot find the user through Auth
. I have tried to dump the current Auth::user()
object but it returns null.
This works fine before the redirect to the OAuth provider but it looks like it's killing the session in between. I am currently using file
as my session driver but have also tried using cookie
.
Sample code of the second API code:
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use Session;
use Socialite;
use Illuminate\Http\Request;
class OAuthController extends Controller
{
public function handleProviderCallback(Request $request)
{
$stravaUser = Socialite::driver('strava')->stateless()->user();
$accessTokenResponseBody = $stravaUser->accessTokenResponseBody;
dd(\Auth::user());
$id = Auth::user()->id;
$user = User::findOrFail($id);
//Do User fields here
$user->save();
return redirect('/home');
}
}
Any advice?