Skip to content

Instantly share code, notes, and snippets.

@jacobdo2
Last active June 8, 2020 09:08
Show Gist options
  • Save jacobdo2/c30594b71fe5e9173558ee9707ac9b4c to your computer and use it in GitHub Desktop.
Save jacobdo2/c30594b71fe5e9173558ee9707ac9b4c to your computer and use it in GitHub Desktop.
AuthService
import { AuthConfig } from './auth.config';
import { Inject, Injectable } from '@nestjs/common';
import {
AuthenticationDetails,
CognitoUser,
CognitoUserPool,
} from 'amazon-cognito-identity-js';
@Injectable()
export class AuthService {
private userPool: CognitoUserPool;
constructor(
@Inject('AuthConfig')
private readonly authConfig: AuthConfig,
) {
this.userPool = new CognitoUserPool({
UserPoolId: this.authConfig.userPoolId,
ClientId: this.authConfig.clientId,
});
}
authenticateUser(user: { name: string, password: string}) {
const { name, password } = user;
const authenticationDetails = new AuthenticationDetails({
Username: name,
Password: password,
});
const userData = {
Username: name,
Pool: this.userPool,
};
const newUser = new CognitoUser(userData);
return new Promise((resolve, reject) => {
return newUser.authenticateUser(authenticationDetails, {
onSuccess: result => {
resolve(result);
},
onFailure: err => {
reject(err);
},
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment