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

feat: add feature to configure module automatically on install #1

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"name": "drupal/supabase_authentication",
"description": "Custom module to integrate Supabase with Drupal 10 for user authentication.",
"type": "drupal-module",
"keywords": ["Drupal", "Firebase", "Authentication"],
"keywords": ["Drupal", "Supabase", "Authentication"],
"require": {
"rafaelwendel/phpsupabase": "^0.0.7"
"rafaelwendel/phpsupabase": "^0.0.7",
"drupal/email_registration": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion src/Form/SupabaseAuthSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,3 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
}

}

1 change: 0 additions & 1 deletion src/SupabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,3 @@ public function signInUser($email, $password) {
}

}

1 change: 1 addition & 0 deletions supabase_authentication.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ core_version_requirement: ^10
package: Custom
dependencies:
- drupal:user
- drupal:email_registration
39 changes: 39 additions & 0 deletions supabase_authentication.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* @file
* Install, update, and uninstall functions for the supabase_auth module.
*/

use Drupal\Core\Entity\Entity\EntityFormDisplay;

/**
* Implements hook_install().
*/
function supabase_authentication_install() {
// Set "Who can register accounts?" field to "Visitors" and untick
// "Require email verification when a visitor creates an account".
$config = \Drupal::configFactory()->getEditable('user.settings');
$config->set('register', 'visitors')
->set('verify_mail', FALSE)
->save();

// Load the form display configuration for the user entity.
$form_display = EntityFormDisplay::load('user.user.default');

// Check if form display is loaded successfully.
if ($form_display) {
$components = $form_display->getComponents();

// Loop through components and disable all fields except "Username and
// password" (account).
foreach ($components as $field_name => $component) {
if ($field_name !== 'account') {
$form_display->removeComponent($field_name);
}
}

// Save the updated form display configuration.
$form_display->save();
}
}
31 changes: 20 additions & 11 deletions supabase_authentication.module
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,34 @@ function supabase_authentication_user_insert(User $user) {
// Create user in Supabase.
$supabase_response = $supabase_service->createUser($email, $password);
if (isset($supabase_response->id)) {
\Drupal::logger('supabase_authentication')->notice('User @username created in Supabase.', ['@username' => $email]);
\Drupal::logger('supabase_authentication')->notice(
'User @username created in Supabase.',
['@username' => $email]
);
}
else {
\Drupal::logger('supabase_authentication')->error('Failed to create user @username in Supabase: @error', [
'@username' => $email,
'@error' => print_r($supabase_response, TRUE),
]);
\Drupal::logger('supabase_authentication')->error(
'Failed to create user @username in Supabase: @error', [
'@username' => $email,
'@error' => print_r($supabase_response, TRUE),
]
);
}

// Sign in the user to get the access token (optional).
$supabase_auth = $supabase_service->signInUser($email, $password);
if (isset($supabase_auth->access_token)) {
\Drupal::logger('supabase_authentication')->notice('User @username authenticated with Supabase.', ['@username' => $email]);
\Drupal::logger('supabase_authentication')->notice(
'User @username authenticated with Supabase.',
['@username' => $email]
);
}
else {
\Drupal::logger('supabase_authentication')->error('Failed to authenticate user @username with Supabase: @error', [
'@username' => $email,
'@error' => print_r($supabase_auth, TRUE),
]);
\Drupal::logger('supabase_authentication')->error(
'Failed to authenticate user @username with Supabase: @error', [
'@username' => $email,
'@error' => print_r($supabase_auth, TRUE),
]
);
}
}

Loading