Пример #1
1
 public function registerAction()
 {
     $email = $this->post_data['email'];
     $password = $this->post_data['password'];
     if ($email == NULL || $password == NULL) {
         return $this->fail('Email and password not empty', Response::HTTP_BAD_REQUEST);
     }
     $user = User::where('email', '=', $email)->first();
     if ($user) {
         return $this->fail('Email already exists ', Response::HTTP_BAD_REQUEST);
     } else {
         $user = new User();
         $user->username = $this->post_data['username'];
         $user->password = md5($this->post_data['password']);
         $user->email = $this->post_data['email'];
         $user->status = 1;
         $user->save();
         if ($user->password == md5($password) && $user->status == 1) {
             $secret = $this->app['config.app.secret'];
             $token = array('user_id' => $user->id);
             $encoded_token = JWT::encode($token, $secret, 'HS256');
             return $this->json(['mess' => 'Thành công', 'user_id' => $user->id, 'token' => $encoded_token]);
         } else {
             return $this->fail('Database error', Response::HTTP_BAD_REQUEST);
         }
     }
 }
Пример #2
0
 public function checkLogin($email, $password)
 {
     $user = User::where('username', '=', $username)->first();
     if ($user && $user->password == md5($password) && $user->status == 1) {
         return $user;
     }
     return false;
 }
Пример #3
0
 public function __construct()
 {
     parent::__construct();
     $authorization_header = $this->request->headers->get('Authorization');
     if (!preg_match('/^Bearer (.+)$/i', $authorization_header, $matches)) {
         $this->response->setStatusCode(Response::HTTP_UNAUTHORIZED);
         $this->response->send();
         return $this->app->terminate($this->request, $this->response);
     }
     $token = $matches[1];
     $secret = $this->app['config.app.secret'];
     $decoded_token = JWT::decode($token, $secret, ['HS256']);
     $this->authed_token = $decoded_token;
     $this->authed = User::findOrFail($decoded_token->user_id);
 }