mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-02-08 22:49:25 +01:00
Validate User forms only with backend
This commit is contained in:
parent
a1c0e2cee3
commit
77b6ac3e3f
@ -15,8 +15,22 @@ class UserController extends Controller
|
|||||||
* log a user in
|
* log a user in
|
||||||
* @return [type] [description]
|
* @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 = [
|
$credentials = [
|
||||||
'email' => request('email'),
|
'email' => request('email'),
|
||||||
'password' => request('password')
|
'password' => request('password')
|
||||||
@ -57,7 +71,7 @@ public function register(Request $request)
|
|||||||
$validator = Validator::make($request->all(), [
|
$validator = Validator::make($request->all(), [
|
||||||
'name' => 'required',
|
'name' => 'required',
|
||||||
'email' => 'required|email',
|
'email' => 'required|email',
|
||||||
'password' => 'required',
|
'password' => 'required|confirmed',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
|
@ -7,16 +7,16 @@
|
|||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label">Email</label>
|
<label class="label">Email</label>
|
||||||
<div class="control">
|
<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>
|
</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>
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label">Password</label>
|
<label class="label">Password</label>
|
||||||
<div class="control">
|
<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>
|
</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>
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<div class="control">
|
<div class="control">
|
||||||
@ -24,10 +24,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<br />
|
|
||||||
<span class="tag is-danger" v-if="errorMessage">
|
|
||||||
{{ errorMessage }}
|
|
||||||
</span>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="columns is-mobile is-centered">
|
<div class="columns is-mobile is-centered">
|
||||||
@ -45,44 +41,34 @@
|
|||||||
data(){
|
data(){
|
||||||
return {
|
return {
|
||||||
email : '',
|
email : '',
|
||||||
emailMissing : false,
|
|
||||||
password : '',
|
password : '',
|
||||||
passwordMissing : false,
|
errors: {}
|
||||||
errorMessage : '',
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods : {
|
methods : {
|
||||||
handleSubmit(e){
|
handleSubmit(e){
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|
||||||
this.emailMissing = (this.email.length === 0) ? true : false;
|
axios.post('api/login', {
|
||||||
this.passwordMissing = (this.password.length === 0) ? true : false;
|
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) {
|
if (localStorage.getItem('jwt') != null){
|
||||||
|
this.$router.go('/');
|
||||||
axios.post('api/login', {
|
}
|
||||||
email: this.email,
|
})
|
||||||
password: this.password
|
.catch(error => {
|
||||||
})
|
if (error.response.status === 400) {
|
||||||
.then(response => {
|
this.errors = error.response.data.error
|
||||||
localStorage.setItem('user',response.data.success.name)
|
}
|
||||||
localStorage.setItem('jwt',response.data.success.token)
|
else {
|
||||||
|
this.errors['password'] = [ 'Password do not match' ]
|
||||||
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'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
beforeRouteEnter (to, from, next) {
|
beforeRouteEnter (to, from, next) {
|
||||||
|
@ -7,30 +7,30 @@
|
|||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label">Name</label>
|
<label class="label">Name</label>
|
||||||
<div class="control">
|
<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>
|
</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>
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label">Email</label>
|
<label class="label">Email</label>
|
||||||
<div class="control">
|
<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>
|
</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>
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label">Password</label>
|
<label class="label">Password</label>
|
||||||
<div class="control">
|
<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>
|
</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>
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label">Confirm Password</label>
|
<label class="label">Confirm Password</label>
|
||||||
<div class="control">
|
<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>
|
</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>
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<div class="control">
|
<div class="control">
|
||||||
@ -38,10 +38,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<br />
|
|
||||||
<span class="tag is-danger" v-if="errorMessage">
|
|
||||||
{{ errorMessage }}
|
|
||||||
</span>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="columns is-mobile is-centered">
|
<div class="columns is-mobile is-centered">
|
||||||
@ -59,55 +55,37 @@
|
|||||||
data(){
|
data(){
|
||||||
return {
|
return {
|
||||||
name : '',
|
name : '',
|
||||||
nameMissing : false,
|
|
||||||
email : '',
|
email : '',
|
||||||
emailMissing : false,
|
|
||||||
password : '',
|
password : '',
|
||||||
passwordMissing : false,
|
|
||||||
password_confirmation : '',
|
password_confirmation : '',
|
||||||
passwordConfirmationMissing : false,
|
errors: {}
|
||||||
errorMessage : '',
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
methods : {
|
methods : {
|
||||||
handleSubmit(e) {
|
handleSubmit(e) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|
||||||
this.nameMissing = (this.name.length === 0) ? true : false;
|
axios.post('api/register', {
|
||||||
this.emailMissing = (this.email.length === 0) ? true : false;
|
name: this.name,
|
||||||
this.passwordMissing = (this.password.length === 0) ? true : false;
|
email: this.email,
|
||||||
this.passwordConfirmationMissing = (this.password_confirmation.length === 0) ? true : false;
|
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 ) {
|
if (localStorage.getItem('jwt') != null){
|
||||||
return false;
|
this.$router.go('/');
|
||||||
}
|
}
|
||||||
|
})
|
||||||
if (this.password === this.password_confirmation && this.password.length > 0)
|
.catch(error => {
|
||||||
{
|
this.errors = error.response.data.error
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
beforeRouteEnter (to, from, next) {
|
beforeRouteEnter (to, from, next) {
|
||||||
if (localStorage.getItem('jwt')) {
|
if (localStorage.getItem('jwt')) {
|
||||||
return next('/');
|
return next('/');
|
||||||
|
Loading…
Reference in New Issue
Block a user