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

207 lines
6.7 KiB
TypeScript
Raw Normal View History

2019-01-18 18:03:45 +01:00
import { AfterViewInit, Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { AuthProvider } from '@app/core/common/enum/auth-provider';
import { ConfigurableProvider } from '@app/core/model/configurable-provider/configurableProvider';
import { AuthService } from '@app/core/services/auth/auth.service';
import { ConfigurableProvidersService } from '@app/ui/auth/login/utilities/configurableProviders.service';
import { LoginService } from '@app/ui/auth/login/utilities/login.service';
import { BaseComponent } from '@common/base/base.component';
import { environment } from 'environments/environment';
2019-01-18 18:03:45 +01:00
import { takeUntil } from 'rxjs/operators';
import { ConfigurationService } from '@app/core/services/configuration/configuration.service';
2019-01-18 18:03:45 +01:00
/// <reference types="gapi" />
/// <reference types="facebook-js-sdk" />
declare const gapi: any;
declare const FB: any;
2019-01-18 18:03:45 +01:00
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent extends BaseComponent implements OnInit, AfterViewInit {
public auth2: any;
private returnUrl: string;
2019-11-13 16:32:55 +01:00
//public cofigurableProviders: ConfigurableProvider[];
2019-01-18 18:03:45 +01:00
constructor(
private router: Router,
private route: ActivatedRoute,
private loginService: LoginService,
2019-11-13 16:32:55 +01:00
private authService: AuthService,
public configurableProviderService: ConfigurableProvidersService,
private configurationService: ConfigurationService
2019-01-18 18:03:45 +01:00
) { super(); }
ngOnInit(): void {
this.route.queryParams
.pipe(takeUntil(this._destroyed))
.subscribe((params: Params) => {
const returnUrl = params['returnUrl'];
if (returnUrl) { this.returnUrl = returnUrl; }
});
2019-11-13 16:32:55 +01:00
this.authService.getConfigurableProviders()
.pipe(takeUntil(this._destroyed))
.subscribe((data: any) => {
this.configurableProviderService.providers = data;
})
2019-01-18 18:03:45 +01:00
}
ngAfterViewInit() {
this.initProviders();
}
public linkedInLogin() {
this.router.navigate(['/login/linkedin']);
}
public twitterLogin() {
this.router.navigate(['/login/twitter']);
}
public b2AccessLogin() {
2019-07-12 16:22:57 +02:00
this.router.navigate(['/login/external/b2access']);
}
public orcidLogin() {
this.router.navigate(['/login/external/orcid']);
2019-01-18 18:03:45 +01:00
}
public openaireLogin() {
this.router.navigate(['/login/openaire']);
}
2019-11-13 16:32:55 +01:00
public configurableLogin(provider: ConfigurableProvider) {
this.router.navigate(['/login/configurable/' + provider.configurableLoginId])
}
public zenodoLogin() {
this.router.navigate(['/login/external/zenodo']);
}
2019-01-18 18:03:45 +01:00
public hasFacebookOauth(): boolean {
return this.hasProvider(AuthProvider.Facebook);
}
public hasLinkedInOauth(): boolean {
return this.hasProvider(AuthProvider.LinkedIn);
}
public hasTwitterOauth(): boolean {
return this.hasProvider(AuthProvider.Twitter);
}
public hasGoogleOauth(): boolean {
return this.hasProvider(AuthProvider.Google);
}
public hasB2AccessOauth(): boolean {
return this.hasProvider(AuthProvider.B2Access);
}
2019-07-12 16:22:57 +02:00
public hasOrcidOauth(): boolean {
return this.hasProvider(AuthProvider.ORCID);
}
public hasOpenAireOauth(): boolean {
return this.hasProvider(AuthProvider.OpenAire);
}
public hasZenodoOauth(): boolean {
return this.hasProvider(AuthProvider.Zenodo);
}
2019-01-18 18:03:45 +01:00
public initProviders() {
if (this.hasProvider(AuthProvider.Google)) { this.initializeGoogleOauth(); }
if (this.hasProvider(AuthProvider.Facebook)) { this.initializeFacebookOauth(); }
}
public hasProvider(provider: AuthProvider) {
for (let i = 0; i < this.configurationService.loginProviders.enabled.length; i++) {
if (provider === this.configurationService.loginProviders.enabled[i]) { return this.isProviderProperlyConfigured(provider); }
2019-01-18 18:03:45 +01:00
}
return false;
}
private isProviderProperlyConfigured(provider: AuthProvider) {
switch (provider) {
case AuthProvider.Facebook: return this.hasAllRequiredFieldsConfigured(this.configurationService.loginProviders.facebookConfiguration);
case AuthProvider.Google: return this.hasAllRequiredFieldsConfigured(this.configurationService.loginProviders.googleConfiguration);
case AuthProvider.LinkedIn: return this.hasAllRequiredFieldsConfigured(this.configurationService.loginProviders.linkedInConfiguration);
case AuthProvider.Twitter: return this.hasAllRequiredFieldsConfigured(this.configurationService.loginProviders.twitterConfiguration);
case AuthProvider.B2Access: return this.hasAllRequiredFieldsConfigured(this.configurationService.loginProviders.b2accessConfiguration);
case AuthProvider.ORCID: return this.hasAllRequiredFieldsConfigured(this.configurationService.loginProviders.orcidConfiguration);
case AuthProvider.OpenAire: return this.hasAllRequiredFieldsConfigured(this.configurationService.loginProviders.openAireConfiguration);
case AuthProvider.Zenodo: return this.hasAllRequiredFieldsConfigured(this.configurationService.loginProviders.zenodoConfiguration);
2019-01-18 18:03:45 +01:00
default: throw new Error('Unsupported Provider Type');
}
}
private hasAllRequiredFieldsConfigured(configuration: any) {
if (configuration != null && configuration.clientId != null) { return true; }
return false;
}
/*
* GOOGLE SIGN IN
*/
private initializeGoogleOauth(): void {
gapi.load('auth2', () => {
this.auth2 = gapi.auth2.init({
client_id: this.configurationService.loginProviders.googleConfiguration.clientId,
2019-01-18 18:03:45 +01:00
scope: 'profile email'
});
this.attachGoogleSignΙn(document.getElementById('googleSignInButton'));
});
}
public attachGoogleSignΙn(element) {
if (!element) { return; }
this.auth2.attachClickHandler(element, {},
(googleUser) => {
const id_token = googleUser.getAuthResponse().id_token;
if (id_token) {
this.authService.login({ ticket: id_token, provider: AuthProvider.Google })
.pipe(takeUntil(this._destroyed))
.subscribe(
res => this.loginService.onLogInSuccess(res, this.returnUrl),
error => this.loginService.onLogInError(error)
);
}
}, (error) => {
});
}
/*
* FACEBOOK SIGN IN
*/
private initializeFacebookOauth(): void {
FB.init({
appId: this.configurationService.loginProviders.facebookConfiguration.clientId,
2019-01-18 18:03:45 +01:00
cookie: false,
xfbml: true,
version: 'v2.8'
});
}
public facebookLogin() {
FB.login((response: any) => {
if (response.status === 'connected' || 'not_authorized') {
this.authService.login({ ticket: response.authResponse.accessToken, provider: AuthProvider.Facebook })
.pipe(takeUntil(this._destroyed))
.subscribe(
res => this.loginService.onLogInSuccess(res, this.returnUrl),
error => this.loginService.onLogInError(error)
);
}
2020-04-02 12:32:14 +02:00
}, { scope: 'email' });
2019-01-18 18:03:45 +01:00
}
public hasConfigurableProviders(): boolean {
return !(this.configurableProviderService.providers == undefined) && this.configurableProviderService.providers.length > 0
}
2019-01-18 18:03:45 +01:00
}