git init v2
This commit is contained in:
42
src/app/services/auth.service.ts
Normal file
42
src/app/services/auth.service.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { JwtHelperService } from '@auth0/angular-jwt';
|
||||
import { Observable } from 'rxjs';
|
||||
import {jwtDecode} from 'jwt-decode';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthService {
|
||||
private baseApi = 'http://localhost:8080/api/auth/login';
|
||||
// baseApi = "https://api.melvanveen.nl/api/auth/login";
|
||||
jwtHelper = new JwtHelperService();
|
||||
|
||||
constructor(private http: HttpClient) {}
|
||||
|
||||
login(username: string, password: string): Observable<any> {
|
||||
return this.http.post(this.baseApi, { username, password });
|
||||
}
|
||||
|
||||
isAuthenticated(): boolean {
|
||||
const token = localStorage.getItem('token');
|
||||
return token !== null && !this.jwtHelper.isTokenExpired(token);
|
||||
}
|
||||
|
||||
logout(): void {
|
||||
localStorage.removeItem('token');
|
||||
}
|
||||
|
||||
getToken(): string | null {
|
||||
return localStorage.getItem('token');
|
||||
}
|
||||
|
||||
getUserInfo(): any {
|
||||
const token = this.getToken();
|
||||
if (token) {
|
||||
return jwtDecode(token);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user