mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-03-13 04:03:52 +00:00
150 - Registering with a Valid Invitation
This commit is contained in:
@@ -3,69 +3,27 @@
|
|||||||
namespace App\Http\Controllers\Auth;
|
namespace App\Http\Controllers\Auth;
|
||||||
|
|
||||||
use App\User;
|
use App\User;
|
||||||
|
use App\Invitation;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Foundation\Auth\RegistersUsers;
|
|
||||||
|
|
||||||
class RegisterController extends Controller
|
class RegisterController extends Controller
|
||||||
{
|
{
|
||||||
/*
|
public function register()
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
| Register Controller
|
|
||||||
|--------------------------------------------------------------------------
|
|
||||||
|
|
|
||||||
| This controller handles the registration of new users as well as their
|
|
||||||
| validation and creation. By default this controller uses a trait to
|
|
||||||
| provide this functionality without requiring any additional code.
|
|
||||||
|
|
|
||||||
*/
|
|
||||||
|
|
||||||
use RegistersUsers;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Where to redirect users after login / registration.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $redirectTo = '/home';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new controller instance.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
{
|
||||||
$this->middleware('guest');
|
$invitation = Invitation::findByCode(request('invitation_code'));
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
$user = User::create([
|
||||||
* Get a validator for an incoming registration request.
|
'email' => request('email'),
|
||||||
*
|
'password' => bcrypt(request('password')),
|
||||||
* @param array $data
|
|
||||||
* @return \Illuminate\Contracts\Validation\Validator
|
|
||||||
*/
|
|
||||||
protected function validator(array $data)
|
|
||||||
{
|
|
||||||
return Validator::make($data, [
|
|
||||||
'name' => 'required|max:255',
|
|
||||||
'email' => 'required|email|max:255|unique:users',
|
|
||||||
'password' => 'required|min:6|confirmed',
|
|
||||||
]);
|
]);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
$invitation->update([
|
||||||
* Create a new user instance after a valid registration.
|
'user_id' => $user->id,
|
||||||
*
|
|
||||||
* @param array $data
|
|
||||||
* @return User
|
|
||||||
*/
|
|
||||||
protected function create(array $data)
|
|
||||||
{
|
|
||||||
return User::create([
|
|
||||||
'name' => $data['name'],
|
|
||||||
'email' => $data['email'],
|
|
||||||
'password' => bcrypt($data['password']),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
Auth::login($user);
|
||||||
|
|
||||||
|
return redirect()->route('backstage.concerts.index');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,18 @@ use Illuminate\Database\Eloquent\Model;
|
|||||||
|
|
||||||
class Invitation extends Model
|
class Invitation extends Model
|
||||||
{
|
{
|
||||||
|
protected $guarded = [];
|
||||||
|
|
||||||
public static function findByCode($code)
|
public static function findByCode($code)
|
||||||
{
|
{
|
||||||
return self::where('code', $code)->firstOrFail();
|
return self::where('code', $code)->firstOrFail();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
|
||||||
public function hasBeenUsed()
|
public function hasBeenUsed()
|
||||||
{
|
{
|
||||||
return $this->user_id !== null;
|
return $this->user_id !== null;
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
@extends('layouts.master')
|
||||||
|
|
||||||
|
@section('body')
|
||||||
|
<div class="container-fluid bg-soft">
|
||||||
|
<div class="full-height flex-center">
|
||||||
|
<div class="constrain constrain-sm flex-fit">
|
||||||
|
<form class="card p-xs-6" action="/register" method="POST">
|
||||||
|
{{ csrf_field() }}
|
||||||
|
<input type="hidden" name="invitation_code" value="{{ $invitation->code }}">
|
||||||
|
<h1 class="text-xl wt-light text-center m-xs-b-6">Join TicketBeast</h1>
|
||||||
|
<div class="form-group {{ $errors->first('email', 'has-error') }}">
|
||||||
|
<label class="form-label pseudo-hidden">Email address</label>
|
||||||
|
<div class="input-group">
|
||||||
|
<span class="input-group-addon">
|
||||||
|
@icon('user', 'text-dark-muted text-xs')
|
||||||
|
</span>
|
||||||
|
<input type="email" name="email" class="form-control" placeholder="Email address" value="{{ old('email') }}">
|
||||||
|
</div>
|
||||||
|
@if ($errors->has('email'))
|
||||||
|
<p class="text-danger m-xs-t-2">{{ $errors->first('email') }}</p>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
<div class="form-group {{ $errors->first('password', 'has-error') }}">
|
||||||
|
<label class="form-label pseudo-hidden">Password</label>
|
||||||
|
<div class="input-group">
|
||||||
|
<span class="input-group-addon">
|
||||||
|
@icon('lock-closed', 'text-dark-muted text-xs')
|
||||||
|
</span>
|
||||||
|
<input type="password" name="password" class="form-control" placeholder="Password">
|
||||||
|
</div>
|
||||||
|
@if ($errors->has('password'))
|
||||||
|
<p class="text-danger m-xs-t-2">{{ $errors->first('password') }}</p>
|
||||||
|
@endif
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-block btn-primary">Create Account</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endsection
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ Route::get('/login', 'Auth\LoginController@showLoginForm')->name('auth.show-logi
|
|||||||
Route::post('/login', 'Auth\LoginController@login')->name('auth.login');
|
Route::post('/login', 'Auth\LoginController@login')->name('auth.login');
|
||||||
Route::post('/logout', 'Auth\LoginController@logout')->name('auth.logout');
|
Route::post('/logout', 'Auth\LoginController@logout')->name('auth.logout');
|
||||||
|
|
||||||
|
Route::post('/register', 'Auth\RegisterController@register')->name('auth.register');
|
||||||
|
|
||||||
Route::get('/invitations/{code}', 'InvitationsController@show')->name('invitations.show');
|
Route::get('/invitations/{code}', 'InvitationsController@show')->name('invitations.show');
|
||||||
|
|
||||||
Route::group(['middleware' => 'auth', 'prefix' => 'backstage', 'namespace' => 'Backstage'], function () {
|
Route::group(['middleware' => 'auth', 'prefix' => 'backstage', 'namespace' => 'Backstage'], function () {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace Tests\Feature;
|
|||||||
use App\User;
|
use App\User;
|
||||||
use App\Invitation;
|
use App\Invitation;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
use Illuminate\Foundation\Testing\WithFaker;
|
use Illuminate\Foundation\Testing\WithFaker;
|
||||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
|
||||||
@@ -49,4 +50,30 @@ class AcceptInvitationTest extends TestCase
|
|||||||
|
|
||||||
$response->assertStatus(404);
|
$response->assertStatus(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function registering_with_a_valid_invitation_code()
|
||||||
|
{
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
|
||||||
|
$invitation = factory(Invitation::class)->create([
|
||||||
|
'user_id' => null,
|
||||||
|
'code' => 'TESTCODE1234',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->post('/register', [
|
||||||
|
'email' => 'john@example.com',
|
||||||
|
'password' => 'secret',
|
||||||
|
'invitation_code' => 'TESTCODE1234',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response->assertRedirect('/backstage/concerts');
|
||||||
|
|
||||||
|
$this->assertEquals(1, User::count());
|
||||||
|
$user = User::first();
|
||||||
|
$this->assertAuthenticatedAs($user);
|
||||||
|
$this->assertEquals('john@example.com', $user->email);
|
||||||
|
$this->assertTrue(Hash::check('secret', $user->password));
|
||||||
|
$this->assertTrue($invitation->fresh()->user->is($user));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user