Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write a script to generate a sample id_token #31

Open
gcarvellas opened this issue Jul 29, 2023 · 0 comments
Open

Write a script to generate a sample id_token #31

gcarvellas opened this issue Jul 29, 2023 · 0 comments
Assignees
Labels
devtools ci/cd, precommit hooks, dev scripts, etc.

Comments

@gcarvellas
Copy link
Contributor

The current way to get an id_token/access_token is weird. It would be nice to have a script to do this in the backend repo.

To get an id token, you need to open the frontend repo, and modify src/components/Signup.tsx to this:

import { CognitoUserAttribute, CognitoUser, AuthenticationDetails } from "amazon-cognito-identity-js";
import React, { useState } from "react";
import UserPool from "../utils/CognitoUserPool";

export const Signup = () => {
  const [email, setEmail] = useState({
    Name: "email",
    Value: "",
  });
  const [vendorType, setVendorType] = useState({
    Name: "custom:vendor_type",
    Value: "",
  });
  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");

  const onSignUp = (e: any) => {
    const attributeList = [];
    e.preventDefault();

    const attributeEmail = new CognitoUserAttribute(email);
    //const attributeVendor = new CognitoUserAttribute(vendorType);

    attributeList.push(attributeEmail);
    //SattributeList.push(attributeVendor);

    UserPool.signUp(
      username,
      password,
      attributeList,
      attributeList,
      (err, data) => {
        if (err) {
          console.log(err);
        } else{
          /* Debug */

          alert("Confirm user");
          
          const cognitoUser = data.user;
          const authenticationDetails = new AuthenticationDetails({
            Username: username,
            Password: password,
          });

          cognitoUser.authenticateUser(authenticationDetails, {
            onSuccess: (loggedInUser) => {
              const accessToken = loggedInUser.getAccessToken().getJwtToken();
               const idToken = loggedInUser.getIdToken().getJwtToken();
               console.log("Access token:", accessToken);
               console.log("ID Token:", idToken);
            },
            onFailure: (err) => {
              console.log(err);
            },
          });
        }
      }
    );
  };

  const handleVendorType = (e: any) => {
    setVendorType({ ...vendorType, Value: e.target.value });
  };

  return (
    <div>
      <h1>Create an account</h1>
      <form className="flex flex-col" onSubmit={onSignUp}>
        <label className="required" htmlFor="username">
          Username
        </label>
        <input
          id="username"
          type="text"
          onChange={(e) => setUsername(e.target.value)}
        />
        <label className="required" htmlFor="email">
          Email
        </label>
        <input
          id="email"
          type="text"
          onChange={(e) => setEmail({ ...email, Value: e.target.value })}
        />
        <label className="required" htmlFor="password">
          Password
        </label>
        <input
          id="password"
          type="password"
          onChange={(e) => setPassword(e.target.value)}
        />
        <div className="flex">
          <h2>What type of vendor are you applying as?</h2>
          <input
            type="radio"
            id="artist"
            value="artist"
            onChange={handleVendorType}
          />
          <label htmlFor="artist">Artist</label>
          <input
            type="radio"
            id="dealer"
            value="dealer"
            onChange={handleVendorType}
          />
          <label htmlFor="artist">Dealer</label>
        </div>
        <button type="submit">Create Account</button>
      </form>
    </div>
  );
};

Once this is done,

  • startup the frontend
  • open developer logs (any errors will appear in there)
  • enter a fake username, password, and email. If successful, a prompt should appear saying Cognito. DONT CLICK ON IT YET
  • Go to aws with your cpac email
  • Click Cognito
  • Click aa-dr
  • Scroll down and click the hyperlink on the user you just addewd
  • Click Actions on the top right
  • Select Confirm account
  • Go back to the frontend and click Ok
  • The access token and the id token should appear in the logs. If you don't know what's the difference, just use the id token
@gcarvellas gcarvellas added enhancement New feature or request testing Unit/Integration/E2E labels Jul 29, 2023
@g00gol g00gol self-assigned this Aug 4, 2023
@gcarvellas gcarvellas added devtools ci/cd, precommit hooks, dev scripts, etc. and removed enhancement New feature or request testing Unit/Integration/E2E labels Sep 29, 2023
@gcarvellas gcarvellas self-assigned this Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devtools ci/cd, precommit hooks, dev scripts, etc.
Projects
None yet
Development

No branches or pull requests

2 participants