Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- login() {
- this.errorMsg = '';
- this._authService.login( this.user )
- .takeUntil( this.destroy$ ).subscribe(
- ( response ) => {
- if ( response['is_on_test'] ) {
- this.showModal();
- }
- if ( this.redirect ) {
- const url = this._route.snapshot
- .queryParams[ 'returnUrl' ] || '/';
- this._router.navigate( [ url ] );
- }
- },
- ( rejection ) => {
- if ( rejection && rejection
- .hasOwnProperty( 'error' ) && rejection.error.non_field_errors ){
- for ( const error in rejection.error.non_field_errors ) {
- if ( rejection.error.non_field_errors
- .hasOwnProperty( error )) {
- this.errorMsg = rejection.error.non_field_errors[ error ];
- }
- }
- }
- });
- }
- public showModal() {
- const dialogConf = new MatDialogConfig();
- dialogConf.disableClose = true;
- dialogConf.position = {
- 'top': '50'
- };
- this.dialog.open(UserModalComponent, dialogConf);
- }
- public login( user: User ): Observable<any> {
- return Observable.concat(
- this.request( {
- 'method': 'POST',
- 'url': '/login/',
- 'body': {
- username: user.username,
- password: user.password
- }
- }),
- this.getUserData()
- );
- }
- getUserData(): Observable<any> {
- return this.request( {
- 'url': '/user/'
- } ).do(
- ( user ) => {
- if ( user ) {
- this.user = user;
- }
- }
- ).catch(
- ( rejection ) => {
- this.user = null;
- return Observable.throw( rejection );
- }
- );
- }
- export class UserModalComponent implements OnInit {
- constructor(private dialogRef: MatDialogRef<any>,
- private _router: Router,
- private _route: ActivatedRoute) {}
- ngOnInit() {
- }
- public closeDialog() {
- const url = this._route.snapshot.queryParams['returnUrl'] || '/';
- this._router.navigate( [ url ] );
- this.dialogRef.close();
- }
- }
Add Comment
Please, Sign In to add comment