jwt implemented

This commit is contained in:
Eudes Inácio
2023-11-29 12:17:52 +01:00
parent 7bd1370c0b
commit f4a998584d
41 changed files with 1204 additions and 693 deletions
+56 -6
View File
@@ -3,7 +3,7 @@ import { StorageService } from './storage.service';
import { HttpClient, HttpHeaders, HttpEventType } from '@angular/common/http';
import { LoginUserRespose, UserForm, UserSession } from '../models/user.model';
import { environment } from 'src/environments/environment';
import { BehaviorSubject } from 'rxjs';
import { BehaviorSubject, of } from 'rxjs';
import { AlertController } from '@ionic/angular';
import { SessionStore } from '../store/session.service';
import { AESEncrypt } from '../services/aesencrypt.service';
@@ -21,6 +21,7 @@ import { ChatSystemService } from 'src/app/services/chat/chat-system.service';
import { HttpErrorHandle } from 'src/app/services/http-error-handle.service';
import { captureException } from '@sentry/angular';
import { CPSession } from '../store/documentManagement';
import { catchError, tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
@@ -80,17 +81,24 @@ export class AuthService {
}
async login(user: UserForm, { saveSession = true }): Promise<LoginUserRespose> {
user.BasicAuthKey = 'Basic ' + btoa(user.username + ':' + this.aesencrypt.encrypt(user.password, user.username));
user.BasicAuthKey = btoa(user.username + ':' + this.aesencrypt.encrypt(user.password, user.username));
this.headers = this.headers.set('Authorization', user.BasicAuthKey);
this.opts = {
headers: this.headers,
/* headers: this.headers, */
"Content-Type": "application/json",
"Accept": "application/json",
}
let body = {
"Auth": user.BasicAuthKey,
"ChannelId": 1
}
let response: any;
try {
response = await this.http.post<LoginUserRespose>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
response = await this.http.post<LoginUserRespose>(environment.apiURL + "UserAuthentication/Login", body, this.opts).toPromise();
if (saveSession) {
@@ -117,7 +125,7 @@ export class AuthService {
let response: any;
try {
response = await this.http.post<LoginUserRespose>(environment.apiURL + "UserAuthentication/LoginJwt", '', this.opts).toPromise();
response = await this.http.post<LoginUserRespose>(environment.apiURL + "UserAuthentication/Login", '', this.opts).toPromise();
console.log('JWT', response)
if (saveSession) {
@@ -169,7 +177,7 @@ export class AuthService {
if (SessionStore.user.ChatData?.data) {
this.RochetChatConnectorService.connect();
this.RochetChatConnectorService.login().then((message: any) => {
console.log('Chat login',message )
console.log('Chat login', message)
SessionStore.user.RochetChatUserId = message.result.id
SessionStore.save()
@@ -293,4 +301,46 @@ export class AuthService {
await alert.present();
}
async logoutUser() {
this.headers = this.headers.set('Authorization', 'Bearer ' + SessionStore.user.Authorization);
this.opts = {
headers: this.headers,
}
let response: any;
try {
response = await this.http.delete<LoginUserRespose>(environment.apiURL + "userauthentication/Logout", this.opts).toPromise();
SessionStore.user.Authorization = "";
SessionStore.user.RefreshToken = "";
} catch (error) {
this.errorHandler.handleError(error);
this.httpErroHandle.loginHttpStatusHandle(error)
captureException(error);
} finally {
return response
}
}
refreshToken() {
return this.http
.put<any>(environment.apiURL + "UserAuthentication/RefreshToken", {
refreshToken: SessionStore.user.RefreshToken,
},)
.pipe(
tap((tokens) => {
console.log(tokens)
SessionStore.user.Authorization = tokens.Authorization;
SessionStore.user.RefreshToken = tokens.refreshToken;
}),
catchError((error) => {
this.logoutUser();
return of(false);
})
);
}
}