/** * Helper function for the simplification of logging a user out. Also * serves as a dual purpose, with a single way to remove a user token. * * @return bool */ protected function _logout() { if ($this->isValidUser()) { /** * Grab the active token. We do this just incase there is another * token floating around. */ $active_token = Auth::user()->token; /** * Now remove that token and return success. Seeing as this is a * simple method/endpoint, no data is required on the return. */ if ($active_token) { $active_token->delete(); return true; } /** * Since this is a helper function, we want to avoid throwing exceptions, * and leave that up to the core code calling this. */ return false; } /** * Like the above, we simply return a boolean response rather than throw an * exception. */ return false; }
/** * Helper to check whether the token provided was valid. * * @return bool */ protected function isValidUser() { return !is_null(Auth::user()); }
/** * Allows users to reset their password. They are sent an email with a code * that they can use to reset their password. * * @return string */ public function reset() { /** * Make sure that we actually have a user token otherwise we'll have * no idea who we're creating a reset for. */ if ($this->isValidUser()) { /** * Load the reset on the off chance that there is already one. */ $user = Auth::user(); Response::add('user', $user); $user->load('reset'); /** * Check to see if a user reset exists, and create one if not. */ if ($user->reset && $user->reset->count() == 0) { /** * Create the user reset and generate a unique code. */ $reset = new Models\User_Reset(); $reset->generate(); /** * Assign the reset to the user. */ $user->reset()->save($reset); } $this->fireEvent(['user' => $this->user]); /** * Get the user data for the email. Not all of this is used in the default * email, but it allows for custom emails to contain more. */ Response::add('reset', $reset); $user->load('profile'); $user_data = $user->toArray(); /** * Send the email. */ Mail::send('emails.users.reset', $user_data, function ($message) use($user_data) { $message->to($user_data['email'], $user_data['profile']['name'])->subject('Password Reset'); }); /** * Unfortunately because of the way Laravels mail library works, * we can't tell whether or not an email was actually sent. It's safe * to let the end developer to control this side. */ return Response::toJson(); } /** * If no token is provided, or the user property is null, we want * to throw an exception of token type, as this should not happen, * if it does, the developer is being naughty and calling this * incorrectly. */ Response::error('No token provided', 'token'); }