{"action":"created","issue":{"url":"https://api.github.com/repos/dingo/api/issues/852","repository_url":"https://api.github.com/repos/dingo/api","labels_url":"https://api.github.com/repos/dingo/api/issues/852/labels{/name}","comments_url":"https://api.github.com/repos/dingo/api/issues/852/comments","events_url":"https://api.github.com/repos/dingo/api/issues/852/events","html_url":"https://github.com/dingo/api/issues/852","id":133931723,"number":852,"title":"Using OAuth2 Middleware with Laravel Sessions","user":{"login":"lvandyk","id":6745257,"avatar_url":"https://avatars.githubusercontent.com/u/6745257?v=3","gravatar_id":"","url":"https://api.github.com/users/lvandyk","html_url":"https://github.com/lvandyk","followers_url":"https://api.github.com/users/lvandyk/followers","following_url":"https://api.github.com/users/lvandyk/following{/other_user}","gists_url":"https://api.github.com/users/lvandyk/gists{/gist_id}","starred_url":"https://api.github.com/users/lvandyk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lvandyk/subscriptions","organizations_url":"https://api.github.com/users/lvandyk/orgs","repos_url":"https://api.github.com/users/lvandyk/repos","events_url":"https://api.github.com/users/lvandyk/events{/privacy}","received_events_url":"https://api.github.com/users/lvandyk/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2016-02-16T09:46:43Z","updated_at":"2016-03-08T03:07:23Z","closed_at":"2016-03-08T03:07:23Z","body":"I have a route group using api.auth middleware working nicely with OAuth2. How do I add the 'auth' middleware so that if the user is actually logged in and has a session, they can also reach that route and thus the $this->auth->user() is populated?\r\n\r\nE.g. User accesses route via mobile app using token vs user accessing route via browser when logged in (javascript ajax call)."},"comment":{"url":"https://api.github.com/repos/dingo/api/issues/comments/193579518","html_url":"https://github.com/dingo/api/issues/852#issuecomment-193579518","issue_url":"https://api.github.com/repos/dingo/api/issues/852","id":193579518,"user":{"login":"jasonlewis","id":829059,"avatar_url":"https://avatars.githubusercontent.com/u/829059?v=3","gravatar_id":"","url":"https://api.github.com/users/jasonlewis","html_url":"https://github.com/jasonlewis","followers_url":"https://api.github.com/users/jasonlewis/followers","following_url":"https://api.github.com/users/jasonlewis/following{/other_user}","gists_url":"https://api.github.com/users/jasonlewis/gists{/gist_id}","starred_url":"https://api.github.com/users/jasonlewis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jasonlewis/subscriptions","organizations_url":"https://api.github.com/users/jasonlewis/orgs","repos_url":"https://api.github.com/users/jasonlewis/repos","events_url":"https://api.github.com/users/jasonlewis/events{/privacy}","received_events_url":"https://api.github.com/users/jasonlewis/received_events","type":"User","site_admin":false},"created_at":"2016-03-08T03:07:23Z","updated_at":"2016-03-08T03:07:23Z","body":"You shouldn't be trying to merge the two authentication mechanisms like that. An API route isn't meant to be viewed through a \"web app\". Your web app may consume your API. Basically, your regular routes should not point to an API route. That's not how it works.\r\n\r\nIf you need to hit the API route from your web app route for whatever reason, you might consider firing off an internal request.\r\n\r\nSo, from your web app:\r\n\r\n```php\r\nRoute::get('example', ['middleware' => ['web'], function () {\r\n  $user = app('api.dispatcher')->as(Auth::user())->get('user'); \r\n}]);\r\n```\r\n\r\nThe `as` method will authenticate you with the API which will allow you to use `$this->auth->user()` in your API controllers still. All subsequent internal requests will then be authenticated as that user.\r\n\r\nYou can use `as(Auth::user())->once()` to only authenticate for that request only."}}