AuthController.php 5.00 KiB
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Mail;
use Illuminate\Http\Request;
class AuthController extends Controller
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    /**
     * Where to redirect users after login / registration.
     * @var string
    protected $redirectTo = '/';
    protected $username = 'username';
    protected $redirectAfterLogout = '/login';
    /**
     * Create a new authentication controller instance.
     * @return void
    public function __construct()
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    /**
     * Get a validator for an incoming registration request.
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
    protected function validator(array $data)
        return Validator::make($data, [
            'username' => 'required|min:6|max:32|unique:users,username',
            'password' => 'required|min:6|max:32|confirmed',
            'firstname' => 'required|max:255',
            'lastname' => 'required|max:255',
            'image' => 'required|image|max:8000',
            'email' => 'required|email|unique:users,email',
        ]);
    /**
     * Create a new user instance after a valid registration.
     * @param  array  $data
     * @return User
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
*/ protected function create(array $data) { $user = User::create([ 'username' => $data['username'], 'password' => bcrypt($data['password']), 'firstname' => $data['firstname'], 'lastname' => $data['lastname'], 'email' => $data['email'], 'status' => false, ]); if (!empty($data['image'])) { $mime_type = request()->file('image')->getClientOriginalExtension(); $destination_path = 'profiles/' . $user->id . '.' . $mime_type; \Storage::put( $destination_path, file_get_contents($data['image']) ); $user->image = $destination_path; $user->save(); } Mail::send('auth.emails.verified', ['user' => $user], function ($m) use ($user) { $m->from('seedcamp@thinknet.co.th', 'email verification'); $m->to($user->email)->subject('Please verified you e-mail'); }); } public function postRegister(Request $request) { $validator = $this->validator($request->all()); if ($validator->fails()) { $this->throwValidationException( $request, $validator ); } $this->create($request->all()); $verifyMsg = 'Thanks for creating an account, Please verify your email address.'; return redirect('/login') ->with('verifyMsg', $verifyMsg); } public function postLogin(Request $request) { $this->validateLogin($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 && $lockedOut = $this->hasTooManyLoginAttempts($request)) { $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } $credentials = $this->getCredentials($request);
141142143144145146147148149150151152153154155156157158159160161162163164165166167
if (\Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) { if(!\Auth::User()->status) { $verifyMsg = 'Please verify your email address.'; \Auth::guard($this->getGuard())->logout(); return redirect()->to('/login') ->with('verifyMsg', $verifyMsg); } return $this->handleUserWasAuthenticated($request, $throttles); } // 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 && ! $lockedOut) { $this->incrementLoginAttempts($request); } return $this->sendFailedLoginResponse($request); } }