Validate User forms only with backend

This commit is contained in:
Bubka 2020-01-09 11:14:39 +01:00
parent a1c0e2cee3
commit 77b6ac3e3f
3 changed files with 67 additions and 89 deletions

View File

@ -15,8 +15,22 @@ class UserController extends Controller
* log a user in
* @return [type] [description]
*/
public function login()
public function login(Request $request)
{
$messages = [
'email.exists' => 'No account found using this email',
];
$validator = Validator::make($request->all(), [
'email' => 'required|exists:users,email',
'password' => 'required',
], $messages);
if ($validator->fails()) {
return response()->json(['error' => $validator->errors()], 400);
}
$credentials = [
'email' => request('email'),
'password' => request('password')
@ -57,7 +71,7 @@ public function register(Request $request)
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email',
'password' => 'required',
'password' => 'required|confirmed',
]);
if ($validator->fails()) {

View File

@ -7,16 +7,16 @@
<div class="field">
<label class="label">Email</label>
<div class="control">
<input id="email" type="email" class="input" v-model="email" v-bind:class="{ 'is-danger' : emailMissing }" required autofocus />
<input id="email" type="email" class="input" v-model="email" v-bind:class="{ 'is-danger' : errors.email }" required autofocus />
</div>
<p class="help is-danger" v-if="emailMissing">Field required</p>
<p class="help is-danger" v-if="errors.email">{{ errors.email.toString() }}</p>
</div>
<div class="field">
<label class="label">Password</label>
<div class="control">
<input id="password" type="password" class="input" v-model="password" v-bind:class="{ 'is-danger' : passwordMissing }" required />
<input id="password" type="password" class="input" v-model="password" v-bind:class="{ 'is-danger' : errors.password }" required />
</div>
<p class="help is-danger" v-if="passwordMissing">Field required</p>
<p class="help is-danger" v-if="errors.password">{{ errors.password.toString() }}</p>
</div>
<div class="field">
<div class="control">
@ -24,10 +24,6 @@
</div>
</div>
</form>
<br />
<span class="tag is-danger" v-if="errorMessage">
{{ errorMessage }}
</span>
</div>
</div>
<div class="columns is-mobile is-centered">
@ -45,44 +41,34 @@
data(){
return {
email : '',
emailMissing : false,
password : '',
passwordMissing : false,
errorMessage : '',
errors: {}
}
},
methods : {
handleSubmit(e){
e.preventDefault()
this.emailMissing = (this.email.length === 0) ? true : false;
this.passwordMissing = (this.password.length === 0) ? true : false;
axios.post('api/login', {
email: this.email,
password: this.password
})
.then(response => {
localStorage.setItem('user',response.data.success.name)
localStorage.setItem('jwt',response.data.success.token)
if (this.password.length > 0 && this.email.length > 0) {
axios.post('api/login', {
email: this.email,
password: this.password
})
.then(response => {
localStorage.setItem('user',response.data.success.name)
localStorage.setItem('jwt',response.data.success.token)
if (localStorage.getItem('jwt') != null){
this.$router.go('/');
}
})
.catch(e => {
console.error(e);
if (e.response.status === 401) {
this.errorMessage = 'bad credential, please try again'
}
else {
this.errorMessage = 'An error occured, please retry'
}
});
}
if (localStorage.getItem('jwt') != null){
this.$router.go('/');
}
})
.catch(error => {
if (error.response.status === 400) {
this.errors = error.response.data.error
}
else {
this.errors['password'] = [ 'Password do not match' ]
}
});
}
},
beforeRouteEnter (to, from, next) {

View File

@ -7,30 +7,30 @@
<div class="field">
<label class="label">Name</label>
<div class="control">
<input id="name" type="email" class="input" v-model="name" v-bind:class="{ 'is-danger' : nameMissing }" required autofocus />
<input id="name" type="text" class="input" v-model="name" v-bind:class="{ 'is-danger' : errors.name }" required autofocus />
</div>
<p class="help is-danger" v-if="nameMissing">Field required</p>
<p class="help is-danger" v-if="errors.name">{{ errors.name.toString() }}</p>
</div>
<div class="field">
<label class="label">Email</label>
<div class="control">
<input id="email" type="email" class="input" v-model="email" v-bind:class="{ 'is-danger' : emailMissing }" required />
<input id="email" type="email" class="input" v-model="email" v-bind:class="{ 'is-danger' : errors.email }" required />
</div>
<p class="help is-danger" v-if="emailMissing">Field required</p>
<p class="help is-danger" v-if="errors.email">{{ errors.email.toString() }}</p>
</div>
<div class="field">
<label class="label">Password</label>
<div class="control">
<input id="password" type="password" class="input" v-model="password" v-bind:class="{ 'is-danger' : passwordMissing }" required />
<input id="password" type="password" class="input" v-model="password" v-bind:class="{ 'is-danger' : errors.password }" required />
</div>
<p class="help is-danger" v-if="passwordMissing">Field required</p>
<p class="help is-danger" v-if="errors.password">{{ errors.password.toString() }}</p>
</div>
<div class="field">
<label class="label">Confirm Password</label>
<div class="control">
<input id="password-confirm" type="password" class="input" v-model="password_confirmation" v-bind:class="{ 'is-danger' : passwordConfirmationMissing }" required />
<input id="password_confirmation" type="password" class="input" v-model="password_confirmation" v-bind:class="{ 'is-danger' : errors.passwordConfirmation }" required />
</div>
<p class="help is-danger" v-if="passwordConfirmationMissing">Field required</p>
<p class="help is-danger" v-if="errors.passwordConfirmation">{{ errors.passwordConfirmation.toString() }}</p>
</div>
<div class="field">
<div class="control">
@ -38,10 +38,6 @@
</div>
</div>
</form>
<br />
<span class="tag is-danger" v-if="errorMessage">
{{ errorMessage }}
</span>
</div>
</div>
<div class="columns is-mobile is-centered">
@ -59,55 +55,37 @@
data(){
return {
name : '',
nameMissing : false,
email : '',
emailMissing : false,
password : '',
passwordMissing : false,
password_confirmation : '',
passwordConfirmationMissing : false,
errorMessage : '',
errors: {}
}
},
methods : {
handleSubmit(e) {
e.preventDefault()
this.nameMissing = (this.name.length === 0) ? true : false;
this.emailMissing = (this.email.length === 0) ? true : false;
this.passwordMissing = (this.password.length === 0) ? true : false;
this.passwordConfirmationMissing = (this.password_confirmation.length === 0) ? true : false;
axios.post('api/register', {
name: this.name,
email: this.email,
password: this.password,
password_confirmation : this.password_confirmation
})
.then(response => {
localStorage.setItem('user',response.data.success.name)
localStorage.setItem('jwt',response.data.success.token)
if( this.nameMissing || this.emailMissing || this.passwordMissing || this.passwordConfirmationMissing ) {
return false;
}
if (this.password === this.password_confirmation && this.password.length > 0)
{
axios.post('api/register', {
name: this.name,
email: this.email,
password: this.password,
c_password : this.password_confirmation
})
.then(response => {
localStorage.setItem('user',response.data.success.name)
localStorage.setItem('jwt',response.data.success.token)
if (localStorage.getItem('jwt') != null){
this.$router.go('/');
}
})
.catch(error => {
console.error(error);
this.errorMessage = error.message
});
} else {
this.errorMessage = 'Passwords do not match'
return false;
}
if (localStorage.getItem('jwt') != null){
this.$router.go('/');
}
})
.catch(error => {
this.errors = error.response.data.error
});
}
},
beforeRouteEnter (to, from, next) {
if (localStorage.getItem('jwt')) {
return next('/');