<?php /** * Created by PhpStorm. * User: camilstaps * Date: 12-5-15 * Time: 14:41 */ namespace CamilStaps\BotleaguesApi; use Dingo\Api\Auth\Provider\Provider; use Dingo\Api\Routing\Route; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; class TokenAuthenticationProvider implements Provider { /** * Authenticate the request and return the authenticated user instance. * * @param \Illuminate\Http\Request $request * @param \Dingo\Api\Routing\Route $route * * @return mixed */ public function authenticate(Request $request, Route $route) { if (!$request->has(['user_id', 'token'])) { throw new UnauthorizedHttpException(null, "Include user_id and token in your request."); } $user = User::find($request->get('user_id')); if ($user != null && $user->validToken($request->get('token'))) { Auth::login($user); return Auth::user(); } throw new UnauthorizedHttpException(null, "Invalid credentials"); } /** * Get the providers authorization method. * * @return string */ public function getAuthorizationMethod() { return 'token'; } }