Merge branch 'usos-front' of s416178/Punktonerzy into master

This commit is contained in:
Marcin Szczepański 2019-11-29 20:23:44 +00:00 committed by Gogs
commit 0c93108991
11 changed files with 130 additions and 5 deletions

View File

@ -45,10 +45,12 @@ import { WaitingResourcesComponent } from './groups/waiting-resources/waiting-re
import { BagdesComponent } from './user/bagdes/bagdes.component';
import { RankingComponent } from './groups/ranking/ranking.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 = [
{ path: 'login', component: LoginComponent },
{ path: 'login/usos', component: LoginUsosComponent },
{ path: '', component: HomePageComponent },
{ path: 'home', component: HomePageComponent },
{ path: 'flashcards', component: FlashcardsComponent },

View File

@ -27,6 +27,7 @@ import { SharedModule } from './shared/shared.module';
import { RoutingStateService } from './routing-state.service';
import { AutofocusDirective } from './autofocus.directive';
import { LoginUsosComponent } from './login/login-usos/login-usos.component';
@NgModule({
@ -37,7 +38,8 @@ import { AutofocusDirective } from './autofocus.directive';
FooterComponent,
HomePageComponent,
WorkInProgressComponent,
AutofocusDirective
AutofocusDirective,
LoginUsosComponent
],
imports: [
BrowserModule,

View File

@ -6,6 +6,7 @@ import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Subject } from 'rxjs/Subject';
import { USOSUrl, LoginKey } from './login';
@Injectable()
export class AuthenticationService {
@ -13,6 +14,7 @@ export class AuthenticationService {
private headers = new HttpHeaders({ 'Content-Type': 'application/json' });
private storageSub = new Subject<boolean>();
public token: string;
constructor(private http: HttpClient, public snackBar: MatSnackBar) {
}
@ -48,6 +50,27 @@ export class AuthenticationService {
}).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<USOSUrl> {
return this.http.get<USOSUrl>(encodeURI('usos/request_token?oauth_callback=http://localhost:4200/login/usos'));
}
getToken(): String {
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
if (currentUser.authorization == null) {
@ -57,6 +80,16 @@ export class AuthenticationService {
}
}
getAccessByUSOS(outh_t: string, outh_t_secret: string, outh_ver: string): Observable<LoginKey> {
const headers = new HttpHeaders({
'oauth_token' : outh_t,
'oauth_token_secret': outh_t_secret,
'oauth_verifier' : outh_ver
});
return this.http.get<LoginKey>('usos/access_token' , {headers: headers});
}
isLoggedIn(): boolean {
const token: String = this.getToken();
return token && token.length > 0;

14
FrontEnd/src/app/login.ts Normal file
View File

@ -0,0 +1,14 @@
export class USOSUrl {
url: string;
constructor() {}
}
export class LoginKey {
key: string;
username: string;
constructor() {}
}

View File

@ -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();
});
});

View File

@ -0,0 +1,29 @@
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(['/home']);
}
);
}
}

View File

@ -1,5 +1,7 @@
<div class="content">
<div id="container-login">
<button class="btn btn-study-cave" (click)="loginWithUsos()">Zaloguj przez USOS</button>
<br /><br /><br /><br />
<div>
<form (ngSubmit)="login()">
Nazwa użytkownika:<br />

View File

@ -16,6 +16,8 @@ export class LoginComponent implements OnInit {
model: any = {};
loading = false;
error = '';
private outh_token = '';
private outh_token_secret = '';
constructor(
private router: Router,
@ -27,8 +29,8 @@ export class LoginComponent implements OnInit {
login() {
this.loading = true;
this.authenticationService.login(this.model.username, this.model.password)
.subscribe(result => {
this.authenticationService.login(this.model.username, this.model.password).subscribe(
result => {
if (result === true) {
// login successful
this.isLogin = true;
@ -47,6 +49,22 @@ export class LoginComponent implements OnInit {
this.error = error;
this.snackBar.open('Niepoprawne hasło lub login.', null,
{ duration: 3000, verticalPosition: 'top', panelClass: ['snackbar-error'] });
});
}
);
}
loginWithUsos() {
let array = [] as string[];
this.authenticationService.getUSOSTokens().subscribe(el => {
array = el.url.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.url;
});
}
}

View File

@ -17,6 +17,6 @@
<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>
</div>
<a *ngIf="isLogin">Witaj, {{currentUser?.username}}!</a>
<!--<a *ngIf="isLogin">Witaj, {{currentUser?.username}}!</a>-->
</div>
</div>