94 lines
3.7 KiB
TypeScript
94 lines
3.7 KiB
TypeScript
import { Component, OnInit, Input } from "@angular/core";
|
|
import { BaseComponent } from "../../../../core/common/base/base.component";
|
|
import { ActivatedRoute, Router, Params } from "@angular/router";
|
|
import { HttpClient } from "@angular/common/http";
|
|
import { LoginService } from "../utilities/login.service";
|
|
import { AuthService } from "../../../../core/services/auth/auth.service";
|
|
import { takeUntil } from "rxjs/operators";
|
|
import { environment } from "../../../../../environments/environment";
|
|
import { AuthProvider } from "../../../../core/common/enum/auth-provider";
|
|
import { ConfigurableProvider } from "../../../../core/model/configurable-provider/configurableProvider";
|
|
import { ConfigurableProvidersService } from "../utilities/configurableProviders.service";
|
|
|
|
@Component({
|
|
selector: 'app-configurable-login',
|
|
templateUrl: './configurable-login.component.html',
|
|
})
|
|
export class ConfigurableLoginComponent extends BaseComponent implements OnInit {
|
|
private returnUrl: string;
|
|
|
|
// private configurableLoginId: string;
|
|
// private clientId: string;
|
|
// private oauthUrl: string;
|
|
// private redirectUri: string;
|
|
// private state: string;
|
|
// @Input() scope: string;
|
|
|
|
private provider: ConfigurableProvider;
|
|
private providerId: string;
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private loginService: LoginService,
|
|
private authService: AuthService,
|
|
private router: Router,
|
|
private httpClient: HttpClient,
|
|
private providers: ConfigurableProvidersService
|
|
) {
|
|
super();
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
const params = this.route.snapshot.params;
|
|
this.providerId = params['id'];
|
|
if (this.providers.providers === undefined) {
|
|
this.authService.getConfigurableProviders()
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe((data) => {
|
|
this.providers.providers = data;
|
|
this.provider = this.providers.providers.find(item => item.configurableLoginId == this.providerId)
|
|
this.route.queryParams
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe((params: Params) => {
|
|
const returnUrlFromParams = params['returnUrl'];
|
|
if (returnUrlFromParams) { this.returnUrl = returnUrlFromParams; }
|
|
if (!params['code']) { this.configurableAuthorize(); } else { this.configurableLoginUser(params['code'], params['state']) }
|
|
})
|
|
});
|
|
} else {
|
|
this.provider = this.providers.providers.find(item => item.configurableLoginId == this.providerId)
|
|
this.route.queryParams
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe((params: Params) => {
|
|
const returnUrlFromParams = params['returnUrl'];
|
|
if (returnUrlFromParams) { this.returnUrl = returnUrlFromParams; }
|
|
if (!params['code']) { this.configurableAuthorize(); } else { this.configurableLoginUser(params['code'], params['state']) }
|
|
})
|
|
}
|
|
}
|
|
|
|
public configurableAuthorize() {
|
|
window.location.href = this.provider.oauthUrl
|
|
+ '?response_type=code&client_id=' + this.provider.clientId
|
|
+ '&redirect_uri=' + this.provider.redirect_uri + this.providerId
|
|
+ '&state=' + this.provider.state
|
|
+ '&scope=' + this.provider.scope;
|
|
}
|
|
|
|
public configurableLoginUser(code: string, state: string) {
|
|
if (state !== this.provider.state) {
|
|
this.router.navigate(['/login'])
|
|
}
|
|
this.httpClient.post(environment.Server + 'auth/configurableProviderRequestToken', { code: code, provider: AuthProvider.Configurable, configurableLoginId: this.providerId })
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe((data: any) => {
|
|
this.authService.login({ ticket: data.payload.accessToken, provider: AuthProvider.Configurable, data: { configurableLoginId: this.provider.configurableLoginId } })
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(
|
|
res => this.loginService.onLogInSuccess(res, this.returnUrl),
|
|
error => this.loginService.onLogInError(error)
|
|
)
|
|
})
|
|
}
|
|
}
|