PUNKT-15 Logowanie przez USOS
This commit is contained in:
parent
0ee3c4ac21
commit
3f2c3d6a5e
@ -45,10 +45,12 @@ import { WaitingResourcesComponent } from './groups/waiting-resources/waiting-re
|
|||||||
import { BagdesComponent } from './user/bagdes/bagdes.component';
|
import { BagdesComponent } from './user/bagdes/bagdes.component';
|
||||||
import { RankingComponent } from './groups/ranking/ranking.component';
|
import { RankingComponent } from './groups/ranking/ranking.component';
|
||||||
import { HistoryOfActivityInGroupComponent } from './groups/history-of-activity-in-group/history-of-activity-in-group.component';
|
import { HistoryOfActivityInGroupComponent } from './groups/history-of-activity-in-group/history-of-activity-in-group.component';
|
||||||
|
import { LoginUsosComponent } from './login/login-usos/login-usos.component';
|
||||||
|
|
||||||
|
|
||||||
const routes: Routes = [
|
const routes: Routes = [
|
||||||
{ path: 'login', component: LoginComponent },
|
{ path: 'login', component: LoginComponent },
|
||||||
|
{ path: 'login/usos', component: LoginUsosComponent },
|
||||||
{ path: '', component: HomePageComponent },
|
{ path: '', component: HomePageComponent },
|
||||||
{ path: 'home', component: HomePageComponent },
|
{ path: 'home', component: HomePageComponent },
|
||||||
{ path: 'flashcards', component: FlashcardsComponent },
|
{ path: 'flashcards', component: FlashcardsComponent },
|
||||||
|
@ -27,6 +27,7 @@ import { SharedModule } from './shared/shared.module';
|
|||||||
import { RoutingStateService } from './routing-state.service';
|
import { RoutingStateService } from './routing-state.service';
|
||||||
|
|
||||||
import { AutofocusDirective } from './autofocus.directive';
|
import { AutofocusDirective } from './autofocus.directive';
|
||||||
|
import { LoginUsosComponent } from './login/login-usos/login-usos.component';
|
||||||
|
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
@ -37,7 +38,8 @@ import { AutofocusDirective } from './autofocus.directive';
|
|||||||
FooterComponent,
|
FooterComponent,
|
||||||
HomePageComponent,
|
HomePageComponent,
|
||||||
WorkInProgressComponent,
|
WorkInProgressComponent,
|
||||||
AutofocusDirective
|
AutofocusDirective,
|
||||||
|
LoginUsosComponent
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
BrowserModule,
|
BrowserModule,
|
||||||
|
@ -13,6 +13,7 @@ export class AuthenticationService {
|
|||||||
private headers = new HttpHeaders({ 'Content-Type': 'application/json' });
|
private headers = new HttpHeaders({ 'Content-Type': 'application/json' });
|
||||||
private storageSub = new Subject<boolean>();
|
private storageSub = new Subject<boolean>();
|
||||||
public token: string;
|
public token: string;
|
||||||
|
|
||||||
constructor(private http: HttpClient, public snackBar: MatSnackBar) {
|
constructor(private http: HttpClient, public snackBar: MatSnackBar) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,6 +49,27 @@ export class AuthenticationService {
|
|||||||
}).catch((error: any) => Observable.throw(error.json().error || 'Server error'));
|
}).catch((error: any) => Observable.throw(error.json().error || 'Server error'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
usosLogin(username, token) {
|
||||||
|
if (token) {
|
||||||
|
// store username and jwt token w local storage aby nie wylogowało przy zmianie stron
|
||||||
|
localStorage.setItem('currentUser', JSON.stringify({ username: username, authorization: token }));
|
||||||
|
this.getLoggedInName.emit('logged');
|
||||||
|
this.storageSub.next(true);
|
||||||
|
this.snackBar.open('Zalogowano pomyślnie!', null,
|
||||||
|
{ duration: 3000, verticalPosition: 'top', panelClass: ['snackbar-success'] });
|
||||||
|
// return true jeśli ok
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
// return false jeśli nie
|
||||||
|
this.getLoggedInName.emit('notLogged');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getUSOSTokens(): Observable<string> {
|
||||||
|
return this.http.get<string>(encodeURI('usos/request_token?oauth_callback=http://localhost:4200/login/usos'));
|
||||||
|
}
|
||||||
|
|
||||||
getToken(): String {
|
getToken(): String {
|
||||||
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
|
||||||
if (currentUser.authorization == null) {
|
if (currentUser.authorization == null) {
|
||||||
@ -57,6 +79,16 @@ export class AuthenticationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getAccessByUSOS(outh_t: string, outh_t_secret: string, outh_ver: string): Observable<string> {
|
||||||
|
const headers = new HttpHeaders({
|
||||||
|
'oauth_token' : outh_t,
|
||||||
|
'oauth_token_secret': outh_t_secret,
|
||||||
|
'oauth_verifier' : outh_ver
|
||||||
|
});
|
||||||
|
|
||||||
|
return this.http.get<string>('usos/access_token' , {headers: headers});
|
||||||
|
}
|
||||||
|
|
||||||
isLoggedIn(): boolean {
|
isLoggedIn(): boolean {
|
||||||
const token: String = this.getToken();
|
const token: String = this.getToken();
|
||||||
return token && token.length > 0;
|
return token && token.length > 0;
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { LoginUsosComponent } from './login-usos.component';
|
||||||
|
|
||||||
|
describe('LoginUsosComponent', () => {
|
||||||
|
let component: LoginUsosComponent;
|
||||||
|
let fixture: ComponentFixture<LoginUsosComponent>;
|
||||||
|
|
||||||
|
beforeEach(async(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
declarations: [ LoginUsosComponent ]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = TestBed.createComponent(LoginUsosComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
32
FrontEnd/src/app/login/login-usos/login-usos.component.ts
Normal file
32
FrontEnd/src/app/login/login-usos/login-usos.component.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
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 {
|
||||||
|
|
||||||
|
username = '';
|
||||||
|
|
||||||
|
constructor(private authenticationService: AuthenticationService,
|
||||||
|
private activatedRoute: ActivatedRoute,
|
||||||
|
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(this.username, message);
|
||||||
|
this.router.navigate(['/home']);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
<div class="content">
|
<div class="content">
|
||||||
<div id="container-login">
|
<div id="container-login">
|
||||||
|
<button class="btn btn-study-cave" (click)="loginWithUsos()">Zaloguj przez USOS</button>
|
||||||
|
<br /><br /><br /><br />
|
||||||
<div>
|
<div>
|
||||||
<form (ngSubmit)="login()">
|
<form (ngSubmit)="login()">
|
||||||
Nazwa użytkownika:<br />
|
Nazwa użytkownika:<br />
|
||||||
|
@ -16,6 +16,8 @@ export class LoginComponent implements OnInit {
|
|||||||
model: any = {};
|
model: any = {};
|
||||||
loading = false;
|
loading = false;
|
||||||
error = '';
|
error = '';
|
||||||
|
private outh_token = '';
|
||||||
|
private outh_token_secret = '';
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private router: Router,
|
private router: Router,
|
||||||
@ -27,8 +29,8 @@ export class LoginComponent implements OnInit {
|
|||||||
|
|
||||||
login() {
|
login() {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.authenticationService.login(this.model.username, this.model.password)
|
this.authenticationService.login(this.model.username, this.model.password).subscribe(
|
||||||
.subscribe(result => {
|
result => {
|
||||||
if (result === true) {
|
if (result === true) {
|
||||||
// login successful
|
// login successful
|
||||||
this.isLogin = true;
|
this.isLogin = true;
|
||||||
@ -47,6 +49,22 @@ export class LoginComponent implements OnInit {
|
|||||||
this.error = error;
|
this.error = error;
|
||||||
this.snackBar.open('Niepoprawne hasło lub login.', null,
|
this.snackBar.open('Niepoprawne hasło lub login.', null,
|
||||||
{ duration: 3000, verticalPosition: 'top', panelClass: ['snackbar-error'] });
|
{ duration: 3000, verticalPosition: 'top', panelClass: ['snackbar-error'] });
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
loginWithUsos() {
|
||||||
|
let array = [] as string[];
|
||||||
|
this.authenticationService.getUSOSTokens().subscribe(el => {
|
||||||
|
array = el.split('?');
|
||||||
|
array = array[1].split('&');
|
||||||
|
this.outh_token = array[0];
|
||||||
|
this.outh_token = this.outh_token.replace('oauth_token=', '');
|
||||||
|
this.outh_token_secret = array[1];
|
||||||
|
this.outh_token_secret = this.outh_token_secret.replace('oauth_token_secret=', '');
|
||||||
|
localStorage.setItem('outh_token', array[0].replace('oauth_token=', ''));
|
||||||
|
localStorage.setItem('outh_token_secret', array[1].replace('oauth_token_secret=', ''));
|
||||||
|
window.location.href = el;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,6 @@
|
|||||||
<a (click)="navToProfile()"><i class="fas fa-user-circle"></i> Profil</a>
|
<a (click)="navToProfile()"><i class="fas fa-user-circle"></i> Profil</a>
|
||||||
<a (click)="logout()"><i class="fas fa-sign-out-alt"></i> Wyloguj</a>
|
<a (click)="logout()"><i class="fas fa-sign-out-alt"></i> Wyloguj</a>
|
||||||
</div>
|
</div>
|
||||||
<a *ngIf="isLogin">Witaj, {{currentUser?.username}}!</a>
|
<!--<a *ngIf="isLogin">Witaj, {{currentUser?.username}}!</a>-->
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
Loading…
Reference in New Issue
Block a user