#Just won't log me in anywhere outside of a Route...

4 messages · Page 1 of 1 (latest)

compact spindle
#

So I am trying to create a 'magic' link type feature that I could send to clients to onboard that logs them directly into the LMS I'm creating. So I created a middleware with code as so:
https://gist.github.com/HeadStudios/c056f6da5992599dc2dc9ee824f31b74

Anyway that works fine with magic link from route as so:

Route::get('/vlesson/{perma}', [CourseController::class, 'vlesson'])->middleware('magic');

it logs them in and show them their personalised stuf... now here's where the issue comes in... the login WON'T stick. So for example if I open up a new tab in my browser and go into Laravel Nova... they're not logged in.

I tested and if I execute the same code in a route:
e.g.

Route::get('/authentifer', function() {

    Auth::loginUsingId(1, true);

    return "Now we are in forever!";

});

It logs me in without issue in the sense that i can open up new tabs and my session is across all the windows and I'm logged in properly. At first I thought it was Middleware so I even added this code to the ACTUAL blade view:

<?php
use App\Models\Checklist;
use App\Models\User;
use App\Models\Course;
use App\Models\Vlesson;
use App\Http\Controllers\CourseController;
use Illuminate\Support\Facades\Auth;

Auth::loginUsingId(1, true);

$course = CourseController::getCourseForLesson($user->id, $vlesson->id);
echo $course->name;
die();
$user_id = $user->id;
$course_id = 2;
...

But even with this view... it STILL doesn't log me in and permeate the sessions across different windows... so... is there any way to log a person in and keep them consistently logged in when using the view/controller/middleware fraework.

Any input would be much appreciated. Thank you!

Gist

GitHub Gist: instantly share code, notes, and snippets.

leaden solar
#
  1. Consider using laravel's signed urls rather than 'magic'
  2. Is you route in the web middleware group which has the StartSession middleware
  3. In your middleware you already have the $user model so just login with auth()->login($user)
compact spindle
compact spindle
#

Still not working for me to have login persist here is what I have tried:

  1. Added web middleware group to my route as so:
Route::get('/vlesson/{perma}', [CourseController::class, 'vlesson'])->middleware(['web', 'magic']);
  1. Have confirmed StartSession is in web middleware group in Kernel:
protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \Illuminate\Session\Middleware\StartSession::class,
        ],
  1. Tried using auth()->login($user) function as so
class MagicStick
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        
        
        $magic = $request->query('magic');
        
        // Check if there is a user with a matching magic column value
        $user = \App\Models\User::where('magic', $magic)->first();
        
        if ($user ) {
            // Log the user in
            //\Illuminate\Support\Facades\Auth::login($user);
            auth()->login($user);

Any other input or ideas and troubleshooting steps to check would be much appreciated