Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { CognitoUserPool, CognitoUser, AuthenticationDetails } from 'amazon-cognito-identity-js';
- const options = {
- apiVersion: '2012-08-10',
- region: 'us-east-1',
- UserPoolId : '', // Your user pool id here
- ClientId : '' // Your client id here
- }
- const userPool = new CognitoUserPool(options);
- export default class Authenication {
- static signupUser(username,password){
- return new Promise((resolve)=>{
- userPool.signUp(username, password, null, null,(err, result)=>{
- if (err) {
- resolve(err);
- }
- resolve(result)
- })
- })
- }
- static loginUser(username,password){
- const authenticationData = {
- Username:username,
- Password:password,
- };
- const authenticationDetails = new AuthenticationDetails(authenticationData);
- const userData = {
- Username : username,
- Pool : userPool
- };
- const cognitoUser = new CognitoUser(userData);
- return new Promise((resolve,reject)=>{
- cognitoUser.authenticateUser(authenticationDetails, {
- onSuccess: (result)=> {
- resolve(result)
- },
- onFailure: (err) => {
- reject(err);
- },
- });
- },
- )
- }
- static getIdToken(){
- return new Promise((resolve, reject) =>{
- const users = userPool.getCurrentUser()
- if (!users) {
- resolve('')
- }
- userPool.getCurrentUser().getSession((err, session)=>{
- if (err) {
- reject(err)
- }
- resolve(session.getIdToken().getJwtToken())
- })}
- )
- }
- static getUser = () => userPool.getCurrentUser();
- }
Add Comment
Please, Sign In to add comment