Try to search your question here, if you can't find : Ask Any Question Now ?

Angular read HTTP header status

HomeCategory: stackoverflowAngular read HTTP header status
Avatarbhawya asked 3 months ago

Hye guys.

Here i want to make a login page where i need to validate the student id and password to be matched from the database. I have the back-end API that sends validate the student account and sends response status when they are success or failed.

Here are my LoginComponent.ts

export class LoginComponent {

 studentForm: FormGroup;

constructor(
private fb: FormBuilder,
private crudService: CrudService,
private router: Router) {
  this.studentForm = this.fb.group({
    id: ['', Validators.compose([Validators.required])],
    password: ['', Validators.compose([Validators.required])]
  });
}
// This are the example for inserting data into database
saveStudentDetails(values) {
  const studentData = new FormData();

  studentData.append('id', values.id);
  studentData.append('password', values.password);
  this.crudService.loginstudent(studentData).subscribe(result => {
    if (this.crudService.getData) {
    this.router.navigate(['address']); alert  ('"success"'); return; }
  });
}

}

// MY service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

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

// Base api url
public url = 'http://localhost:8080/';
headerProperty: string;

constructor(private http: HttpClient) { }

createUser(data) {
  return this.http.post(this.url + 'todo', data);
}

createAddress(data) {
  return this.http.post(this.url + 'address', data);
}

}

Ignore the saveStudentDetails, it just for testing purpose and my question is how can i make a validation base on response status from api and make it go to next page if the api send a success status. Thank you

// This is my backend api using slim framework

$app->post('/login', function (Request $request, Response $response, array $args) {

$input = $request->getParsedBody();
$sql = "SELECT * FROM users WHERE id= :id";
$sth = $this->db->prepare($sql);
$sth->bindParam("id", $input['id']);
$sth->execute();
$user = $sth->fetchObject();

// verify id.
if(!$user) {
    return $this->response->withJson(['error' => true, 'message' => 'No id found'], 404);  
}
// Compare the input password and the password from database for a validation
if (strcmp($input['password'],$user->password) == 1 ) {
    return $this->response->withJson(['error' => true, 'message' => 'These credentials do not match our records.'], 404);  
}

    return $this->response->withJson($input,202);

});

1 Answers
Best Answer
AvatarArben answered 3 months ago
Your Answer

13 + 15 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com