Skip to content

Instantly share code, notes, and snippets.

@avatsaev
Last active July 19, 2022 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avatsaev/0a4027acba81651f7c4e2d1043b84285 to your computer and use it in GitHub Desktop.
Save avatsaev/0a4027acba81651f7c4e2d1043b84285 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import {Angular2TokenService} from "angular2-token";
import {Subject, Observable} from "rxjs";
import {Response} from "@angular/http";
@Injectable()
export class AuthService {
userSignedIn$:Subject<boolean> = new Subject();
constructor(public authService:Angular2TokenService) {
this.authService.validateToken().subscribe(
res => res.status == 200 ? this.userSignedIn$.next(res.json().success) : this.userSignedIn$.next(false)
)
}
logOutUser():Observable<Response>{
return this.authService.signOut().map(
res => {
this.userSignedIn$.next(false);
return res;
}
);
}
registerUser(signUpData: {email:string, password:string, passwordConfirmation:string}):Observable<Response>{
return this.authService.registerAccount(signUpData).map(
res => {
this.userSignedIn$.next(true);
return res
}
);
}
logInUser(signInData: {email:string, password:string}):Observable<Response>{
return this.authService.signIn(signInData).map(
res => {
this.userSignedIn$.next(true);
return res
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment