<?php
namespace app\controllers;
use app\models\User;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\base\Security;
class UsersController extends \yii\web\Controller
{
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionLogin()
    {
        return $this->render('login');
    }

    public function actionRegister()
    {
        $user = new User();
        if ($user->load(Yii::$app->request->post())) {
            if ($user->validate()) {
               $user->password = sha1($user->password);
               $user->token =  \Yii::$app->security->generateRandomString();
               $user->save();
               \Yii::$app->getSession()->setFlash('success', 'Compte crée.');
               return $this->redirect('index.php');
            }
        }
        return $this->render('register', [
            'user' => $user,
        ]);
    }

}
