import { HttpClient, HttpHeaders } from
'@angular/common/http';
import { Injectable,OnInit } from
'@angular/core';
import { BehaviorSubject, map, Observable } from
'rxjs';
import { environment } from
'src/environments/environment';
import { User_Model } from
'../models/usermodel';
import { CookieService } from
'ngx-cookie-service' //*for
using cookies
import { Router } from
'@angular/router';
const url_Api_Auth = environment.apiUrl + "/Authentication";
const httpOptions = { headers: new
HttpHeaders({ 'Content-Type': 'application/json' })};
@Injectable({
providedIn: 'root'
})
export class AuthenticationService implements
OnInit{
//--------< AuthenticationService
>--------
//*opens webapi and gets data
private userSubject: BehaviorSubject<User_Model |
null>;
public user: Observable<User_Model | null>;
public token:string="";
constructor(private http: HttpClient, private
cookieService: CookieService, private router:Router) {
this.userSubject = new
BehaviorSubject(JSON.parse(localStorage.getItem('u')!));
this.user = this.userSubject.asObservable();
} //*inject web-api caller
ngOnInit() {
//this.email =
localStorage.getItem('e')!;
this.token = this.cookieService.get("t")!;
}
//try to register and send result
true/false
//test: public async
register(username:string, email:string,
password:string):Promise<boolean> {
public register(username:string, email:string,
password:string) {
//--------<
register() >--------
//*try to /register
with email password and get token as result.
return this.http.post<any>(url_Api_Auth + "/register",{Username:username,Email:email,Password:password})
.subscribe(
{
next: (response_username_token:any ) =>
{
//*store
token in local storage
var token=response_username_token.token;
var username=response_username_token.username;
this.cookieService.set('t',
token,365);
this.token=token;
localStorage.setItem('u',JSON.stringify(username))
this.userSubject.next(username);
console.log("register ok");
this.router.navigate(['/home']);
return true; //->total
result is true for the wrapping function
}
,error: (e) =>
{
console.log("/register.error:" + e.message);
alert(e.message);
return false; //->total
result is true for the wrapping function
}
,complete: () =>
console.log('register complete')
});
//--------</
register() >--------
}
//try
to register and send result true/false
public async login(email:string,
password:string):Promise<boolean> {
//*try to /register with email
password and get token as result.
//if token is ok, then true else false
const webrequest=await
this.http.post<any>(url_Api_Auth + "/login",{email,password});
webrequest.subscribe(
{
next: (token: string) => {
//*store token in
local storage
this.cookieService.set("t", token,365);
this.token=token;
console.log("login
ok");
this.router.navigate(['/home']);
return true;
}
,error: (e) => {
console.log("/login.error:"+ e.message);
alert(e.message);
return false;
}
,complete: () => console.log('register complete')
});
if (this.token!="") return true; else return false;
}
public logout(){
localStorage.clear();
this.cookieService.deleteAll();
this.token="";
this.user.pipe(map(u=>{
u!.username="x";}))
}
public getMe(): Observable<string> {
return this.http.get(
url_Api_Auth + "/getme", { responseType: 'text'
}
)
}
}
|