argos/dmp-frontend/src/app/ui/auth/login/orcid-login/orcid-login.component.ts

82 lines
3.0 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { BaseComponent } from '../../../../core/common/base/base.component';
import { LoginService } from '../utilities/login.service';
import { ActivatedRoute, Params } from '@angular/router';
import { takeUntil } from 'rxjs/operators';
import { environment } from '../../../../../environments/environment';
import { AuthService } from '../../../../core/services/auth/auth.service';
import { AuthProvider } from '../../../../core/common/enum/auth-provider';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { OrcidUser } from '../../../../core/model/orcid/orcidUser';
import { FormControl } from '@angular/forms';
@Component({
selector: 'app-orcid-login',
templateUrl: './orcid-login.component.html',
styleUrls: ['./orcid-login.component.scss']
})
export class OrcidLoginComponent extends BaseComponent implements OnInit {
private returnUrl: string;
private orcidUser: OrcidUser
private accessToken: string;
private emailFormControl = new FormControl('');
constructor(
private route: ActivatedRoute,
private authService: AuthService,
private loginService: LoginService,
private httpClient: HttpClient
) {
super();
this.orcidUser = new OrcidUser;
}
ngOnInit(): void {
this.route.queryParams
.pipe(takeUntil(this._destroyed))
.subscribe((params: Params) => {
const returnUrl = params['returnUrl'];
if (returnUrl) { this.returnUrl = returnUrl; }
if (!params['code']) { this.orcidAccessGetAuthCode(); } else { this.orcidLogin(params['code']); }
});
}
public orcidAccessGetAuthCode() {
window.location.href = environment.loginProviders.orcidConfiguration.oauthUrl
+ '?client_id='
+ environment.loginProviders.orcidConfiguration.clientId
+ '&response_type=code&scope=/authenticate&redirect_uri='
+ environment.loginProviders.orcidConfiguration.redirectUri;
}
public orcidLogin(code: string) {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/json');
headers = headers.set('Accept', 'application/json');
this.httpClient.post(environment.Server + 'auth/orcidRequestToken', { code: code }, { headers: headers })
.pipe(takeUntil(this._destroyed))
.subscribe((responseData: any) => {
this.orcidUser.orcidId = responseData.payload.orcidId
this.orcidUser.name = responseData.payload.name
this.accessToken = responseData.payload.accessToken;
this.authService.login({ ticket: this.accessToken, provider: AuthProvider.ORCID, data: this.orcidUser })
.pipe(takeUntil(this._destroyed))
.subscribe(
res => this.loginService.onLogInSuccess(res, this.returnUrl),
error => this.loginService.onLogInError(error)
);
});
}
public login() {
this.orcidUser.email = this.emailFormControl.value;
this.authService.login({ ticket: this.accessToken, provider: AuthProvider.ORCID, data: this.orcidUser })
.pipe(takeUntil(this._destroyed))
.subscribe(
res => this.loginService.onLogInSuccess(res, this.returnUrl),
error => this.loginService.onLogInError(error)
);
}
}