Robert Nicjoo Robert Nicjoo - 3 years ago 72
PHP Question

Laravel custom auth route issue

I tried to make custom Auth routes for my app because I needed to use referral system and default auth wouldn't let users register with referral link so I've make custom one, now I can register with refellal link and save id of user in new user table as referred person. But the issue is now i cannot register without referral link! and it keeps refreshing register page my guess is register form

action
link but i'm not sure how to fix it.

This is my
AuthController


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\User;
use Cookie;
use Guard;
use Auth;

class AuthController extends Controller
{

public function __construct()
{
$this->middleware('guest');
}

// Show register form
public function showRegisterForm(Request $request)
{
$user = User::where('affiliate_id',$request->query('ref'))->first();
$referred_by = count($user) > 0 ? $user->id : '';
return view('auth.register',compact('referred_by'));
}

protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'username' => 'required|string|max:255|unique:users',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
'g-recaptcha-response' => 'required|captcha',
]);
}

protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'affiliate_id' => str_random(10),
'referred_by' => $data['referred_by'],
]);
}

public function register(Request $request, $referred_by=0)
{
$request->merge(['referred_by' => $referred_by]);
$this->validator($request->all())->validate();
$this->create($request->all());

Auth::attempt($request->only('username','password'));
return redirect()->to('/home');

}
}


Here is my register form:

<form class="form-horizontal" method="POST" action="/register/{{$referred_by}}">
{{ csrf_field() }}

<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Name</label>

<div class="col-md-6">
<input id="name" type="text" class="form-control" name="name" value="{{ old('name') }}" required autofocus>

@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
<label for="username" class="col-md-4 control-label">Username</label>

<div class="col-md-6">
<input id="username" type="text" class="form-control" name="username" value="{{ old('username') }}" required autofocus>

@if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>

<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required>

@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>

<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>

@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group">
<label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>

<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
</div>
</div>
@captcha()
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Register
</button>
<a href="{{route('login')}}" class="btn btn-warning">
Already member?
</a>
</div>
</div>
</form>


I also have roles and I need to define default role for users who just register so doesn't need i give them role manually.

Update:

Route::get('/login',['as'=>'login', 'uses' => 'Auth\LoginController@showLoginForm']);
Route::post('/login', ['uses'=>'Auth\LoginController@login']);
Route::get('/logout',['as'=>'logout', 'uses'=>'Auth\LoginController@logout']);
Route::get('/register', ['as' => 'register', 'uses' => 'AuthController@showRegisterForm']);
Route::post('/register/{referred_by?}', 'AuthController@register');
Route::post('password/email', ['as'=>'password.email', 'uses'=>'Auth\ForgotPasswordController@sendResetLinkEmail']);
Route::get('password/reset', ['as'=>'password.request', 'uses'=>'Auth\ForgotPasswordController@showLinkRequestForm']);
Route::post('password/reset', ['as'=>'password.request', 'uses'=>'Auth\ResetPasswordController@reset']);
Route::get('password/reset/{token}', ['as'=>'password.reset', 'uses'=>'Auth\ResetPasswordController@showResetForm']);
Route::post('logout', ['as'=>'logout', 'uses'=>'Auth\LoginController@logout']);


Thanks.

Answer Source

There is small thing you need to change.

$referred_by = count($user) > 0 ? $user->id : '';

Replace this line with

$referred_by = count($user) > 0 ? $user->id : 0;

Solution Explaination

When you are passing '' to reffered_by, it is considered nothing and URL would look like www.example.com/register and

Route::get('/register', ['as' => 'register', 'uses' => 'AuthController@showRegisterForm']);

this route gets hit and it shows registration form again, When you actually want

Route::post('/register/{referred_by?}', 'AuthController@register');

this route to get hit. So avoid passing '' and pass 0, so URL would look like www.example.com/register/0 and it will hit correct route and do what you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download