Guest User

Untitled

a guest
Apr 2nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. login() {
  2. this.errorMsg = '';
  3. this._authService.login( this.user )
  4. .takeUntil( this.destroy$ ).subscribe(
  5. ( response ) => {
  6. if ( response['is_on_test'] ) {
  7. this.showModal();
  8. }
  9. if ( this.redirect ) {
  10. const url = this._route.snapshot
  11. .queryParams[ 'returnUrl' ] || '/';
  12. this._router.navigate( [ url ] );
  13. }
  14. },
  15. ( rejection ) => {
  16. if ( rejection && rejection
  17. .hasOwnProperty( 'error' ) && rejection.error.non_field_errors ){
  18. for ( const error in rejection.error.non_field_errors ) {
  19. if ( rejection.error.non_field_errors
  20. .hasOwnProperty( error )) {
  21. this.errorMsg = rejection.error.non_field_errors[ error ];
  22. }
  23. }
  24. }
  25. });
  26. }
  27.  
  28. public showModal() {
  29. const dialogConf = new MatDialogConfig();
  30. dialogConf.disableClose = true;
  31. dialogConf.position = {
  32. 'top': '50'
  33. };
  34. this.dialog.open(UserModalComponent, dialogConf);
  35. }
  36.  
  37. public login( user: User ): Observable<any> {
  38. return Observable.concat(
  39. this.request( {
  40. 'method': 'POST',
  41. 'url': '/login/',
  42. 'body': {
  43. username: user.username,
  44. password: user.password
  45. }
  46. }),
  47. this.getUserData()
  48. );
  49. }
  50.  
  51. getUserData(): Observable<any> {
  52. return this.request( {
  53. 'url': '/user/'
  54. } ).do(
  55. ( user ) => {
  56. if ( user ) {
  57. this.user = user;
  58. }
  59. }
  60. ).catch(
  61. ( rejection ) => {
  62. this.user = null;
  63. return Observable.throw( rejection );
  64. }
  65. );
  66. }
  67.  
  68. export class UserModalComponent implements OnInit {
  69.  
  70. constructor(private dialogRef: MatDialogRef<any>,
  71. private _router: Router,
  72. private _route: ActivatedRoute) {}
  73.  
  74. ngOnInit() {
  75. }
  76.  
  77. public closeDialog() {
  78. const url = this._route.snapshot.queryParams['returnUrl'] || '/';
  79. this._router.navigate( [ url ] );
  80. this.dialogRef.close();
  81. }
  82. }
Add Comment
Please, Sign In to add comment