Guest User

Untitled

a guest
Mar 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. import {
  2. isAbsoluteURL,
  3. isTrustedURL,
  4. appendParamsToURL,
  5. redirectToURL
  6. } from '../url-fns';
  7.  
  8. class LoginController {
  9. /*@ngInject*/
  10. constructor($rootScope, $log, $state, commonAuthUser) {
  11. this.$rootScope = $rootScope;
  12. this.$log = $log;
  13. this.$state = $state;
  14.  
  15. this.commonAuthUser = commonAuthUser;
  16.  
  17. this.user = {
  18. login: '',
  19. password: ''
  20. };
  21.  
  22. this.isPasswordVisible = false;
  23. }
  24.  
  25. redirectToAccount() {
  26. const { accountAppUrl } = this.commonAuthUser.config;
  27. const token = this.commonAuthUser.getToken();
  28.  
  29. redirectToURL(`${accountAppUrl}/#/auth/check?token=${token}`);
  30. }
  31.  
  32. redirectToTarget(){
  33. const { callback: callbackURL } = this.$state.params;
  34.  
  35. if (callbackURL) {
  36. if (!isAbsoluteURL(callbackURL)) {
  37. redirectToURL(callbackURL);
  38. return;
  39. }
  40.  
  41. if (isTrustedURL(callbackURL)) {
  42. redirectToURL(appendParamsToURL(callbackURL, {token: this.commonAuthUser.getToken()}));
  43. return;
  44. }
  45. }
  46.  
  47. this.$state.go(this.commonAuthUser.getTargetState(), {}, { location: 'replace' });
  48. }
  49.  
  50. submit(form) {
  51. if (form.$valid && !this.isLoading) {
  52. this.isLoading = true;
  53. this.error = null;
  54. this.commonAuthUser
  55. .login(this.user.login, this.user.password)
  56. .then(data => {
  57. this.$log.log('Logged in:', data);
  58.  
  59. if (data.status === 'New') {
  60. // NOTE: redirect to account app, to allow fill in user account data
  61. this.redirectToAccount();
  62. }
  63.  
  64. this.redirectToTarget();
  65. })
  66. .catch(response => {
  67. const { status, data } = response;
  68.  
  69. this.error = (
  70. status === 400 || status === 401 || status === 403 ?
  71. 'Неправильный логин или пароль' :
  72. 'Произошла ошибка. Пожалуйста, попробуйте позже.'
  73. );
  74.  
  75. this.$log.warn('Login error:', data);
  76. })
  77. .finally(() => {
  78. this.isLoading = false;
  79. });
  80. }
  81. }
  82. }
  83.  
  84. export default {
  85. template: require('./auth-login.html'),
  86. controller: LoginController,
  87. controllerAs: 'vm'
  88. };
Add Comment
Please, Sign In to add comment