Skip to content

Instantly share code, notes, and snippets.

@EvanGertis
Created June 18, 2018 10:03
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 EvanGertis/f5ffdbb7d52cd35af8e17f9428401f22 to your computer and use it in GitHub Desktop.
Save EvanGertis/f5ffdbb7d52cd35af8e17f9428401f22 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { User } from './user.model';
import {CognitoUser, CognitoUserAttribute, CognitoUserPool} from 'amazon-cognito-identity-js';
const POOL_DATA = {
UserPoolId: 'us-east-1_0Xmtx95ll',
ClientId: '4bnkju4lcmuom5hrgbbdd4io3h'
};
const userPool = new CognitoUserPool(POOL_DATA);
@Injectable()
export class AuthService {
authIsLoading = new BehaviorSubject<boolean>(false);
authDidFail = new BehaviorSubject<boolean>(false);
authStatusChanged = new Subject<boolean>();
registeredUser: CognitoUser;
constructor(private router: Router) {}
signUp(username: string, email: string, password: string): void {
this.authIsLoading.next(true);
const user: User = {
username: username,
email: email,
password: password
};
const attrList: CognitoUserAttribute[] = [];
const emailAttribute = {
Name: 'email',
Value: user.email
};
attrList.push(new CognitoUserAttribute(emailAttribute));
userPool.signUp(user.username, user.password, attrList, null, (err, result) => {
if (err) {
this.authDidFail.next(true);
this.authIsLoading.next(false);
this.registeredUser = result.user;
return;
}
});
this.authDidFail.next(false);
this.authIsLoading.next(true);
}
confirmUser(username: string, code: string) {
this.authIsLoading.next(true);
const userData = {
Username: username,
};
}
signIn(username: string, password: string): void {
this.authIsLoading.next(true);
const authData = {
Username: username,
Password: password
};
this.authStatusChanged.next(true);
return;
}
getAuthenticatedUser() {
}
logout() {
this.authStatusChanged.next(false);
}
isAuthenticated(): Observable<boolean> {
const user = this.getAuthenticatedUser();
const obs = Observable.create((observer) => {
if (!user) {
observer.next(false);
} else {
observer.next(false);
}
observer.complete();
});
return obs;
}
initAuth() {
this.isAuthenticated().subscribe(
(auth) => this.authStatusChanged.next(auth)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment