Created
June 18, 2018 10:03
-
-
Save EvanGertis/f5ffdbb7d52cd35af8e17f9428401f22 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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