Update Login and Registration for new api configuration
This commit is contained in:
parent
f25a9293c2
commit
a5f17b80eb
@ -11,18 +11,19 @@ const httpOptions = {
|
|||||||
})
|
})
|
||||||
export class AuthService {
|
export class AuthService {
|
||||||
constructor(private http: HttpClient) {}
|
constructor(private http: HttpClient) {}
|
||||||
login(username: string, password: string): Observable<any> {
|
login(email: string, password: string): Observable<any> {
|
||||||
return this.http.post(AUTH_API + '/login', {
|
return this.http.post(AUTH_API + '/login', {
|
||||||
username,
|
email,
|
||||||
password,
|
password,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
register(username: string, password: string): Observable<any> {
|
register(email: string, username: string, password: string): Observable<any> {
|
||||||
return this.http.post(
|
return this.http.post(
|
||||||
AUTH_API + '/register',
|
AUTH_API + '/register',
|
||||||
{
|
{
|
||||||
username,
|
email,
|
||||||
password,
|
password,
|
||||||
|
username,
|
||||||
},
|
},
|
||||||
httpOptions
|
httpOptions
|
||||||
);
|
);
|
||||||
|
@ -14,21 +14,21 @@
|
|||||||
class="backgorund"
|
class="backgorund"
|
||||||
>
|
>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="username">Username</label>
|
<label for="email">Email</label>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="email"
|
||||||
class="form-control"
|
class="form-control"
|
||||||
name="username"
|
name="email"
|
||||||
[(ngModel)]="form.username"
|
[(ngModel)]="form.email"
|
||||||
required
|
required
|
||||||
#username="ngModel"
|
email
|
||||||
|
#email="ngModel"
|
||||||
/>
|
/>
|
||||||
<div
|
<div class="alert-danger" *ngIf="email.errors && f.submitted">
|
||||||
class="alert alert-danger"
|
<div *ngIf="email.errors?.['required']">Email is required</div>
|
||||||
role="alert"
|
<div *ngIf="email.errors?.['email']">
|
||||||
*ngIf="username.errors && f.submitted"
|
Email must be a valid email address
|
||||||
>
|
</div>
|
||||||
Username is required!
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
@ -10,7 +10,7 @@ import { Router } from '@angular/router';
|
|||||||
})
|
})
|
||||||
export class LoginComponent implements OnInit {
|
export class LoginComponent implements OnInit {
|
||||||
form: any = {
|
form: any = {
|
||||||
username: null,
|
email: null,
|
||||||
password: null,
|
password: null,
|
||||||
};
|
};
|
||||||
isLoggedIn = false;
|
isLoggedIn = false;
|
||||||
@ -39,15 +39,15 @@ export class LoginComponent implements OnInit {
|
|||||||
|
|
||||||
//onSubmit() saves valuable information in session storage
|
//onSubmit() saves valuable information in session storage
|
||||||
onSubmit(): void {
|
onSubmit(): void {
|
||||||
const { username, password } = this.form;
|
const { email, password } = this.form;
|
||||||
this.authService.login(username, password).subscribe(
|
this.authService.login(email, password).subscribe(
|
||||||
(data) => {
|
(data) => {
|
||||||
this.tokenStorage.saveToken(data.data.token);
|
this.tokenStorage.saveToken(data.data.token);
|
||||||
this.tokenStorage.saveUser(data.data);
|
this.tokenStorage.saveUser(data.data);
|
||||||
|
|
||||||
this.isLoginFailed = false;
|
this.isLoginFailed = false;
|
||||||
this.isLoggedIn = true;
|
this.isLoggedIn = true;
|
||||||
this.accountName = username;
|
this.accountName = email;
|
||||||
this.router.navigate(['']);
|
this.router.navigate(['']);
|
||||||
},
|
},
|
||||||
(err) => {
|
(err) => {
|
||||||
|
@ -35,6 +35,24 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="email">Email</label>
|
||||||
|
<input
|
||||||
|
type="email"
|
||||||
|
class="form-control"
|
||||||
|
name="email"
|
||||||
|
[(ngModel)]="form.email"
|
||||||
|
required
|
||||||
|
email
|
||||||
|
#email="ngModel"
|
||||||
|
/>
|
||||||
|
<div class="alert-danger" *ngIf="email.errors && f.submitted">
|
||||||
|
<div *ngIf="email.errors?.['required']">Email is required</div>
|
||||||
|
<div *ngIf="email.errors?.['email']">
|
||||||
|
Email must be a valid email address
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="password">Password</label>
|
<label for="password">Password</label>
|
||||||
<input
|
<input
|
||||||
|
@ -9,8 +9,9 @@ import { AuthService } from '../../Services/auth.service';
|
|||||||
})
|
})
|
||||||
export class RegisterComponent implements OnInit {
|
export class RegisterComponent implements OnInit {
|
||||||
form: any = {
|
form: any = {
|
||||||
username: null,
|
email: null,
|
||||||
password: null,
|
password: null,
|
||||||
|
username: null,
|
||||||
};
|
};
|
||||||
isSuccessful = false;
|
isSuccessful = false;
|
||||||
isSignUpFailed = false;
|
isSignUpFailed = false;
|
||||||
@ -23,8 +24,8 @@ export class RegisterComponent implements OnInit {
|
|||||||
constructor(private authService: AuthService, private router: Router) {}
|
constructor(private authService: AuthService, private router: Router) {}
|
||||||
ngOnInit(): void {}
|
ngOnInit(): void {}
|
||||||
onSubmit(): void {
|
onSubmit(): void {
|
||||||
const { username, password } = this.form;
|
const { email, username, password } = this.form;
|
||||||
this.authService.register(username, password).subscribe(
|
this.authService.register(email, username, password).subscribe(
|
||||||
(data) => {
|
(data) => {
|
||||||
this.isSuccessful = true;
|
this.isSuccessful = true;
|
||||||
this.isSignUpFailed = false;
|
this.isSignUpFailed = false;
|
||||||
|
Loading…
Reference in New Issue
Block a user