/** * @param array $params * @return mixed * @throws BadRequestException */ public function login($params = array()) { $defaults = array('email' => $email = null, 'password' => $password = null); $rules = array('email' => array('required', 'email', 'exists:users,email,login_type,' . User::LOGIN_TYPE_PASSWORD . ',deleted_at,NULL'), 'password' => array('required')); $params = $this->validateParams($defaults, $params, $rules); extract($params); if (!$this->api->guard->attempt(array('email' => $email, 'password' => $password, 'login_type' => User::LOGIN_TYPE_PASSWORD, 'deleted_at' => null))) { throw new BadRequestException("Your credentials are incorrect. Please try again."); } $user = $this->api->user(); $access_token = AccessToken::make($user); return AccessToken::where('token', $access_token->token)->firstOrFail(); }
/** * Handle an incoming request. * Get the user associated with the access_token. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if ($token = $request->input('access_token', false)) { $cache = app('Illuminate\\Contracts\\Cache\\Repository'); $user = false; if ($cache->has($token)) { $user_id = $cache->get($token, false); $user = User::findOrFailCached($user_id); } elseif ($access_token = AccessToken::where('token', $token)->where('expires_at', '>', new \DateTime())->first()) { /** * Logging this because this shouldn't happen. */ app('log')->info('Looking up token in database.'); $user = $access_token->user; $cache->put($access_token->token, $user->id, $access_token->expires_at); } if ($user) { $this->guard->login($user); } } return $next($request); }