mirror of
https://github.com/akveo/ngx-admin.git
synced 2026-01-12 20:48:51 +01:00
add login service
This commit is contained in:
parent
cdf458488a
commit
d5bf1ab388
22 changed files with 1632 additions and 159 deletions
9
src/app/@core/data/user-response.ts
Normal file
9
src/app/@core/data/user-response.ts
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
export interface UserResponse {
|
||||
username: string,
|
||||
value: UserRole[],
|
||||
}
|
||||
|
||||
export interface UserRole {
|
||||
resourcealias: string,
|
||||
relationshiptypealias: string,
|
||||
}
|
||||
4
src/app/@core/data/user.ts
Normal file
4
src/app/@core/data/user.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export interface User {
|
||||
username: string,
|
||||
password: string,
|
||||
}
|
||||
50
src/app/auth/login.service.ts
Normal file
50
src/app/auth/login.service.ts
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
||||
import { Observable, throwError } from 'rxjs';
|
||||
import { map, catchError, tap } from 'rxjs/operators';
|
||||
import { UserResponse } from '../@core/data/user-response';
|
||||
import { User } from '../@core/data/user';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class LoginService {
|
||||
constructor(private http: HttpClient) {
|
||||
}
|
||||
|
||||
login(user: User): Observable<any> {
|
||||
const url = 'http://10.170.3.190:8011/api/user/getAuthorization ';
|
||||
return this.http.post(url,JSON.stringify(user)).pipe(
|
||||
map(this.extractData),
|
||||
catchError(this.handleError),
|
||||
tap(res => this.setSession(res)),
|
||||
);
|
||||
}
|
||||
|
||||
public setSession(authResult) {
|
||||
localStorage.setItem('roles', authResult.value);
|
||||
localStorage.setItem('username', authResult.username);
|
||||
}
|
||||
|
||||
|
||||
private extractData(body: any): UserResponse {
|
||||
return Object.assign(body.promotionList);
|
||||
}
|
||||
|
||||
private handleError(error: HttpErrorResponse | any) {
|
||||
let errMsg: string;
|
||||
let errObj: any;
|
||||
|
||||
if (error instanceof HttpErrorResponse) {
|
||||
const err = error.message || JSON.stringify(error);
|
||||
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
|
||||
errObj = error.error.message;
|
||||
} else {
|
||||
errMsg = error.message ? error.message : error.toString();
|
||||
const body = error.message || '';
|
||||
errObj = body;
|
||||
}
|
||||
|
||||
return throwError(errObj);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue