-
Notifications
You must be signed in to change notification settings - Fork 0
/
supabase_authentication.module
62 lines (54 loc) · 1.84 KB
/
supabase_authentication.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
/**
* @file
* Contains the supabase_authentication module hooks.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\Entity\User;
/**
* Implements hook_help().
*/
function supabase_authentication_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.supabase_authentication':
return '<p>' . t('This module integrates Drupal with Supabase for user authentication.') . '</p>';
}
}
/**
* Implements hook_user_insert().
*/
function supabase_authentication_user_insert(User $user) {
$supabase_service = \Drupal::service('supabase_authentication.supabase');
$email = $user->getEmail();
// Retrieve the password using the getPassword method.
$password = $user->getPassword();
// Create user in Supabase.
$supabase_response = $supabase_service->createUser($email, $password);
if (isset($supabase_response->id)) {
$supabase_service->log('notice', 'User @username created in Supabase.', [
'@username' => $email,
]);
// Insert the user's email and password into the custom table using the
// service method.
$supabase_service->storeUserCredentials($email, $password);
}
else {
$supabase_service->log('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)) {
$supabase_service->log('notice', 'User @username authenticated with Supabase.', [
'@username' => $email,
]);
}
else {
$supabase_service->log('error', 'Failed to authenticate user @username with Supabase: @error', [
'@username' => $email,
'@error' => print_r($supabase_auth, TRUE),
]);
}
}