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

Added docs for authentication #57

Merged
merged 8 commits into from
Jul 13, 2023
43 changes: 43 additions & 0 deletions docs/authentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Authentication

## Devise

[Devise](https://github.com/heartcombo/devise) is a flexible authentication solution that provides a full-featured authentication system that provides a complete MVC solution based on Rails engines. Some of the modules it includes:

**Database Authenticatable**, **Registerable**, **Confirmable**, **Timeoutable**, **Omniauthable**..etc

Here is an example of how Devise is used in our User model:

```
class User < ApplicationRecord
devise :database_authenticatable, :registerable, :confirmable, :trackable, :omniauthable
end
```

Devise is customizable, allowing you to add or remove modules that best suit your application. Devise provides many helper methods like `user_signed_in?` that checks if a user is signed in or `current_user` that returns the **User** record of the signed in user.
jacobperia marked this conversation as resolved.
Show resolved Hide resolved

### Configuration

In the provided codebase, devise in configured in `config/initializers/devise.rb`.
jacobperia marked this conversation as resolved.
Show resolved Hide resolved

1. If using the Rails mailer, don't forget to set the value of the constant `DEFAULT_FROM_EMAIL` for your application.
jacobperia marked this conversation as resolved.
Show resolved Hide resolved

2. If modifying anything related to the sign up or confirmation process, refer to the controllers `RegistrationsController` and `ConfirmationsController` which override and/or inherits from their original devise controllers.
jacobperia marked this conversation as resolved.
Show resolved Hide resolved

## Omniauth

Google OmniAuth is used to authenticate users via Google's OAuth 2.0 API. This means users can sign in to your application using their Google account credentials. This simplifies the sign-in process for users, as they don't need to remember another username/password.
jacobperia marked this conversation as resolved.
Show resolved Hide resolved

### Configuration

In the provided codebase, Google OmniAuth is configured in the same file as devise `config/initializers/devise.rb`.

```
config.omniauth :google_oauth2,
Rails.application.credentials.dig(Rails.env.to_sym, :google, :client_id),
Rails.application.credentials.dig(Rails.env.to_sym, :google, :client_secret),
scope: "email, profile"
```
jacobperia marked this conversation as resolved.
Show resolved Hide resolved

1. The `client_id` and `client_secret` should be setup via the [Google API console](https://console.developers.google.com/) and then stored in the credentials file. You can follow [this tutorial](https://fwuensche.medium.com/how-to-use-google-oauth-on-rails-c6e07047e4fb) to set up your google client keys on the **Google API console** or get them from your admin if the keys are already setup.
2. The google oauth callback creates a **User** record using the `User.from_omniauth(data)` method that can be found in the `User::Omniauthable` concern. You may customize this method according to the requirements of your app.
Loading