Created
April 8, 2019 08:30
-
-
Save elmarputz/28aa7347b6dcbe9f476d8dc18bc9588d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from '@angular/core'; | |
import {isNullOrUndefined} from "util"; | |
import {HttpClient} from "@angular/common/http"; | |
import * as decode from 'jwt-decode'; | |
import {retry} from 'rxjs/operators'; | |
//npm install --save-dev jwt-decode | |
interface User { | |
result: { | |
created_at: Date, | |
email: string, | |
id: number, | |
name: string, | |
updated_at: Date | |
} | |
} | |
@Injectable() | |
export class AuthService { | |
private api:string = 'http://bookstore19test.schoenboeck.kwmhgb.at/api/auth';//'http://localhost:8080/api/auth'; | |
constructor(private http: HttpClient) { | |
} | |
login(email: string, password: string ) { | |
return this.http.post(`${this.api}/login`, {'email': email, 'password': password}); | |
} | |
public setCurrentUserId(){ | |
this.http.get<User>(`${this.api}/user`).pipe(retry(3)).subscribe(res =>{ | |
localStorage.setItem('userId', res.result.id.toString()); | |
} | |
); | |
} | |
public getCurrentUserId(){ | |
return Number.parseInt(localStorage.getItem('userId')); | |
} | |
public setLocalStorage(token: string) { | |
console.log("Storing token"); | |
console.log(token); | |
const decodedToken = decode(token); | |
console.log(decodedToken); | |
console.log(decodedToken.user.id); | |
localStorage.setItem('token', token); | |
localStorage.setItem('userId', decodedToken.user.id); | |
} | |
logout() { | |
this.http.post(`${this.api}/logout`, {}); | |
localStorage.removeItem("token"); | |
localStorage.removeItem("userId"); | |
console.log("logged out"); | |
} | |
public isLoggedIn() { | |
return !isNullOrUndefined(localStorage.getItem("token")); | |
} | |
isLoggedOut() { | |
return !this.isLoggedIn(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment