Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ajax call Sentinel::check() and Sentinel::getUser() always return null #354

Closed
hbsoftco opened this issue Mar 30, 2017 · 11 comments
Closed

Comments

@hbsoftco
Copy link

hbsoftco commented Mar 30, 2017

Hi
I'm using Sentinel package, Everything is fine, But when I want call Sentinel::check() and Sentinel::getUser() using ajax, it return null. help me please :(

my route in api.php
Route::post('/managersShowMarkers', 'ManagerController@managersShowMarkers');

managerShowMarkers method in ManagerController
public function managersShowMarkers() { $currentUser = Sentinel::getUser(); $my_id = $currentUser->id; $markers = MapLocation::where('code_shahrestan', $my_id)->get(); return $markers; }

jquery and ajax
`var map;
var marker;

jQuery(document).ready(function () {

var latval = 37.4714526;
var lngval = 57.320;

console.log([latval, lngval]);

myLatLng = new google.maps.LatLng(latval, lngval);

createMap(myLatLng);

// Create Map
function createMap(myLatLng) {
    map = new google.maps.Map(document.getElementById('mymap'), {
        center: myLatLng,
        zoom: 14
    });

    setMarkers(map);

}

function setMarkers(map) {

    $.post(
        'http://localhost:8000/api/managersShowMarkers',
        {vaz: 'ok'},
        function (match) {
            console.log(match);

            $.each(match, function (i, val) {

                var glatval = val.lat;
                var glngval = val.lng;

                var gname = val.address;
                var status = val.status;
                var shahrestan = val.shahrestan;
                var code = val.code_keshvari_shobe;
                var shahr_rousta = val.shahr_rousta;

                // console.log(status);
                var gicn;
                if (status == 'آنلاين') {
                    gicn = 'storage/uploads/onlineFlag.png';
                }
                else if (status == 'آفلاين') {

                    gicn = 'storage/uploads/offlineFlag.png';

                } else {

                }

                marker = new google.maps.Marker({
                    position: {
                        lat: parseFloat(glatval), lng: parseFloat(glngval)
                    },
                    map: map,
                    icon: gicn,
                    title: gname
                });

                var infoWindow = new google.maps.InfoWindow(), marker, i;

                google.maps.event.addListener(marker, 'click', (function (marker, i) {
                    return function () {
                        infoWindow.setContent('<div class="font-iran scrollFix gmap-info">کد شعبه:' + code + '</div><div class="font-iran scrollFix gmap-info">' + shahrestan + '</div><div class="font-iran scrollFix gmap-info">' + shahr_rousta + '</div><div class="font-iran scrollFix gmap-info">' + gname + '</div>');
                        infoWindow.open(map, marker);
                    }
                })(marker, i));

            });

        });
}});
@brunogaspar
Copy link
Member

What Laravel version are you using?

@hbsoftco
Copy link
Author

laravel 5.3

@brunogaspar
Copy link
Member

Is that route you posted using the web middleware?

@hbsoftco
Copy link
Author

hbsoftco commented Mar 30, 2017

I did not use a middleware

This is my code in api.php

user(); })->middleware('auth:api'); Route::post('/showMarkers', 'MapLocationsController@showMarkers'); Route::post('/managersShowMarkers', 'ManagerController@managersShowMarkers');

@brunogaspar
Copy link
Member

You'll need to use the web middleware as well i'm afraid, otherwise you'll not be able to get the logged in user i suppose since the API routes are stateless.

@hbsoftco
Copy link
Author

hbsoftco commented Mar 30, 2017

Thanks for reply, but it's not working with web middleware :(

@brunogaspar
Copy link
Member

Is it working as expected without being an Ajax request?

@hbsoftco
Copy link
Author

Yes, it's working correctly

@brunogaspar
Copy link
Member

So something on those routes are not correctly setup.

Therefore i believe it's not a Sentinel issue since it's working as expected in the rest of your application.

@joginder89
Copy link

@hbsoftco Do you able to solve this problem ? I am facing same problem.

@hbsoftco
Copy link
Author

hbsoftco commented Apr 4, 2017

@joginder89 I used another way.
First add user's info into a session after logging
Session::put('city_id', $currentUser->city_id);
then goto app/Http/Kernel.php and active session on rest api routes
'api' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Session\Middleware\StartSession::class, 'throttle:60,1', 'bindings', ],
and use it in ajax call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants