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

Save login number to constituent table #705

Open
3 tasks
Tracked by #455
manishapriya94 opened this issue Nov 8, 2023 · 0 comments
Open
3 tasks
Tracked by #455

Save login number to constituent table #705

manishapriya94 opened this issue Nov 8, 2023 · 0 comments
Assignees

Comments

@manishapriya94
Copy link
Contributor

manishapriya94 commented Nov 8, 2023

Goal: collect number from Auth0 login process and save into constituent table so we have the ability to send notifications later on

Background info

Constituent table lives within: amplify/server/db/migrations/20220413154827_rename-volunteers-table-to-constituents.js

Image

Data types:

  • name is a string
  • Street address, City, and State are strings
  • Zipcode is a string
  • Letters sent holds an array of ids of letter that was successfully posted with Lob (payment and address verification went through)
  • User agreement is a boolean that they abide by the platforms outlined = community safety and protection guidelines
  • Updates is sending campaign_id so that we can send to advocacy groups so they can follow up and by our user education team

Directions

To save the user's phone number from Auth0 Universal Login to the constituent table, you can modify the up function of the migration file that creates the constituent table. Here are the steps you can follow:

  • 1. Add a new column to the constituent table to store the phone number:
exports.up = function(knex) {
  return knex.schema.createTable('constituent', (table) => {
    table.increments('id').primary()
    table.string('name').notNullable()
    table.string('email').notNullable().unique()
    table.string('phone_number') // Add this line
    table.timestamps(true, true)
  })
}
  • 2. Modify the down function of the migration file to drop the phone_number column:
exports.down = function(knex) {
  return knex.schema.dropTable('constituent')
}
  • 3. In your Express app, modify the /callback route to save the user's phone number to the constituent table:
router.get('/callback', passport.authenticate('auth0', {
  failureRedirect: '/login'
}), async (req, res) => {
  const { phone_number } = req.user._json // Get the phone number from the user object
  const { name, email } = req.user // Get the name and email from the user object

  try {
    // Check if the user already exists in the constituent table
    const existingConstituent = await knex('constituent').where({ email }).first()

    if (existingConstituent) {
      // Update the existing constituent with the phone number
      await knex('constituent').where({ email }).update({ phone_number })
    } else {
      // Create a new constituent with the name, email, and phone number
      await knex('constituent').insert({ name, email, phone_number })
    }

    res.redirect('/')
  } catch (error) {
    console.error(error)
    res.status(500).send('Internal server error')
  }
})

This code gets the phone number from the req.user._json object, which is provided by Passport when the user is authenticated. It then checks if the user already exists in the constituent table based on their email address. If the user exists, it updates their phone number. If the user does not exist, it creates a new constituent with the name, email, and phone number.

  • Note that you will need to import the knex object and the passport middleware in your Express app for this code to work.
@beachsideproperty beachsideproperty self-assigned this Nov 8, 2023
@EmaleeSoto EmaleeSoto self-assigned this Nov 16, 2023
Alex-is-Gonzalez added a commit that referenced this issue Dec 11, 2023
Added phone number to the constituent table. Only Step 1 and 2 completed on issue #705
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants