Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {userPoolId, clientId} from './env.js'
- import {CognitoUserPool} from 'amazon-cognito-identity-js'
- import {CognitoUserAttribute} from 'amazon-cognito-identity-js'
- import {CognitoUser} from 'amazon-cognito-identity-js'
- import {AuthenticationDetails} from 'amazon-cognito-identity-js'
- var userPoolData = {
- UserPoolId : userPoolId,
- ClientId : clientId
- }
- const userPool = new CognitoUserPool(userPoolData)
- const helloBtn = document.querySelector('#hello')
- helloBtn.addEventListener('click', ()=> {
- alert("No siema ziomeczki")
- })
- const bob = {
- userName: 'lubiePlackiDobre',
- password: '123qwe123',
- email: 'wtt59574@cndps.com'
- }
- const handleRegistration = (rRequest) => {
- userPool.signUp(
- rRequest.username,
- rRequest.password,
- [
- new CognitoUserAttribute({
- Name: 'name',
- Value: rRequest.username,
- }),
- new CognitoUserAttribute({
- Name: 'email',
- Value: rRequest.email,
- })
- ],
- null,
- (err, result) => {
- if(err){
- alert(err.message);
- return;
- }
- console.log('yaay ' + rRequest.username + ' is registered');
- }
- )
- }
- const registerBtn = document.querySelector('#testRegister')
- registerBtn.addEventListener('click', ()=> {
- const rRequest = {
- username: bob.userName,
- password: bob.password,
- email: bob.email
- }
- handleRegistration(rRequest)
- })
- const handleLogin = (loginRequest) => {
- const cognitoUser = new CognitoUser({
- Username: loginRequest.username,
- Pool: userPool,
- });
- const authDetails = new AuthenticationDetails({
- Username: loginRequest.username,
- Password: loginRequest.password
- });
- cognitoUser.authenticateUser(authDetails, {
- onSuccess: (result) => {
- console.log(result);
- alert("Yaaay, I am in!");
- },
- onFailure: (err) => {
- console.log(err);
- alert("Omg, does not wooork :(");
- }
- })
- }
- const loginBtn = document.querySelector('#testLogin')
- loginBtn.addEventListener('click', ()=>{
- const loginRequest = {
- username : bob.userName,
- password : bob.password
- }
- handleLogin(loginRequest)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement