Skip to content

Commit

Permalink
Registration mail verification and some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bestmomo committed Jul 1, 2015
1 parent b8636b0 commit 14a7199
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 84 deletions.
106 changes: 85 additions & 21 deletions app/Http/Controllers/Auth/AuthController.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
<?php namespace App\Http\Controllers\Auth;
<?php

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
namespace App\Http\Controllers\Auth;

use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use App\Http\Requests\Auth\RegisterRequest;
use App\Repositories\UserRepository;
use App\Services\MaxValueDelay;
use App\Jobs\SendMail;

class AuthController extends Controller {
class AuthController extends Controller
{

use AuthenticatesAndRegistersUsers;

/**
* Create a new authentication controller instance.
*
* @param Guard $auth
* @return void
*/
public function __construct(Guard $auth)
public function __construct()
{
$this->auth = $auth;

$this->middleware('guest', ['except' => 'getLogout']);
}

/**
* Handle a login request to the application.
*
* @param App\Http\Requests\LoginRequest $request
* @param App\Services\MaxValueDelay $maxValueDelay
* @param Guard $auth
* @return Response
*/
public function postLogin(LoginRequest $request, MaxValueDelay $maxValueDelay)
public function postLogin(
LoginRequest $request,
MaxValueDelay $maxValueDelay,
Guard $auth)
{
$logValue = $request->input('log');

Expand All @@ -43,19 +50,35 @@ public function postLogin(LoginRequest $request, MaxValueDelay $maxValueDelay)
}

$logAccess = filter_var($logValue, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

$credentials = [$logAccess => $logValue, 'password' => $request->input('password')];

if ($this->auth->attempt($credentials, $request->has('memory')))
{
return redirect('/');
$credentials = [
$logAccess => $logValue,
'password' => $request->input('password')
];

if(!$auth->validate($credentials)) {
$maxValueDelay->increment($logValue);

return redirect('/auth/login')
->with('error', trans('front/login.credentials'))
->withInput($request->only('log'));
}

$user = $auth->getLastAttempted();

if($user->confirmed) {
$auth->login($user, $request->has('memory'));

$maxValueDelay->increment($logValue);
if($request->session()->has('user_id')) {
$request->session()->forget('user_id');
}

return redirect('/auth/login')
->with('error', trans('front/login.credentials'))
->withInput($request->only('email'));
return redirect('/');
}

$request->session()->put('user_id', $user->id);

return redirect('/auth/login')->with('error', trans('front/verify.again'));
}


Expand All @@ -70,11 +93,52 @@ public function postRegister(
RegisterRequest $request,
UserRepository $user_gestion)
{
$user = $user_gestion->store($request->all());
$user = $user_gestion->store(
$request->all(),
$confirmation_code = str_random(30)
);

$this->auth->login($user);
$this->dispatch(new SendMail($user));

return redirect('/')->with('ok', trans('front/register.ok'));
return redirect('/')->with('ok', trans('front/verify.message'));
}

/**
* Handle a confirmation request.
*
* @param App\Repositories\UserRepository $user_gestion
* @param string $confirmation_code
* @return Response
*/
public function getConfirm(
UserRepository $user_gestion,
$confirmation_code)
{
$user = $user_gestion->confirm($confirmation_code);

return redirect('/')->with('ok', trans('front/verify.success'));
}

/**
* Handle a resend request.
*
* @param App\Repositories\UserRepository $user_gestion
* @param Illuminate\Http\Request $request
* @return Response
*/
public function getResend(
UserRepository $user_gestion,
Request $request)
{
if($request->session()->has('user_id')) {
$user = $user_gestion->getById($request->session()->get('user_id'));

$this->dispatch(new SendMail($user));

return redirect('/')->with('ok', trans('front/verify.resend'));
}

return redirect('/');
}

}
56 changes: 25 additions & 31 deletions app/Http/Controllers/Auth/PasswordController.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?php namespace App\Http\Controllers\Auth;
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;

use Illuminate\View\Factory;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Support\Facades\Password;
use Illuminate\Mail\Message;
use App\Http\Requests\Auth\EmailPasswordLinkRequest;
use App\Http\Requests\Auth\ResetPasswordRequest;

class PasswordController extends Controller {
class PasswordController extends Controller
{

/*
|--------------------------------------------------------------------------
Expand All @@ -27,15 +30,10 @@ class PasswordController extends Controller {
/**
* Create a new password controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param \Illuminate\Contracts\Auth\PasswordBroker $passwords
* @return void
*/
public function __construct(Guard $auth, PasswordBroker $passwords)
public function __construct()
{
$this->auth = $auth;
$this->passwords = $passwords;

$this->middleware('guest');
}

Expand All @@ -60,17 +58,17 @@ public function postEmail(
]);
});

switch ($response = $this->passwords->sendResetLink($request->only('email'), function($message)
{
$message->subject(trans('front/password.reset'));
}))
{
case PasswordBroker::RESET_LINK_SENT:
return redirect()->back()->with('status', trans($response));
$response = Password::sendResetLink($request->only('email'), function (Message $message) {
$message->subject(trans('front/password.reset'));
});

case PasswordBroker::INVALID_USER:
return redirect()->back()->with('error', trans($response));
}
switch ($response) {
case Password::RESET_LINK_SENT:
return redirect()->back()->with('status', trans($response));

case Password::INVALID_USER:
return redirect()->back()->with('error', trans($response));
}
}

/**
Expand All @@ -85,22 +83,18 @@ public function postReset(ResetPasswordRequest $request)
'email', 'password', 'password_confirmation', 'token'
);

$response = $this->passwords->reset($credentials, function($user, $password)
{
$user->password = bcrypt($password);

$user->save();

$this->auth->login($user);
$response = Password::reset($credentials, function($user, $password) {
$this->resetPassword($user, $password);
});

switch ($response)
{
case PasswordBroker::PASSWORD_RESET:
switch ($response) {
case Password::PASSWORD_RESET:
return redirect()->to('/')->with('ok', trans('passwords.reset'));

default:
return redirect()->back()->with('error', trans($response))->withInput();
return redirect()->back()
->with('error', trans($response))
->withInput($request->only('email'));
}
}

Expand Down
13 changes: 8 additions & 5 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php namespace App\Http\Controllers;
<?php

use App\Jobs\ChangeLocaleCommand;
namespace App\Http\Controllers;

class HomeController extends Controller {
use App\Jobs\ChangeLocale;

class HomeController extends Controller
{

/**
* Display the home page.
Expand All @@ -21,9 +24,9 @@ public function index()
* @return Response
*/
public function language(
ChangeLocaleCommand $changeLocaleCommand)
ChangeLocale $changeLocale)
{
$this->dispatch($changeLocaleCommand);
$this->dispatch($changeLocale);

return redirect()->back();
}
Expand Down
17 changes: 10 additions & 7 deletions app/Http/Middleware/App.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php namespace App\Http\Middleware;
<?php

namespace App\Http\Middleware;

use Closure;

use App\Jobs\SetLocaleCommand;
use App\Jobs\SetLocale;

use Illuminate\Bus\Dispatcher as BusDispatcher;

class App {
class App
{

/**
* The command bus.
Expand All @@ -20,7 +23,7 @@ class App {
*
* @array $bus
*/
protected $setLocaleCommand;
protected $setLocale;

/**
* Create a new App instance.
Expand All @@ -31,10 +34,10 @@ class App {
*/
public function __construct(
BusDispatcher $bus,
SetLocaleCommand $setLocaleCommand)
SetLocale $setLocale)
{
$this->bus = $bus;
$this->setLocaleCommand = $setLocaleCommand;
$this->setLocale = $setLocale;
}

/**
Expand All @@ -46,7 +49,7 @@ public function __construct(
*/
public function handle($request, Closure $next)
{
$this->bus->dispatch($this->setLocaleCommand);
$this->bus->dispatch($this->setLocale);

event('user.access');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php namespace App\Jobs;
<?php

namespace App\Jobs;

use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;

class ChangeLocaleCommand extends Job implements SelfHandling
class ChangeLocale extends Job implements SelfHandling
{
/**
* Execute the job.
Expand Down
52 changes: 52 additions & 0 deletions app/Jobs/SendMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Jobs;

use App\Jobs\Job;
use App\Models\User;
use Request;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Mail\Mailer;

class SendMail extends Job implements SelfHandling
{

/**
* User Model.
*
* @var App\Models\User
*/
protected $user;

/**
* Create a new SendMailCommand instance.
*
* @param App\Models\User $user
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
}

/**
* Execute the job.
*
* @param Mailer $mailer
* @return void
*/
public function handle(Mailer $mailer)
{
$data = [
'title' => trans('front/verify.email-title'),
'intro' => trans('front/verify.email-intro'),
'link' => trans('front/verify.email-link'),
'confirmation_code' => $this->user->confirmation_code
];

$mailer->send('emails.auth.verify', $data, function($message) {
$message->to($this->user->email, $this->user->username)
->subject(trans('front/verify.email-title'));
});
}
}
Loading

0 comments on commit 14a7199

Please sign in to comment.