Created
October 18, 2021 19:51
-
-
Save sroehrl/0bc1858d33141531fcf0d5741b53fd2d to your computer and use it in GitHub Desktop.
example of neoan3 implementation when using https://github.com/neoan3/email-password-auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Neoan3\Component\Auth; | |
use Neoan3\Core\RouteException; | |
use Neoan3\Frame\Demo; | |
use Neoan3\Model\User\UserModel; | |
use Neoan3\Provider\Auth\Authorization; | |
use Neoan3\Provider\Model\InitModel; | |
/** | |
* Class AuthController | |
* @package Neoan3\Component\Auth | |
* | |
* Generated by neoan3-cli for neoan3 v3.* | |
*/ | |
class AuthController extends Demo{ | |
#[InitModel(UserModel::class)] | |
#[Authorization('restrict')] | |
function getAuth(): array | |
{ | |
$auth = $this->Auth->validate(); | |
return [ | |
'user' => $auth->getPayload() | |
]; | |
} | |
/** | |
* POST: api.v1/auth | |
* @param string $mode | |
* @param array $body | |
* @return array | |
* @throws RouteException | |
*/ | |
#[InitModel(UserModel::class)] | |
function postAuth(string $mode = 'Login', array $body=[]): array | |
{ | |
if(!isset($body['email'])|| !isset($body['password'])){ | |
throw new RouteException('missing fields', 400); | |
} | |
if($mode == 'Register'){ | |
$user = UserModel::create($body); | |
if(empty($user)){ | |
throw new RouteException('duplicate entry', 400); | |
} | |
} else { | |
$user = UserModel::login($body); | |
} | |
$auth = $this->Auth->assign($user['id'], ['all'], $user); | |
return ['token'=> $auth->getToken()]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment