<commit_before>import { Injectable } from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';
import { Router, NavigationStart, CanActivate } from '@angular/router';
import { UrlManagerService } from './url-manager.service';
import { LoggerService } from './logger.service';
import { ConfigService } from './config.service';
import Auth0Lock from 'auth0-lock';


export abstract class AuthService {

    abstract login(): void;

    abstract authenticated(): boolean;

    abstract logout(): void;

    abstract canActivate(): boolean;
}


@Injectable()
export class Auth0AuthService extends AuthService implements CanActivate {

    private readonly lock: Auth0LockStatic;

    private pendingAuthenticate : boolean = false;

    constructor(
        private readonly router: Router,
        private readonly urlManager: UrlManagerService,
        private readonly logger: LoggerService,
        private readonly configService: ConfigService
    ) {
        super();

        const clientId = configService.config.authClientId;
        const domain = configService.config.authDomain;

        if (clientId && domain) {

            const options = {
                auth: {
                    params: { scope: 'openid email' },
                }
            };

            // Configure Auth0
            // this is client id which is public 
            this.lock = new Auth0Lock(clientId, domain, options);

            // Add callback for lock `authenticated` event
            this.lock.on("authenticated", authResult => localStorage.setItem('id_token', authResult.idToken));

            this
                .router
                .events
                .filter(event => event instanceof NavigationStart)
                .filter((event: NavigationStart) => (/access_token|id_token|error/).test(event.url))
                .subscribe(() => {
                    
                    this.lock.resumeAuth(window.location.hash, (error: any, authResult: any) => {
                        if (error) {
                            logger.error(error);
                        }
                        else if (authResult && authResult.idToken) {
                            // some sort of race here with token response navigationg us to a page,
                            // we're making auth OK with token but app.component doesn't yet have router-outlet 
                            // so we see errors. Set the pending Authenticate flag which will make it look like 
                            // we're not authenticated and then clear and route home on next event loop.

                            this.pendingAuthenticate = true;
                            localStorage.setItem('id_token', authResult.idToken);
                            setTimeout(() => {
                                this.pendingAuthenticate = false;
                                this.urlManager.setHomeSinglePane()});
                        }
                    });
                });
        }
    }<commit_msg>tidy authenticate to avoid CORS errors<commit_after>import { Injectable } from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';
import { Router, NavigationStart, CanActivate } from '@angular/router';
import { UrlManagerService } from './url-manager.service';
import { LoggerService } from './logger.service';
import { ConfigService } from './config.service';
import Auth0Lock from 'auth0-lock';


export abstract class AuthService {

    abstract login(): void;

    abstract authenticated(): boolean;

    abstract logout(): void;

    abstract canActivate(): boolean;
}


@Injectable()
export class Auth0AuthService extends AuthService implements CanActivate {

    private readonly lock: Auth0LockStatic;

    private pendingAuthenticate : boolean = false;

    constructor(
        private readonly router: Router,
        private readonly urlManager: UrlManagerService,
        private readonly logger: LoggerService,
        private readonly configService: ConfigService
    ) {
        super();

        const clientId = configService.config.authClientId;
        const domain = configService.config.authDomain;
        const authenticate = configService.config.authenticate;

        if (authenticate && clientId && domain) {

            const options = {
                auth: {
                    params: { scope: 'openid email' },
                }
            };

            // Configure Auth0
            // this is client id which is public 
            this.lock = new Auth0Lock(clientId, domain, options);

            // Add callback for lock `authenticated` event
            this.lock.on("authenticated", authResult => localStorage.setItem('id_token', authResult.idToken));

            this
                .router
                .events
                .filter(event => event instanceof NavigationStart)
                .filter((event: NavigationStart) => (/access_token|id_token|error/).test(event.url))
                .subscribe(() => {
                    
                    this.lock.resumeAuth(window.location.hash, (error: any, authResult: any) => {
                        if (error) {
                            logger.error(error);
                        }
                        else if (authResult && authResult.idToken) {
                            // some sort of race here with token response navigating us to a page,
                            // we're making auth OK with token but app.component doesn't yet have router-outlet 
                            // so we see errors. Set the pending Authenticate flag which will make it look like 
                            // we're not authenticated and then clear and route home on next event loop.

                            this.pendingAuthenticate = true;
                            localStorage.setItem('id_token', authResult.idToken);
                            setTimeout(() => {
                                this.pendingAuthenticate = false;
                                this.urlManager.setHomeSinglePane()});
                        }
                    });
                });
        }
    }
{'fraction_non_alphanumeric': 0.08723958333333333, 'fraction_numerical': 0.0022786458333333335, 'mean_word_length': 1.6671006944444444, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '123254', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1377, 'n_words': 417}