角登录帖子功能不捕获请求,但与邮递员一起很好地工作

时间:2019-08-19 11:32:19

标签: php angular authentication jwt

即使凭据电子邮件和密码正确,index_post方法的无效条件也只能起作用。根据我的调查,我认为在角度api.Services.ts loginUser()中没有将对象'us​​er'正确传递给'post'方法。我解决了吗?

通过api调用调用Users.Php:Index_post()

    <?php

    defined('BASEPATH') OR exit('No direct script access allowed');
    require APPPATH . 'libraries/REST_Controller.php';
    require APPPATH . 'libraries/Format.php';
    class Users extends REST_Controller{
    public function __construct(){

    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method");
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
    $method = $_SERVER['REQUEST_METHOD'];
    if($method == "OPTIONS") {
        die();
    }
}
 public function index_post(){

    $users=new UsersModel;
    $email = $this->input->post('email');
    $password = $this->input->post('password');
    $data['user']=$users->checkLogin($email,$password);

    if(!empty($data['user'])){
        //Create  a token 
         $token = AUTHORIZATION ::generateToken($email,$password);
        $this->response([$token,$data], REST_Controller::HTTP_OK);

    }
    else{
        //Even when the credentials are correct only this line works
        $this->response(['User Invalid'], REST_Controller::HTTP_OK);

    }
} 
}
?>

UsersModel.php:db和控制器之间进行通信的地方。函数名为:checkLogin

<?php
class UsersModel extends CI_Model{
public function checkLogin($username,$password){

    $this->db->select("*");
    $this->db->from("idl_users");
    $this->db->where("email",$username);
    $this->db->where("password",$password);
    if($query = $this->db->get()){
        return $query->row_array();
    }
    else{
        return false;
    }
}

login.components.ts:角度登录类型脚本,其中从apiServices.ts调用loginUser()函数

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from  '@angular/forms';
import { ApiService } from '../api.service';

@Component({selector: 'app-login',templateUrl: './login.component.html',styleUrls: ['./login.component.css']
})

export class LoginComponent implements OnInit {
 loginForm: FormGroup;
submitted = false;


loginUserData = {}
//console.log(loginUserData)
constructor(private apiService: ApiService) { }

ngOnInit() { 
}

loginUser(){

  this.apiService.loginUser(this.loginUserData)
  .subscribe(
    res => console.log(res),
    err => console.log(err)
  )
}

}

api.Services.ts:实际的post函数与参数username和password一起通过'user'参数传递

import { Injectable } from '@angular/core';
import { HttpClient ,HttpHeaders} from '@angular/common/http';
import { User } from  './user';
import { Observable } from  'rxjs';

 @Injectable({ providedIn: 'root'
})
export class ApiService {

constructor(private httpClient: HttpClient) {}
PHP_API_SERVER = "http://localhosthrms/api/users";

//Read using get method from table users
readUsers(): Observable<User[]>{
return this.httpClient.get<User[]>(`${this.PHP_API_SERVER}/api/users`);
}

loginUser(user){
console.log(user);
return this.httpClient.post<any>(this.PHP_API_SERVER,(user))
}


}

即使凭据电子邮件和密码正确,index_post方法的无效条件也仅适用。 有人可以帮忙吗?谢谢。

0 个答案:

没有答案