-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.service.ts
54 lines (48 loc) · 1.98 KB
/
authentication.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import {
AuthenticationDetails,
CognitoUser,
CognitoUserPool,
IAuthenticationDetailsData,
CognitoUserSession,
ICognitoUserData
} from 'amazon-cognito-identity-js';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
constructor() {}
signInUser(model: { userName: string; password: string }): Observable<{ userName: string }> {
const authenticationModel: IAuthenticationDetailsData = {
Username: model.userName,
Password: model.password
};
// test - Cognito.AuthenticationDetails instantiated with correct credentials?
const authenticationDetails = new AuthenticationDetails(authenticationModel);
const userData: ICognitoUserData = {
Username: authenticationModel.Username,
// test - Cognito.CognitoUserPool instantiated with the correct UserPoolId and ClientId
Pool: new CognitoUserPool({
UserPoolId: environment.userPoolData.userPoolId,
ClientId: environment.userPoolData.appClientId
})
};
// test - Cognito.CognitoUser instantiated with the correct ICognitoUserData object
const cognitoUser = new CognitoUser(userData);
return new Observable(observer => {
// test - cognitoUser.authenticateUser was called only once
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: (session: CognitoUserSession) => {
// test - on successful login, username must be returned
console.log('*************** SUCCESSFULLY SIGNED-IN!!!!! ********************');
observer.next({ userName: model.userName });
},
onFailure: err => {
console.error(err);
}
});
});
}
}