import { Injectable } from '@angular/core' ; import { Router } from '@angular/router' ; import { TokenAuthServiceProxy , AuthenticateModel , AuthenticateResultModel , ExternalLoginProviderInfoModel , ExternalAuthenticateModel , ExternalAuthenticateResultModel } from '@shared/service-proxies/service-proxies' ; import { UrlHelper } from '@shared/helpers/UrlHelper' ; import { AppConsts } from '@shared/AppConsts' ; import { MessageService } from '@abp/message/message.service' ; import { LogService } from '@abp/log/log.service' ; import { TokenService } from '@abp/auth/token.service' ; import { UtilsService } from '@abp/utils/utils.service' ; import { Abp } from '@abp' ; import * as _ from 'lodash' ; @ Injectable ( ) export class LoginService { static readonly twoFactorRememberClientTokenName = 'TwoFactorRememberClientToken' ; authenticateModel : AuthenticateModel ; authenticateResult : AuthenticateResultModel ; rememberMe : boolean ; constructor ( private _tokenAuthService , private _router , private _utilsService , private _messageService , private _tokenService , private _logService ) { this . clear ( ) ; } authenticate ( finallyCallback ? ) { finallyCallback = finallyCallback || ( ( ) => { } ) ; this . _tokenAuthService . authenticate ( this . authenticateModel ) . finally ( finallyCallback ) . subscribe ( ( result ) => { this . processAuthenticateResult ( result ) ; } ) ; } private processAuthenticateResult ( authenticateResult ) { this . authenticateResult = authenticateResult ; if ( authenticateResult . accessToken ) { this . login ( authenticateResult . accessToken , authenticateResult . encryptedAccessToken , authenticateResult . expireInSeconds , this . rememberMe ) ; } else { this . _logService . warn ( 'Unexpected authenticateResult!' ) ; this . _router . navigate ( [ 'account/login' ] ) ; } } private login ( accessToken , encryptedAccessToken , expireInSeconds , rememberMe ? ) { var tokenExpireDate = rememberMe ? ( new Date ( new Date ( ) . getTime ( ) + 1000 * expireInSeconds ) ) : undefined ; this . _tokenService . setToken ( accessToken , tokenExpireDate ) ; this . _utilsService . setCookieValue ( AppConsts . authorization . encrptedAuthTokenName , encryptedAccessToken , tokenExpireDate , Abp . appPath ) ; var initialUrl = UrlHelper . initialUrl ; if ( initialUrl . indexOf ( '/login' ) > 0 ) { initialUrl = AppConsts . appBaseUrl ; } location . href = initialUrl ; } private clear ( ) { this . authenticateModel = new AuthenticateModel ( ) ; this . authenticateModel . rememberClient = false ; this . authenticateResult = null ; this . rememberMe = false ; } }