<commit_before># Omniauth Marvin

OmniAuth OAuth2 strategy for 42 School.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'omniauth-marvin', github: "fakenine/omniauth-marvin"
```

run `bundle install`

## Usage

Register your application on 42's intranet to receive an API Key.

Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`

```ruby
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :marvin, ENV["42_ID"], ENV["42_SECRET"]
end
```

You can now access the OmniAuth 42 OAuth2 URL: `/auth/marvin`

## Devise

If you wish to use this gem with devise, do not use the code snippet above in the Usage section. Instead, follow these steps:

Add the devise gem to your Gemfile.

```ruby
gem 'devise'
```

run `bundle install`

#### Generate migrations and models

```
rails g devise:install
rails g devise user
rails g migration AddLoginToUsers login:string
rails g migration AddOmniauthToUsers provider:index uid:index
rake db:migrate
```

You can add any additional migration you want. For instance, mobile, level, wallet...etc.

#### Declare the provider
`config/initializers/devise.rb`

```ruby
Devise.setup do |config|
  .
  .
  config.omniauth :marvin, ENV["42_ID"], ENV["42_SECRET"]
  .
  .
end
```

Don't forget to set the "42_ID" and "42_SECRET" (your app id and secret) in your environment variables.


#### Make your model omniauthable

In this case, `app/models/user.rb`

```ruby
class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, omniauth_providers: [:marvin]
end
```

#### Add the from_omniauth class method to the user model

`app/models/user.rb`

Example:

```ruby
  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = <PASSWORD>_token[0,20]
      user.login = auth.info.login
      # If your user model has other attributes for which you can get the values via the
      # 42 API, add them here. For instance:
      # user.level = auth.info.level
      # user.image = auth.info.image
    end
  end
```

#### Implement a callback in the routes

`config/routes.rb`

```ruby
  devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
```

#### Create the callbacks controller

`app/controllers/users/omniauth_callbacks_controller.rb`

Example:

```ruby
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def marvin
    @user = User.from_omniauth(request.env["omniauth.auth"])

    if @user.persisted?<commit_msg>update devise doc<commit_after># Omniauth Marvin

OmniAuth OAuth2 strategy for 42 School.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'omniauth-marvin', github: "fakenine/omniauth-marvin"
```

run `bundle install`

## Usage

Register your application on 42's intranet to receive an API Key.

Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`

```ruby
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :marvin, ENV["42_ID"], ENV["42_SECRET"]
end
```

You can now access the OmniAuth 42 OAuth2 URL: `/auth/marvin`

## Devise

If you wish to use this gem with devise, do not use the code snippet above in the Usage section. Instead, follow these steps:

Add the devise gem to your Gemfile.

```ruby
gem 'devise'
```

run `bundle install`

#### Generate migrations and models

```
rails g devise:install
rails g devise user
rails g migration AddNicknameToUsers nickname:string
rails g migration AddOmniauthToUsers provider:index uid:index
rake db:migrate
```

You can add any additional migration you want. For instance, phone, level, wallet...etc.

#### Declare the provider
`config/initializers/devise.rb`

```ruby
Devise.setup do |config|
  .
  .
  config.omniauth :marvin, ENV["42_ID"], ENV["42_SECRET"]
  .
  .
end
```

Don't forget to set the "42_ID" and "42_SECRET" (your app id and secret) in your environment variables.


#### Make your model omniauthable

In this case, `app/models/user.rb`

```ruby
class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, omniauth_providers: [:marvin]
end
```

#### Add the from_omniauth class method to the user model

`app/models/user.rb`

Example:

```ruby
  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = <PASSWORD>.friendly_token[0,20]
      user.nickname = auth.info.nickname
      # If your user model has other attributes for which you can get the values via the
      # 42 API, add them here. For instance:
      # user.level = auth.info.level
      # user.image = auth.info.image
    end
  end
```

#### Implement a callback in the routes

`config/routes.rb`

```ruby
  devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
```

#### Create the callbacks controller

`app/controllers/users/omniauth_callbacks_controller.rb`

Example:

```ruby
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def marvin
    @user = User.from_omniauth(request.env["omniauth.auth"])

    if @user.persisted?
{'fraction_non_alphanumeric': 0.10986382039013617, 'fraction_numerical': 0.009201324990798674, 'mean_word_length': 4.236030828516378, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1806793', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1732, 'n_words': 563}