Commit e3983e78 by Muhammad Usman

auth module code refactoring

parent 7bbd37e6
......@@ -4,8 +4,8 @@ import { takeUntil } from 'rxjs/operators';
import { BaseComponent } from '../../base/base.component';
import { EXTERNAL_LINKS } from '../../config/constants';
import { AuthService } from '../../services/auth.service';
import { HelperService } from '../../services/helper.service';
import { ToasterService } from '../../services/toaster.service';
import { UtilsService } from '../../../oneit/services/utils.service';
@Component({
selector: 'app-forgot-password',
......@@ -23,30 +23,29 @@ export class ForgotPasswordComponent extends BaseComponent {
@ViewChild('form') form: NgForm;
constructor(
private hs: HelperService,
private ts: ToasterService,
private as: AuthService
private toasterService: ToasterService,
private authService: AuthService,
private utilsService: UtilsService
) {
super();
}
forgot(): void {
if (this.form.invalid) {
return this.hs.validateAllFormFields(this.form);
return this.utilsService.showAllErrorMessages();
}
this.isLoading = true;
this.as.forgot(this.forgotForm)
this.authService.forgot(this.forgotForm)
.pipe(takeUntil(this.componentInView))
.subscribe(response => {
this.isLoading = false;
this.hs.oneItHttpResponse(response);
if (response.emailSent) {
this.emailSent = true;
this.ts.success('Please check your email for instructions.');
this.toasterService.success('Please check your email for instructions.');
}
}, err => {
this.isLoading = false;
this.hs.handleHttpError(err);
this.utilsService.handleError(err);
});
}
......
......@@ -6,7 +6,6 @@ import { AppService } from '../../../app.service';
import { BaseComponent } from '../../base/base.component';
import { EXTERNAL_LINKS } from '../../config/constants';
import { AuthService } from '../../services/auth.service';
import { HelperService } from '../../services/helper.service';
import { UtilsService } from '../../../oneit/services/utils.service';
@Component({
......@@ -26,40 +25,38 @@ export class LoginComponent extends BaseComponent {
@ViewChild('form') form: NgForm;
constructor(
private hs: HelperService,
private as: AuthService,
private r: Router,
private aps: AppService,
private us: UtilsService
private authService: AuthService,
private router: Router,
private appService: AppService,
private utilsService: UtilsService
) {
super();
}
login(): void {
if (this.form.invalid) {
return this.us.showAllErrorMessages();
return this.utilsService.showAllErrorMessages();
}
this.isLoading = true;
this.as.login(this.loginForm)
this.authService.login(this.loginForm)
.pipe(takeUntil(this.componentInView))
.subscribe(response => {
this.isLoading = false;
this.hs.oneItHttpResponse(response);
if (response.status === 'error') {
this.loginForm.password = '';
return;
}
this.r.navigate(['/my-career-web/dashboard']);
this.router.navigate(['/my-career-web/dashboard']);
}, err => {
this.hs.handleHttpError(err);
this.utilsService.handleError(err);
this.isLoading = false;
});
}
socialMediaLogin(type: 'google' | 'linkedIn') {
this.isLoading = true;
this.aps.socicalMediaLogin(type)
this.appService.socicalMediaLogin(type)
.pipe(takeUntil(this.componentInView))
.subscribe(data => {
this.isLoading = false;
......@@ -70,7 +67,7 @@ export class LoginComponent extends BaseComponent {
},
err => {
this.isLoading = false;
this.hs.handleHttpError(err);
this.utilsService.handleError(err);
}
)
}
......
......@@ -5,8 +5,8 @@ import { takeUntil } from 'rxjs/operators';
import { BaseComponent } from '../../base/base.component';
import { EXTERNAL_LINKS } from '../../config/constants';
import { AuthService } from '../../services/auth.service';
import { HelperService } from '../../services/helper.service';
import { ToasterService } from '../../services/toaster.service';
import { UtilsService } from '../../../oneit/services/utils.service';
@Component({
selector: 'app-register',
......@@ -32,36 +32,34 @@ export class RegisterComponent extends BaseComponent {
@ViewChild('form') form: NgForm;
constructor(
private hs: HelperService,
private as: AuthService,
private ts: ToasterService,
private r: Router
private authService: AuthService,
private toasterService: ToasterService,
private router: Router,
private utilsService: UtilsService
) {
super();
}
register(): void {
if (this.form.invalid) {
return this.hs.validateAllFormFields(this.form);
return this.utilsService.showAllErrorMessages();
}
this.registerForm.email = this.registerForm.username;
if (!this.registerForm.was_referred) {
delete this.registerForm.ReferralCode;
}
this.isLoading = true;
this.as.register(this.registerForm)
this.authService.register(this.registerForm)
.pipe(takeUntil(this.componentInView))
.subscribe(response => {
this.isLoading = false;
this.hs.oneItHttpResponse(response);
if (response.registered) {
this.ts.success('Registration successful');
this.r.navigate(['/my-career-web/dashboard']);
this.toasterService.success('Registration successful');
this.router.navigate(['/my-career-web/dashboard']);
}
}, err => {
this.isLoading = false;
this.hs.handleHttpError(err);
this.utilsService.handleError(err);
});
}
......
......@@ -3,6 +3,7 @@ import { Component, OnInit } from '@angular/core';
import { MsgsService } from '../../../oneit/services/msgs.service';
import { takeUntil } from 'rxjs/operators';
import { BaseComponent } from '../../base/base.component';
import { ToasterService } from '../../services/toaster.service';
@Component({
selector: 'app-wrapper',
......@@ -21,7 +22,8 @@ import { BaseComponent } from '../../base/base.component';
export class WrapperComponent extends BaseComponent implements OnInit {
constructor(
private msgService: MsgsService
private msgService: MsgsService,
private toasterService: ToasterService
) {
super();
}
......@@ -29,8 +31,15 @@ export class WrapperComponent extends BaseComponent implements OnInit {
ngOnInit() {
this.msgService.errorMsgsUpdated
.pipe(takeUntil(this.componentInView))
.subscribe(error => {
// console.log(error);
.subscribe(errors => {
console.log(errors);
if(Array.isArray(errors)) {
errors.forEach((err, i) => {
if (i > 0) {
this.toasterService.error(err.detail);
}
})
}
});
}
......
......@@ -18,25 +18,53 @@ export class AuthService {
login(formData): Observable<any> {
formData.environment = this.env;
return this.apiService.post('svc/Login', formData);
return this.apiService.post('svc/Login', formData)
.map(
data => {
if (this.utilsService.isSuccessfulResponse(data)) {
return data;
}
}
);
}
forgot(formData): Observable<any> {
formData.environment = this.env;
return this.apiService.post('svc/RequestPasswordReset', formData);
return this.apiService.post('svc/RequestPasswordReset', formData)
.map(
data => {
if (this.utilsService.isSuccessfulResponse(data)) {
return data;
}
}
);
}
register(formData): Observable<any> {
formData.environment = this.env;
return this.apiService.post('svc/RegisterMyCareerUser', formData);
return this.apiService.post('svc/RegisterMyCareerUser', formData)
.map(
data => {
if (this.utilsService.isSuccessfulResponse(data)) {
return data;
}
}
);
}
reset(formData): Observable<any> {
formData.environment = this.env;
return this.apiService.post('svc/PasswordChange', formData);
return this.apiService.post('svc/PasswordChange', formData)
.map(
data => {
if (this.utilsService.isSuccessfulResponse(data)) {
return data;
}
}
);
}
logout(formData = {} as any): Observable<any> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment