Reputation: 23
I have a view file named "register" and I'm using AJAX to validate form. This view page is a modal window containing register form. If type url like "http://localhost/register" it will proceed to the register view. I'm new in laravel and I can not understand how to prevent from accessing this view. Please, help :(
register.blade.php file:
<div class="modal fade" id="register">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<p>Sign up</p>
</div>
<div class="modal-body">
<div class="alert alert-danger alert-reg" style="display:none"></div>
<form method="POST" action="{{ route('register') }}">
@csrf
<div class="form-group row">
<label for="name" class="col-md-5 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-7">
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" autofocus>
@if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-5 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-7">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}">
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-5 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-7">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password">
@if ($errors->has('password'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-5 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-7">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation">
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary regist">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
register.blade.php is included in welcome.blade.php:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<!-- Styles -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css">
<!-- Scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="{{ url('public/css/css.css') }}" />
<script src="{{ url("public/js/register.js") }}"></script>
<script src="{{ url("public/js/login.js") }}"></script>
<title>Harry Potter</title>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Harry Potter</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="#">Home</a></li>
<li><a href="#">Map</a></li>
<li><a href="#">Contact us</a></li>
<li class="dropdown">
<a href="/" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Info<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="/">Info</a></li>
<li role="separator" class="divider"></li>
<li><a href="/">Info</a></li>
</ul>
</li>
</ul>
@include('layouts.app')
</div>
</div>
</nav>
<div id="carousel" class="carousel slide" data-ride="carousel">
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="{{ url("public/img/join.jpg") }}" alt="" />
<div class="carousel-caption">
<h2>Chat and share with other fans!</h2>
<p><a href="{{ url("/entrance") }}">Get started!</a></p>
</div>
</div>
<div class="item">
<img src="{{ url("public/img/xmashp.png") }}" alt="" />
<div class="carousel-caption">
<h2>Choose your side!</h2>
</div>
</div>
<div class="item">
<img src="{{ url("public/img/order.jpg") }}" alt="" />
<div class="carousel-caption">
<h2>Join to the Order of the Phoenix!</h2>
<p><a href="#">Learn more!</a></p>
</div>
</div>
<div class="item">
<img src="{{ url("public/img/voldemort.jpg") }}" alt="" />
<div class="carousel-caption">
<h2>Join to the Death Eaters!</h2>
<p><a href="#">Learn more!</a></p>
</div>
</div>
</div>
<a class="left carousel-control" href="#carousel" role="button" data-slide="prev">
<span class="icon-prev" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel" role="button" data-slide="next">
<span class="icon-next" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
@include('auth.login')
@include('auth.register')
</body>
</html>
web.php file:
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/welcome', 'HomeController@index');
Route::get('auth/register', 'Auth\RegisterController@register');
Route::post('auth/register', 'Auth\RegisterController@store');
Route::get('auth/login', 'Auth\LoginController@signIn');
Route::post('auth/login', 'Auth\LoginController@authenticated');
Upvotes: 0
Views: 1229
Reputation: 2454
As per OP's request:
I want to restrict everyone from accessing the
/register
page.
There's a few ways you can do this:
showRegistrationForm()
function1 - Don't register the routes (easiest and best solution)
You're using Auth::routes();
to register your auth related routes, change it to this:
Auth::routes(['register' => false]);
This won't register the /register
route and make the page not accessible.
2 - Override the showRegistrationForm()
function
Make your way over to: vendor\laravel\framework\src\Illuminate\Foundation\Auth\RegistersUsers.php
/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
return view('auth.login'); // this changed
// you can also use this to return with a message
// return view('auth.login')->with('error', 'Registration disabled.');
}
The big downside of this solution is that it will get overwritten when Laravel updates so I suggest you use the first one.
Upvotes: 2