import { Component, OnInit } from '@angular/core';
import { AuthenticationService } from '../../authentication.service';
import { Router, ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-login-usos',
  templateUrl: './login-usos.component.html',
  styleUrls: ['./login-usos.component.css']
})
export class LoginUsosComponent implements OnInit {

  constructor(private authenticationService: AuthenticationService,
              private router: Router) {}


  ngOnInit() {
    const urlArray: string[] = this.router.url.toString().replace('?', '&').split('&');
    localStorage.setItem('oauth_verifier', urlArray[2].replace('oauth_verifier=', ''));

    this.authenticationService.getAccessByUSOS(localStorage.getItem('outh_token'),
      localStorage.getItem('outh_token_secret'), localStorage.getItem('oauth_verifier')).subscribe(
        message => {
          this.authenticationService.usosLogin(message.username, message.key);
          this.router.navigate(['/my-groups']);
      }
    );
  }

}
