Skip to content
Permalink
Browse files

Fixing resetting of token, changing endpoint, simplifying isLoggedIn …

…function
  • Loading branch information
Evan Langlais
Evan Langlais committed Apr 30, 2019
1 parent faeb48b commit c23ef5d5857496502d0fd393e4d490d4c99e27dc
@@ -54,7 +54,7 @@ export class AuthenticationComponent implements OnInit {
localStorage.removeItem('gen_name');
localStorage.removeItem('data_type');
localStorage.removeItem('fault_type');
localStorage.removeItem('auth_token');
// localStorage.removeItem('auth_token');
}

}
@@ -23,16 +23,8 @@ export class HeaderComponent implements OnInit {
resetGenAndVisualize() {
this.resetAndRedirect('/visualize-all');
}
isLoggedIn()
{
if(localStorage.getItem('auth_token'))
{
return true;
}
else
{
return false;
}
isLoggedIn(): boolean {
return (localStorage.getItem('auth_token') !== null);
}

resetAndRedirect(url: string) {
@@ -3,13 +3,14 @@ import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import {Router} from '@angular/router';

@Injectable({
providedIn: 'root'
})
export class LoginService {

constructor(private http: HttpClient) { }
constructor(private http: HttpClient, private router: Router) { }

authorizeUser(loginInfo): Observable<Object> {
return this.http.post('http://sd5-backend.engr.uconn.edu/auth', loginInfo);
@@ -33,19 +34,28 @@ export class LoginService {
getToken(): Promise<string> {
return new Promise<string>((resolve, reject) => {
const token = localStorage.getItem('auth_token');
if (!token) { reject('No Token'); }
this.http.get('http://sd5-backend.engr.uconn.edu/auth/verify', {
if (!token) { this.resetToken(); reject('No Token'); }
this.http.get('http://sd5-backend.engr.uconn.edu/verify', {
headers: {
Authorization: 'Bearer ' + token
},
observe: 'response'
}).subscribe((response) => {
if (response.status === 200) {
if (response.ok) {
resolve(token);
} else {
reject('Invalid Token');
this.resetToken();
reject('Bad Token!');
}
}, () => {
this.resetToken();
reject('Bad Token!');
});
});
}

resetToken() {
localStorage.removeItem('auth_token');
this.router.navigateByUrl('#');
}
}

0 comments on commit c23ef5d

Please sign in to comment.
You can’t perform that action at this time.