Mushahid Hussain
Mushahid Hussain

Reputation: 4045

Use Yii2-user for user registration in Yii2 Rest Api

I want to implement user registration/login process in my project in which i have web client application made in angular and mobile client is on android.

I am using REST for client/server communication. I have installed yii2-user in my yii developed back-end. What I want is to create a rest api in such a way that each of my client application use yii2-user for user registration and login process. So that in client if user want to register the request will be handled by yii-2 user module? Any suggestion that how can I achieve this? Or is there any better way to make REST api for registration and authentication in Yii2?

Upvotes: 9

Views: 3725

Answers (1)

Fareed Ud Din
Fareed Ud Din

Reputation: 530

Use the yii2-user controller e.g dektrium "UserController" and extend it from \yii\rest\ActiveController then you can specify the model class in the UserController as

public $modelClass = 'dektrium\user\models\User';

In the rest post request use some parameter to give json response for rest request. i.e

register-form[username]=YOURUSERNAME&register-form[password]=YOURPASS&register-form[email][email protected]&someparam=true

In the controller

$model->load(\Yii::$app->request->post()) && $model->register()

will successfully register the user.

You can extend the idea from here. cheers :)

Upvotes: 6

Related Questions