Skip to content

Instantly share code, notes, and snippets.

@akkonrad
Created October 24, 2022 12:38
Show Gist options
  • Save akkonrad/4959919bed3c4c503b649fd697385b3a to your computer and use it in GitHub Desktop.
Save akkonrad/4959919bed3c4c503b649fd697385b3a to your computer and use it in GitHub Desktop.
auth/auth.service.ts
import { Injectable } from '@nestjs/common';
import {
AuthenticationDetails,
CognitoUser,
CognitoUserPool,
CognitoUserSession,
} from 'amazon-cognito-identity-js';
import { AuthConfig } from './auth.config';
@Injectable()
export class AuthService {
private readonly userPool: CognitoUserPool;
constructor(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: CognitoUserSession) => {
resolve(result);
},
onFailure: (err) => {
reject(err);
},
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment