Skip to content

Commit 7783cf8

Browse files
committed
handle login validation - #202
1 parent 2d4d82a commit 7783cf8

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

angular/app/components/login-form/login-form.component.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
class LoginFormController {
2-
constructor($auth) {
2+
constructor($auth, ToastService) {
33
'ngInject';
44

55
this.$auth = $auth;
6+
this.ToastService = ToastService;
67

78
this.email = '';
89
this.password = '';
@@ -16,11 +17,20 @@ class LoginFormController {
1617

1718
this.$auth.login(user)
1819
.then((response) => {
19-
//save token in localStorage
2020
this.$auth.setToken(response.data);
2121

2222
//
23-
});
23+
})
24+
.catch(this.failedLogin.bind(this));
25+
}
26+
27+
failedLogin(response) {
28+
if (response.status === 422) {
29+
for (var error in response.data.errors) {
30+
return this.ToastService.error(response.data.errors[error][0]);
31+
}
32+
}
33+
this.ToastService.error(response.statusText);
2434
}
2535
}
2636

app/Http/Controllers/Auth/AuthController.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class AuthController extends Controller
1212
{
1313
public function postLogin(Request $request)
1414
{
15+
$this->validate($request, [
16+
'email' => 'required|email',
17+
'password' => 'required|min:8',
18+
]);
19+
1520
$credentials = $request->only('email', 'password');
1621

1722
try {
@@ -36,9 +41,9 @@ public function postRegister()
3641
'password' => 'required|min:8',
3742
]);
3843

39-
$user = new User;
40-
$user->name = trim($request->name);
41-
$user->email = trim(strtolower($request->email));
44+
$user = new User;
45+
$user->name = trim($request->name);
46+
$user->email = trim(strtolower($request->email));
4247
$user->password = bcrypt($request->password);
4348
$user->save();
4449

0 commit comments

Comments
 (0)