forked from projects/fipamo
ro
064407aa88
a class for members was needed for long term handling of member functions like login, update, status checking, etc so that class was created and the AuthService class was removed as it was redundant and it's functionaity moved to the member class
108 lines
2.9 KiB
PHP
108 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Interfaces\MemberRepositoryInterface;
|
|
use ReallySimpleJWT\Token;
|
|
|
|
use function _\find;
|
|
|
|
class MemberRepository implements MemberRepositoryInterface
|
|
{
|
|
protected $folks;
|
|
|
|
public function __construct()
|
|
{
|
|
if (file_exists(env('FOLKS_PATH'))) {
|
|
$this->folks = json_decode(file_get_contents(env('FOLKS_PATH')), true);
|
|
} else {
|
|
$this->folks = json_decode(file_get_contents('../content/init/folks-template.json'), true);
|
|
}
|
|
}
|
|
|
|
public function getAll()
|
|
{
|
|
return $this->$folks;
|
|
}
|
|
|
|
public function getById($id)
|
|
{
|
|
$member = find($this->folks, ['id' => $id]);
|
|
return $member;
|
|
}
|
|
|
|
public function getByHandle($handle)
|
|
{
|
|
$member = find($this->folks, ['handle' => $handle]);
|
|
return $member;
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
//delete member stuff
|
|
}
|
|
|
|
public function create($member)
|
|
{
|
|
//make new member
|
|
}
|
|
|
|
public function update($id)
|
|
{
|
|
//update member data
|
|
}
|
|
|
|
public function auth($request)
|
|
{
|
|
//suth stuff
|
|
$folks = $this->folks;
|
|
$found = $this->getByHandle($request->handle);
|
|
if ($found) {
|
|
if (password_verify($request->password, $found['password'])) {
|
|
$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);
|
|
return ['status' => true, 'message' => 'HEY WELCOME BACK'];
|
|
//DO SESSION STUFF
|
|
} else {
|
|
return ['status' => false, 'message' => 'CHECK THAT PASSWORD'];
|
|
//RETURN ERROR
|
|
}
|
|
} else {
|
|
return ['status' => false, 'message' => 'CHECK THAT HANDLE'];
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|