mercredi 15 juillet 2020

Laravel - How to use Auth::check or Auth::user between different domains?

I've got a question regarding laravel framework (vers. 5.2) and the authentication. I have 2 domains which represent a software. let's call them https://azure.mydomain.com and https://azuresoftware.mydomain.com.

The laravel application is hosted on the domain https://azuresoftware.mydomain.com. https://azure.mydomain.com is just a cms framework which is providing some information about the website.

Now I want to display different menus, if the user is logged in or not on https://azure.mydomain.com. I thought, I can do a fetch request to https://azuresoftware.mydomain.com/ and use the laravel methods Auth::check() to check if the user is already logged in or not. I know that this is a cors fetch request, but this is not the issue. I've allowed in the IIS webserver requests from https://azure.mydomain.com. The request works fine and also just a simple request. But actually Auth::check() is always returning false, even when I'm logged in on the software side.

This is my code so far:

<script>
    fetch('https://azuresoftware.mydomain.com/checkLogin', {
        method: 'GET', 
        headers: { 
            'Content-Type': 'text/plain'
        }
    })
    .then(function(res) { 
        return res.json()
    })
    .then(function(data) { 
        if(data.isLoggedIn) {
            // do some stuff...
        }
        else 
        {
            // do some other stuff...   
        }
    });
</script>

routes.php:

Route::group(['middleware' => 'web'], function () {
...
    Route::get('checkLogin', function() {
        return json_encode(['isLoggedIn'=>\Auth::check()]);
    });
...

I'm sure, I forgot something essential, why it is not working this way, so I'm happy for any hint. Many thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire