title: how to add hashed passwords in yii2
body: <p>using Yii 2 basic Not the advanced version.</p>
  
  <p>I have a crud admin authentication system. Which stores just an id, username and password in the database. When the user goes to log in if the username and password are correct they are logged in. </p>
  
  <p>However i now want to make these passwords secure so i want to salt and hash them. This is the part i am finding hard to do Or more so where to put things.</p>
  
  <p><strong>Part 1:</strong>
  I have an AdminController which goes along with my User Model Create.php page.
  <strong>Part 2:</strong>
  I have a siteController which goes along with the LoginForm model and the login.php page to log in.</p>
  
  <p>I will go over part one first as it will obviously have to actually generate a hashed password here.</p>
  
  <p><strong>AdminController:</strong></p>
  
  <pre><code>public function actionCreate()
  {
      $model = new User();
  
      if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;save()) {
          return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;id]);
      } else {
          return $this-&gt;render('create', [
              'model' =&gt; $model,
          ]);
      }
  }
  </code></pre>
  
  <p><strong>User.php</strong></p>
  
  <pre><code>    &lt;?php
  
      namespace app\models;
      use yii\base\NotSupportedException;
      use yii\db\ActiveRecord;
      use yii\web\IdentityInterface;
      use yii\data\ActiveDataProvider;
      /**
       * User model
       *
       * @property integer $id
       * @property string $username
       * @property string $password
       */
  class User extends ActiveRecord implements IdentityInterface
  {
  
  
  /**
   * @inheritdoc
   */
  public static function tableName()
  {
      return 'Users';
  }
  
  
  public function rules(){
          return [
              [['username','password'], 'required']
          ];
  }
  
  
  
  public static function findAdmins(){
      $query = self::find();
  
      $dataProvider = new ActiveDataProvider([
          'query' =&gt; $query,
      ]);
  
      return $dataProvider;
  
  }
  
  
  /**
   * @inheritdoc
   */
  public static function findIdentity($id)
  {
      return static::findOne(['id' =&gt; $id]);
  }
  
  
  
  /**
   * @inheritdoc
   */
  public static function findIdentityByAccessToken($token, $type = null)
  {
      throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  }
  
  /**
   * Finds user by username
   *
   * @param  string      $username
   * @return static|null
   */
  public static function findByUsername($username)
  {
      return static::findOne(['username' =&gt; $username]);
  }
  
  /**
   * @inheritdoc
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * @inheritdoc
   */
  public function getAuthKey()
  {
      return static::findOne('AuthKey');
  }
  
  /**
   * @inheritdoc
   */
  public function validateAuthKey($authKey)
  {
      return static::findOne(['AuthKey' =&gt; $authKey]);
  }
  
  /**
   * Validates password
   *
   * @param  string  $password password to validate
   * @return boolean if password provided is valid for current user
   */
  public function validatePassword($password)
  {
      return $this-&gt;password === $password;
  }
  }
  </code></pre>
  
  <p><strong>Question??:</strong>
  So as you can see in this model i only have id, username and password coming from the database, so i take i will need to create one for field in db called "hashed_password"?</p>
  
  <p><strong>create.php:</strong></p>
  
  <pre><code>&lt;?php $form = ActiveForm::begin(); ?&gt;
  
  &lt;?= $form-&gt;field($model, 'username')-&gt;textInput(['maxlength' =&gt; 50]) ?&gt;
  
  &lt;?= $form-&gt;field($model, 'password')-&gt;passwordInput(['maxlength' =&gt; 50]) ?&gt;
  
  &lt;div class="form-group"&gt;
      &lt;?= Html::submitButton($model-&gt;isNewRecord ? 'Create' : 'Update', ['class' =&gt; $model-&gt;isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?&gt;
  &lt;/div&gt;
  
  &lt;?php ActiveForm::end(); ?&gt;
  </code></pre>
  
  <p>Right so that was part 1, the actual bit where the hashed password needs to be generated and saved into the database, how can i achieve this?</p>
  
  <p>Okay moving on the <strong>Part2:</strong></p>
  
  <p><strong>SiteController:</strong></p>
  
  <pre><code>public function actionLogin()
  {
      if (!\Yii::$app-&gt;user-&gt;isGuest) {
          return $this-&gt;goHome();
      }
  
      $model = new LoginForm();
  
      if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;login()) {
          return $this-&gt;goBack();
      } else {
          return $this-&gt;render('login', [
              'model' =&gt; $model,
          ]);
      }
  }
  </code></pre>
  
  <p><strong>LoginForm.php (model):</strong></p>
  
  <pre><code>class LoginForm extends Model
  {
  public $username;
  public $password;
  public $rememberMe = true;
  
  private $_user = false;
  
  
  /**
   * @return array the validation rules.
   */
  public function rules()
  {
      return [
          // username and password are both required
          [['username', 'password'], 'required'],
          // rememberMe must be a boolean value
          ['rememberMe', 'boolean'],
          // password is validated by validatePassword()
          ['password', 'validatePassword'],
      ];
  }
  
  /**
   * Validates the password.
   * This method serves as the inline validation for password.
   *
   * @param string $attribute the attribute currently being validated
   * @param array $params the additional name-value pairs given in the rule
   */
  public function validatePassword($attribute, $params)
  {
      if (!$this-&gt;hasErrors()) {
          $user = $this-&gt;getUser();
  
          if (!$user || !$user-&gt;validatePassword($this-&gt;password)) {
              $this-&gt;addError($attribute, 'Incorrect username or password.');
          }
      }
  }
  
  /**
   * Logs in a user using the provided username and password.
   * @return boolean whether the user is logged in successfully
   */
  public function login()
  {
      if ($this-&gt;validate()) {
          return Yii::$app-&gt;user-&gt;login($this-&gt;getUser(), $this-&gt;rememberMe ? 3600*24*30 : 0);
      } else {
          return false;
      }
  }
  
  /**
   * Finds user by [[username]]
   *
   * @return User|null
   */
  public function getUser()
  {
      if ($this-&gt;_user === false) {
          $this-&gt;_user = User::findByUsername($this-&gt;username);
      }
  
      return $this-&gt;_user;
  }
  }
  </code></pre>
  
  <p>Login.php:</p>
  
  <pre><code>&lt;?php $form = ActiveForm::begin(); ?&gt;
  
  &lt;?= $form-&gt;field($model, 'username'); ?&gt; 
  &lt;?= $form-&gt;field($model, 'password')-&gt;passwordInput(); ?&gt; 
  
  
  &lt;div class="form-group"&gt;
      &lt;div class="col-lg-offset-1 col-lg-11"&gt;
          &lt;?= Html::submitButton('Login', ['class' =&gt; 'btn btn-primary', 'name' =&gt; 'login-button']) ?&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>So thats it, how do i integrate a hashed_password for each user when they create and then validate this upon login?</p>
  
  <p>I have been reading this on the documentation but just cant get this to work <a href="http://www.yiiframework.com/doc-2.0/guide-security-passwords.html">http://www.yiiframework.com/doc-2.0/guide-security-passwords.html</a></p>