Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {userPoolId, clientId } from './env.js'
- import {CognitoUserPool, CognitoUserAttribute} from 'amazon-cognito-identity-js'
- import {CognitoUser, AuthenticationDetails} from 'amazon-cognito-identity-js'
- const userPoolData = {
- UserPoolId : userPoolId,
- ClientId : clientId
- }
- const userPool = new CognitoUserPool(userPoolData)
- const helloBtn = document.querySelector('#hello')
- helloBtn.addEventListener('click', () => {
- alert("Hello Jakub")
- })
- const bob = {
- username: 'jkanclerz-123456',
- password: '123qwe123',
- email: 'gnr20280@cndps.com'
- }
- const handleRegistration = (registerRequest) => {
- userPool.signUp(
- registerRequest.username,
- registerRequest.password,
- [
- new CognitoUserAttribute({
- Name: 'name',
- Value: registerRequest.name
- }),
- new CognitoUserAttribute({
- Name: 'email',
- Value: registerRequest.email
- })
- ],
- null,
- (err, result) => {
- if (err) {
- alert(err.message);
- return;
- }
- console.log(`Hurayy user ${registerRequest.username} is registered`)
- }
- )
- }
- const registerBtn = document.querySelector('#testRegister')
- registerBtn.addEventListener('click', () => {
- const registerRequest = {
- username: bob.username,
- password: bob.password,
- name: 'Uncle bob',
- email: bob.email,
- }
- handleRegistration(registerRequest)
- })
- const handleLogin = (loginRequest) => {
- const cognitoUser = new CognitoUser({
- Username: loginRequest.Username,
- Pool: userPool
- })
- const authDetails = new AuthenticationDetails(loginRequest)
- cognitoUser.authenticateUser(authDetails, {
- onSuccess: (result) => {
- console.log(result)
- alert('Hurayy i am in');
- },
- onFailure: (err) => {
- console.log(err);
- alert("Something is not YES!!!!")
- }
- })
- }
- const loginBtn = document.querySelector('#testLogin')
- loginBtn.addEventListener('click', () => {
- const loginRequest = {
- Username: bob.username,
- Password: bob.password
- }
- handleLogin(loginRequest)
- })
- const handleConfirm = (confirmRequest) => {
- const cognitoUser = new CognitoUser({
- Username: confirmRequest.Username,
- Pool: userPool
- })
- cognitoUser.confirmRegistration(
- confirmRequest.code,
- true,
- (err, result) => {
- if (err) {
- alert(err);
- return;
- }
- alert('hurray confirmed');
- });
- }
- const confirmBtn = document.querySelector('#testConfirm')
- confirmBtn.addEventListener('click', () => {
- const confirmRequest = {
- Username: bob.username,
- code: '045088'
- }
- handleConfirm(confirmRequest);
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement