src/app/auth/auth.service.ts
Properties |
|
Methods |
constructor(http: HttpClient, cookie: CookieService, md5: Md5Service)
|
||||||||||||
Defined in src/app/auth/auth.service.ts:19
|
||||||||||||
Parameters :
|
Public login | ||||||
login(user: User)
|
||||||
Defined in src/app/auth/auth.service.ts:27
|
||||||
Parameters :
Returns :
Observable<object>
|
Public logout |
logout()
|
Defined in src/app/auth/auth.service.ts:40
|
Returns :
void
|
Public isLoggedIn |
Default value : false
|
Defined in src/app/auth/auth.service.ts:15
|
Public redirectUrl |
Type : string
|
Defined in src/app/auth/auth.service.ts:17
|
Public user |
Type : User
|
Defined in src/app/auth/auth.service.ts:19
|
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Md5Service } from '../share/md5.service';
import { User } from './auth.user.entity';
@Injectable({
providedIn: 'root',
})
export class AuthService {
// 是否已登陆
public isLoggedIn = false;
// 登陆前的url
public redirectUrl: string;
// 用户信息
public user: User;
constructor(
private http: HttpClient,
private cookie: CookieService,
private md5: Md5Service,
) { }
public login(user: User): Observable<object> {
user.password = this.md5.fromString(user.password);
return this.http.post('https://int.mycht.cn/port/4201/user/login?_allow_anonymous=true', user).pipe(
tap((data: any) => {
if (data.msg === 'ok') {
this.isLoggedIn = true;
this.user = data.user;
this.cookie.set('user', JSON.stringify(data.user), new Date('2088-12-12'), undefined, undefined, undefined, 'Lax');
}
}),
);
}
public logout(): void {
this.isLoggedIn = false;
this.cookie.delete('user');
}
}