Last active
October 9, 2016 10:02
-
-
Save ydatech/e89d59bf1af4116e8fd6ab91eddd8f40 to your computer and use it in GitHub Desktop.
Authentication Controller
This file contains hidden or 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 app\controllers; | |
| use Yii; | |
| use yii\rest\Controller; | |
| use yii\filters\Cors; | |
| use app\models\SignupForm; | |
| use app\models\LoginForm; | |
| class AuthController extends Controller{ | |
| // Override behaviors() untuk menambahkan \yii\filters\Cors | |
| public function behaviors(){ | |
| $behaviors = parent::behaviors(); | |
| // unset / hapus authenticator | |
| unset($behaviors['authenticator']); | |
| // tambahkan cors filter | |
| $behaviors['corsFilter'] = [ | |
| 'class' => Cors::className(), | |
| ]; | |
| return $behaviors; | |
| } | |
| public function verbs(){ | |
| // validasi http verbs untuk action signup dan login | |
| $verbs = [ | |
| 'signup'=>['POST'], | |
| 'login'=>['POST'] | |
| ]; | |
| return $verbs; | |
| } | |
| /* | |
| * action signup | |
| * dapat diakses melalui endpoint /auth/signup | |
| */ | |
| public function actionSignup(){ | |
| $model = new SignupForm(); | |
| // load data dari POST request | |
| $model->load(Yii::$app->getRequest()->getBodyParams(), ''); | |
| if($user = $model->signup()){ | |
| return $user; | |
| } | |
| } | |
| /* | |
| * action signup | |
| * dapat diakses melalui endpoint /auth/login | |
| */ | |
| public function actionLogin(){ | |
| $model = new LoginForm(); | |
| // load data dari POST request | |
| $model->load(Yii::$app->getRequest()->getBodyParams(), ''); | |
| if($jwt = $model->login()){ | |
| return $jwt; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment