Commit 9035e9df authored by Simone Vuotto's avatar Simone Vuotto

Update login and authentication

parent af50384f
......@@ -5,7 +5,7 @@
<a class="navbar-brand" href="#">ReqV</a>
<ul class="navbar-nav navbar-right" *ngIf="authenticationService.isAuthenticated()">
<li class="nav-item">
<a class="nav-link" onclick="logout()" href="/" >Logout</a>
<a class="nav-link" (click)="logout()" routerLink="/" >Logout</a>
</li>
</ul>
......
......@@ -13,7 +13,7 @@ export class AuthenticationService {
login(username: string, password: string) {
return new Observable(subscriber =>
this.http.post('/api/login', {username: username, password: password}, {
this.http.post('api/login', {username: username, password: password}, {
headers: new HttpHeaders().set('Content-Type', 'application/json'),
observe: 'response',
})
......
......@@ -5,14 +5,12 @@ import { Observable } from 'rxjs/Observable';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const url = 'http://localhost:8080/';
// add authorization header with jwt token if available
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
const url = 'http://localhost:8080';
let authorization = '';
if (currentUser && currentUser.token) {
authorization = `Bearer ${currentUser.token}`;
let authorization = localStorage.getItem('currentUser');
if (authorization == null) {
authorization = '';
}
request = request.clone({
url: url + request.url,
setHeaders: {
......
......@@ -12,9 +12,10 @@
<div *ngIf="f.submitted && !password.valid" class="help-block">Password is required</div>
</div>
<div class="form-group">
<button [disabled]="loading" class="btn btn-primary">Login</button>
<img *ngIf="loading" src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==" />
<a [routerLink]="['/register']" class="btn btn-link">Register</a>
<button [disabled]="loading" class="btn btn-primary">
<i *ngIf="loading" class='fa fa-spinner fa-spin'></i>Login
</button>
<a [routerLink]="['/register']" disabled="true" class="btn btn-link">Register</a>
</div>
</form>
</div>
......@@ -23,11 +23,13 @@ export class LoginComponent implements OnInit {
private alertService: AlertService) { }
ngOnInit() {
// reset login status
this.authenticationService.logout();
// get return url from route parameters or default to '/'
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/projects';
if (this.authenticationService.isAuthenticated()) {
this.router.navigate([this.returnUrl]);
}
}
login() {
......@@ -37,7 +39,7 @@ export class LoginComponent implements OnInit {
data => {
this.alertService.success('Login succesful!');
this.loading = false;
this.router.navigate(['/projects']);
this.router.navigate([this.returnUrl]);
},
error => {
console.log(error);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment