Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {
- isAbsoluteURL,
- isTrustedURL,
- appendParamsToURL,
- redirectToURL
- } from '../url-fns';
- class LoginController {
- /*@ngInject*/
- constructor($rootScope, $log, $state, commonAuthUser) {
- this.$rootScope = $rootScope;
- this.$log = $log;
- this.$state = $state;
- this.commonAuthUser = commonAuthUser;
- this.user = {
- login: '',
- password: ''
- };
- this.isPasswordVisible = false;
- }
- redirectToAccount() {
- const { accountAppUrl } = this.commonAuthUser.config;
- const token = this.commonAuthUser.getToken();
- redirectToURL(`${accountAppUrl}/#/auth/check?token=${token}`);
- }
- redirectToTarget(){
- const { callback: callbackURL } = this.$state.params;
- if (callbackURL) {
- if (!isAbsoluteURL(callbackURL)) {
- redirectToURL(callbackURL);
- return;
- }
- if (isTrustedURL(callbackURL)) {
- redirectToURL(appendParamsToURL(callbackURL, {token: this.commonAuthUser.getToken()}));
- return;
- }
- }
- this.$state.go(this.commonAuthUser.getTargetState(), {}, { location: 'replace' });
- }
- submit(form) {
- if (form.$valid && !this.isLoading) {
- this.isLoading = true;
- this.error = null;
- this.commonAuthUser
- .login(this.user.login, this.user.password)
- .then(data => {
- this.$log.log('Logged in:', data);
- if (data.status === 'New') {
- // NOTE: redirect to account app, to allow fill in user account data
- this.redirectToAccount();
- }
- this.redirectToTarget();
- })
- .catch(response => {
- const { status, data } = response;
- this.error = (
- status === 400 || status === 401 || status === 403 ?
- 'Неправильный логин или пароль' :
- 'Произошла ошибка. Пожалуйста, попробуйте позже.'
- );
- this.$log.warn('Login error:', data);
- })
- .finally(() => {
- this.isLoading = false;
- });
- }
- }
- }
- export default {
- template: require('./auth-login.html'),
- controller: LoginController,
- controllerAs: 'vm'
- };
Add Comment
Please, Sign In to add comment