1. Go to this page and download the library: Download odevnet/dulceauth library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
odevnet / dulceauth example snippets
$configPath = __DIR__ . '/config/config.php'; //
// Define the project base route
define('BASE_PATH', dirname(__DIR__, 1)); // Return to the root of the project from src/config/
// Define common constants here
define('WEB_PAGE', 'yourwebsite.com'); // without http(s), without www and without ending in /
// some examples: define('WEB_PAGE', 'yourwebsite.com'); or define('WEB_PAGE', 'yourwebsite.com/myFolder');
define('EMAIL_FROM', '[email protected]');
// Error log
define('LOG_FILE', BASE_PATH . '/logs/log_file.log'); // IF IT DOES NOT EXIST, CREATE IT OR ESTABLISH A DIRECTORY AND FILE OF YOUR PREFERENCE
// A little configuration about emails...
define('JSON_FILE_VERIFICATION_EMAIL', BASE_PATH . '/config/verification_email.json'); // json template for verification email. Edit the text as you like
define('FORGOT_PASSWORD_PAGE', 'forgot.php'); // default file where the email data (token and user id) is captured
define('JSON_FILE_FORGOT_PASSWORD_EMAIL', BASE_PATH . '/config/forgot_password_email.json'); // json template for forgotten password email. Edit the text as you like
define('VERIFICATION_PAGE', 'verification.php'); // default file where the verification email data is captured
// Roles. At the moment do not modify anything!!
define('DEFAULT_ROLE', 'User'); // default role
define('DEFAULT_VISIBILITY', 'public'); // default profile visibility
// Accounts
define('VERIFIED', '0'); // 0 = unverified account,
if ($dulceAuth->validateTokenAccount($token, $userId)) {
// If the token is validated successfully, we verify the user's account by setting
// the "verified" field value to 1
$dulceAuth->verified($userId);
}
$token = $_GET['token'];
$userId = $_GET['userId'];
if ($dulceAuth->validateTokenPassword($token, $userId)) {
// If it has been validated successfully, here you can display a form to enter the new password...
}
try {
$forgotPassword = $dulceAuth->forgotPassword('[email protected]', false); // return ['userId' => $userId, 'token' => $token];
// We check that `$forgotPassword` returns the userId and the token.
if ($forgotPassword) {
$mail = $dulceAuth->dulceMail();
$mail->from('[email protected]')
->to('[email protected]')
->subject('Password Regeneration')
->message("You received this email because you forgot your password and a token has been generated to reset it.
Click on the following link: yourwebsite.com/forgot.php?token=" . $forgotPassword['token'] . "&userId=" . $forgotPassword['userId'] . " \n
If it wasn't you, please contact administration urgently as your account may be at risk.");
$mail->send();
// If the sending is successful, we can display a message in the browser.
if ($mail->send()) {
echo "We have just sent you an email.
Please check your email to create a new password that you will remember.";
}
}
} catch (TokenSaveException $ex) {
Logger::error($ex->getMessage(), $ex->getTraceAsString());
echo $ex->getMessage();
} catch (UserNotFoundException $ex) {
Logger::error($ex->getMessage(), $ex->getTraceAsString());
echo $ex->getMessage();
} catch (RuntimeException $ex) {
Logger::error($ex->getMessage(), $ex->getTraceAsString());
echo $ex->getMessage();
} catch (InvalidArgumentException $ex) {
Logger::error($ex->getMessage(), $ex->getTraceAsString());
echo $ex->getMessage();
}
try {
$roles = $dulceAuth->userRoles(2);
echo 'The user with ID 2 has the following roles: <br>';
foreach ($roles as $role) {
echo "ROLE: $role->name <br>";
}
} catch (UserNotFoundException $ex) {
Logger::error($ex->getMessage(), $ex->getTraceAsString());
echo $ex->getMessage();
}
if ($dulceAuth->hasRole('Admin')) {
echo 'You are an administrator.';
// Here you can add an administration area or anything else...
} else {
echo 'You do not have the ole('Admin')) {
echo 'You are an administrator.';
// Here you can add an administration area or anything else...
} else {
echo 'You do not have the
if ($dulceAuth->hasRole('SuperAdmin', 1)) {
echo 'The user has the role!';
} else {
echo 'The user does not have the role! :-(';
}
if ($dulceAuth->hasPermission('Create user')) {
echo 'You have the necessary permission to create users.';
} else {
echo 'You do not have permission to perform this action.';
}
$dulceAuth->session()->set('color', 'red');
echo $dulceAuth->session()->get('color');
$dulceAuth->session()->has('color'); // returns true or false
if ($dulceAuth->session()->has('color')) {
echo 'It exists!';
} else {
echo 'It does not exist!';
}
$dulceAuth->session()->remove('name');
$dulceAuth->session()->destroy(); // destroys the entire session.
echo $dulceAuth->session()->get('userId');
$dulceAuth->session()->get('expire_time');
echo $dulceAuth->session()->expirationTime(); // displays time in format: Y-m-d H:i:s
if ($dulceAuth->session()->isValid()) {
echo 'The session is active';
// We can do anything here...
} else {
echo 'The session has expired';
}
$mail = $dulceAuth->dulceMail();
// We prepare the email: sender, recipient, subject and message
$mail->from('[email protected]')->to('[email protected]')->subject('Subject/topic')->message('Any message...');
// and we send it
$mail->send();
try {
$mail = $dulceAuth->dulceMail();
$mail->from('[email protected]')->to('[email protected]')->subject('Subject/topic')->message('Any message...');
$send = $mail->send();
// If the sending is successful, we can display a message in the browser.
if ($send) {
echo 'We just sent you an email.';
}
} catch (RuntimeException $ex) {
Logger::error($ex->getMessage(), $ex->getTraceAsString());
echo $ex->getMessage();
} catch (InvalidArgumentException $ex) {
Logger::error($ex->getMessage(), $ex->getTraceAsString());
echo $ex->getMessage();
}
$dulceAuth->dulce->addService('Name of the service', function ($dulce) {
return new espacioDeNombres\Servicio();
});
$dulceAuth->dulce->addService('Forms', function ($dulce) {
return new helpers\Form();
});
echo $dulceAuth->dulce->get('Forms');