53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import { Injectable, Inject } from '@angular/core';
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
import { Observable } from 'rxjs';
|
|
import { map } from 'rxjs/operators';
|
|
import { JwtSigninResponse } from '../model/jwt-signin-response.model';
|
|
|
|
import { CookieService } from 'ngx-cookie-service';
|
|
import { API_BASE_URL } from 'src/modules/common/type/injection-token.type';
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class AuthService {
|
|
public constructor(
|
|
private httpClient: HttpClient,
|
|
private cookieService: CookieService,
|
|
@Inject(API_BASE_URL) private apiBaseUrl: string
|
|
) { }
|
|
|
|
public authenticate(
|
|
username: string,
|
|
password: string
|
|
): Observable<JwtSigninResponse> {
|
|
const model = {
|
|
username,
|
|
password
|
|
};
|
|
|
|
return this.httpClient
|
|
.post<JwtSigninResponse>(`${this.apiBaseUrl}/auth/signin`, {
|
|
username,
|
|
password
|
|
})
|
|
.pipe(
|
|
map(res => {
|
|
sessionStorage.setItem('username', username);
|
|
let tokenStr = res.tokenType + ' ' + res.accessToken;
|
|
sessionStorage.setItem('token', tokenStr);
|
|
return res;
|
|
})
|
|
);
|
|
// return this.httpClient.post<User>(`${this.apiBaseUrl}/auth/signin`, model);
|
|
}
|
|
|
|
isLoggedIn() {
|
|
const user = sessionStorage.getItem('username');
|
|
return !(null === user);
|
|
}
|
|
|
|
logOut() {
|
|
sessionStorage.removeItem('username');
|
|
}
|
|
}
|