maulikam / nearnstyle-codebase

1 stars 0 forks source link

User Registration and Login Flow #33

Closed maulikam closed 1 year ago

maulikam commented 1 year ago

To implement registration and login using mobile number and OTP, we need to make several considerations:

  1. Storage of OTP: You will need a table to store OTPs associated with mobile numbers, along with expiration timestamps.

  2. OTP Generation: Typically, a random number generator can be used to generate OTPs. You might want to consider setting an expiration time for the OTP.

  3. Validation and Authentication: When a user provides an OTP for validation, you'll have to check if the OTP is valid (i.e., if it matches the OTP stored in the database and hasn't expired). If it is valid, the user is authenticated.

  4. Integration with OAuth: Once authenticated using OTP, you would typically generate an OAuth token for the user which can then be used for authorizing subsequent requests.

  5. User Registration: If the user is registering for the first time, they would provide the mobile number and get an OTP. Upon validating the OTP, a new user account can be created.

Here's a possible approach to structure the additional tables:

-- OTP storage table
CREATE TABLE user_otp (
    otp_id BIGSERIAL PRIMARY KEY,
    phone_number VARCHAR(255) NOT NULL UNIQUE REFERENCES users(phone_number),
    otp_code VARCHAR(6) NOT NULL,
    expiration_time TIMESTAMP NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    is_used BOOLEAN NOT NULL DEFAULT FALSE
);

-- This table will store the user registration requests. The status field will help in determining if the user has validated the OTP.
CREATE TABLE user_registration (
    registration_id BIGSERIAL PRIMARY KEY,
    phone_number VARCHAR(255) NOT NULL UNIQUE,
    email_address VARCHAR(255) UNIQUE,
    full_name VARCHAR(255),
    password VARCHAR(255),
    user_type VARCHAR(20) NOT NULL CHECK (user_type IN ('CUSTOMER', 'SALON_OWNER', 'ADMIN')),
    profile_picture VARCHAR(255),
    status VARCHAR(20) NOT NULL CHECK (status IN ('PENDING_OTP', 'COMPLETED'))
);

Flow for Registration:

  1. User submits phone number.
  2. System generates OTP, saves it in user_otp table, and sends it to the user.
  3. User submits OTP for validation.
  4. System checks OTP against the user_otp table.
  5. If valid, user details are saved in the users table, and an OAuth token is generated and sent to the user. The status in the user_registration table is set to COMPLETED.
  6. If invalid, an error is sent back to the user.

Flow for Login:

  1. User submits phone number.
  2. System generates OTP, saves it in user_otp table, and sends it to the user.
  3. User submits OTP for validation.
  4. System checks OTP against the user_otp table.
  5. If valid, an OAuth token is generated and sent to the user.
  6. If invalid, an error is sent back to the user.

You will also need APIs to handle these flows, integration with an SMS gateway to send OTPs, and logic to handle OTP expiration and retry scenarios.