Skip to content
Oleg Valter edited this page Sep 12, 2024 · 4 revisions

Backend

Authentication

Devise

Overview

Source repository: https://github.com/heartcombo/devise

Using custom layouts for views

ref: https://github.com/heartcombo/devise/wiki/How-To:-Create-custom-layouts

Use layout to override the default one for a single controller (extending from Devise::RegistrationsController).
If needed, it can also be constrained with only to a subset of views:

class Users::RegistrationsController < Devise::RegistrationsController
  layout 'without_sidebar', only: :edit
end

Troubleshooting

Docker

Database

If you get the following error upon starting the Rails server or trying to run the entrypoint.sh script:

There is an issue connecting to your database with your username/password, username: <your username>.

First, check the configuration, specifically that:

  • the credentials provided in the docker/env file are correct (MYSQL_USER should also match the username from the error message). If you haven't changed the defaults, they should look like this:

    MYSQL_ROOT_PASSWORD=qpixel
    MYSQL_DATABASE=qpixel
    MYSQL_USER=qpixel
    MYSQL_PASSWORD=qpixel
    
  • the credentials in the docker/env file match the ones in config/database.docker.yml. The default section should look like this (irrelevant keys omitted for brevity). Note that username should match MYSQL_USER and password should match MYSQL_PASSWORD:

    default: &default
      username: qpixel
      password: qpixel
  • the user whose username was in the error message (and that matches username and MYSQL_USER) exists and has the grants necessary to connect to the database and manage it. First, connect to the database container, for example, with docker exec -it qpixel-db-1 bash. Once inside, enter the MySQL CLI as root (mysql -u root -p). After that, confirm that you are root with select user(), current_user(); The output should look like this:

    +----------------+----------------+
    | user()         | current_user() |
    +----------------+----------------+
    | root@localhost | root@localhost |
    +----------------+----------------+
    

    If you are root, switch to the mysql database (use mysql;) and check if the user that is supposed to connect to the database exists, has a correct authentication plugin, and host by running a select host, user, plugin from user; query. The output should look something like this (irrelevant users omitted for brevity). The correct output must have a user matching the one from MYSQL_USER [docker/env] and username [config/database.docker.yml] with host set to % and authentication plugin set to mysql_native_password:

    +------------------+-----------+-----------------------+
    | user             | host      | plugin                |
    +------------------+-----------+-----------------------+
    | qpixel           | %         | mysql_native_password |
    | root             | %         | caching_sha2_password |
    | root             | localhost | mysql_native_password |
    +------------------+-----------+-----------------------+
    

    If there is no such user, create one with create user '<your username>'@'%' identified with mysql_native_password by '<your password>'; (note that the password must match MYSQL_PASSWORD [docker/env] and password [config/database.docker.yml]). After the user is created, add the necessary grants to it:

    grant usage on *.* to '<your username>'@'%';
    grant all privileges on qpixel.* to '<your username>'@'%';
    grant all privileges on qpixel_dev.* to '<your username>'@'%';
    grant all privileges on qpixel_test.* to '<your username>'@'%';
    

    For the grants to take effect immediately, run flush privileges; afterwards. At this point, the error should be resolved. Restart the services to be sure everything's applied correctly - the user should successfully connect to the database.

    If the user does exist, check the grants with show grants for '<your username>'@'%'. If the output doesn't look like below, you'll need to add the missing grants (see above) depending on what's amiss (note that there should be a USAGE grant on *.* and ALL PRIVILEGES grant for the qpixel, qpixel_dev, and qpixel_test databases):

    +---------------------------------------------------------+
    | Grants for qpixel@%                                     |
    +---------------------------------------------------------+
    | GRANT USAGE ON *.* TO `qpixel`@`%`                      |
    | GRANT ALL PRIVILEGES ON `qpixel`.* TO `qpixel`@`%`      |
    | GRANT ALL PRIVILEGES ON `qpixel_dev`.* TO `qpixel`@`%`  |
    | GRANT ALL PRIVILEGES ON `qpixel_test`.* TO `qpixel`@`%` |
    +---------------------------------------------------------+