2024-03-01 20:34:36 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2024-03-06 16:50:09 +01:00
|
|
|
use ReallySimpleJWT\Token;
|
|
|
|
|
2024-03-01 20:34:36 +01:00
|
|
|
use function _\find;
|
|
|
|
|
|
|
|
class AuthService
|
|
|
|
{
|
|
|
|
protected $config;
|
2024-03-03 20:48:22 +01:00
|
|
|
protected $request;
|
2024-03-01 20:34:36 +01:00
|
|
|
|
|
|
|
public function __construct(SettingsService $config)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
2024-03-03 20:48:22 +01:00
|
|
|
public function check($request)
|
2024-03-01 20:34:36 +01:00
|
|
|
{
|
|
|
|
$folks = $this->config->getFolks();
|
2024-03-03 20:48:22 +01:00
|
|
|
$found = find($folks, ['handle' => $request->handle]);
|
2024-03-01 20:34:36 +01:00
|
|
|
if ($found) {
|
2024-03-03 20:48:22 +01:00
|
|
|
if (password_verify($request->password, $found['password'])) {
|
2024-03-06 16:50:09 +01:00
|
|
|
$member = [
|
|
|
|
'handle' => $found['handle'],
|
|
|
|
'email' => $found['email'],
|
|
|
|
'role' => $found['role'],
|
|
|
|
'avatar' => $found['avi'],
|
|
|
|
'key' => $found['key'],
|
|
|
|
'secret' => $found['secret'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$token = Token::create(
|
|
|
|
$found['key'],
|
|
|
|
$found['secret'],
|
|
|
|
time() + 3600,
|
|
|
|
'localhost'
|
|
|
|
); //expires in an hour
|
|
|
|
$form_token = md5(uniqid(microtime(), true));
|
|
|
|
$request->session()->put('member', $member);
|
|
|
|
$request->session()->put('token', $token);
|
|
|
|
$request->session()->put('form_token', $form_token);
|
2024-03-03 20:48:22 +01:00
|
|
|
return ['status' => true, 'message' => 'HEY WELCOME BACK'];
|
2024-03-01 20:34:36 +01:00
|
|
|
//DO SESSION STUFF
|
|
|
|
} else {
|
2024-03-03 20:48:22 +01:00
|
|
|
return ['status' => false, 'message' => 'CHECK THAT PASSWORD'];
|
2024-03-01 20:34:36 +01:00
|
|
|
//RETURN ERROR
|
|
|
|
}
|
|
|
|
} else {
|
2024-03-03 20:48:22 +01:00
|
|
|
return ['status' => false, 'message' => 'CHECK THAT HANDLE'];
|
2024-03-01 20:34:36 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-06 16:50:09 +01:00
|
|
|
|
|
|
|
public static function status()
|
|
|
|
{
|
|
|
|
if (session('member') !== null) {
|
|
|
|
if (
|
|
|
|
Token::validate(session('token'), session('member')['secret']) &&
|
|
|
|
Token::validateExpiration(session('token'), session('member')['secret'])
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2024-03-01 20:34:36 +01:00
|
|
|
}
|