Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
webbingbrasil/users-module / Http / Controllers / Api / AuthController.php
Size: Mime:
<?php

namespace Modules\Users\Http\Controllers\Api;

use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Support\Facades\Auth;
use Modules\Core\Contracts\Repositories\ApiTokenRepository;
use Modules\Core\Http\Controllers\Api\ApiBaseController;
use Modules\Users\Http\Requests\Api\LoginRequest;

class AuthController extends ApiBaseController
{

    private $redirectAfterLogout = '/painel/login';
    private $loginPath = '/painel/login';
    private $redirectTo = '/painel';
    private $username = 'email';

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    public function __construct(ApiTokenRepository $apiTokenRepository)
    {
        parent::__construct($apiTokenRepository);

        // config redirect routes
        $this->redirectAfterLogout = 'site.logout';
        $this->loginPath = 'site.login';
        $this->redirectTo = config('users.redirectTo', 'site.index');
        $this->username = config('users.usernameField', 'username');
    }

    /**
     * Handle a login request to the application.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function postLogin(LoginRequest $request)
    {
        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        $throttles = $this->isUsingThrottlesLoginsTrait();

        if ($throttles && $this->hasTooManyLoginAttempts($request)) {
            return $this->sendLockoutResponse($request);
        }

        $credentials = $this->getCredentials($request);

        if (Auth::attempt($credentials, $request->has('remember'))) {
            return response()->json([
                'redirectTo' => route($this->redirectTo)
            ], 200);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        if ($throttles) {
            $this->incrementLoginAttempts($request);
        }
        return response()->json([
            $this->loginUsername() => [$this->getFailedLoginMessage()]
        ], 401);
    }
}