I have developed my api rest in symfony, and do some tests with postman, everything is ok, now i am working on the front part with ionic for the mobile application, and for the authentication part, i would like to know how to use my url login of my api rest. when I test my login form, there is an error in the console that shows me
POST http://localhost:8100/api/login_check 404 (Not Found)
Authentication Controller
/**
* @Rest\View(statusCode=Response::HTTP_CREATED, serializerGroups={"auth-token"})
* @Rest\Post("/auth-tokens")
*/
public function postAuthTokensAction(Request $request)
{
$credentials = new Credentials();
$form = $this->createForm(CredentialsType::class, $credentials);
$form->submit($request->request->all());
if (!$form->isValid()) {
return $form;
}
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository("DoctixUserBundle:User")
->findOneByUsername($credentials->getLogin());
if (!$user) { // L'utilisateur n'existe pas
return $this->invalidCredentials();
}
$encoder = $this->get('security.password_encoder');
$isPasswordValid = $encoder->isPasswordValid($user, $credentials->getPassword());
if (!$isPasswordValid) { // Le mot de passe n'est pas correct
return $this->invalidCredentials();
}
$authToken = new AuthToken();
$authToken->setValue(base64_encode(random_bytes(50)));
$authToken->setCreatedAt(new \DateTime('now'));
$authToken->setUser($user);
$em->persist($authToken);
$em->flush();
return $authToken;
}
private function invalidCredentials()
{
return \FOS\RestBundle\View\View::create(['message' => 'Invalid credentials'], Response::HTTP_BAD_REQUEST);
}
Ionic Html page
<ion-app>
<ion-header translucent>
<ion-toolbar>
<ion-title>Se Connecter</ion-title>
</ion-toolbar>
</ion-header>
<ion-content fullscreen>
<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
<ion-list lines="full" class="ion-no-margin ion-no-padding">
<ion-item>
<ion-label position="stacked">Nom d'utilisateur <ion-text color="danger">*</ion-text></ion-label>
<ion-input required type="text" class="form-control" placeholder="Username*" formControlName="username" ></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Mot de passe <ion-text color="danger">*</ion-text></ion-label>
<ion-input required type="password" class="form-control" placeholder="Password*" formControlName="password"></ion-input>
</ion-item>
</ion-list>
<div class="ion-padding">
<ion-button size="small" type="submit" class="ion-no-margin" [disabled]="!loginForm.valid">Se Connecter</ion-button>
<ion-button size="small" routerLink="/register" routerDirection="forward" class="ion-no-margin">S'inscrire</ion-button>
</div>
</form>
</ion-content>
</ion-app>
Ionic Script controller
import { Component } from '@angular/core';
import { FormBuilder, FormsModule, ReactiveFormsModule, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import {AuthenticationService} from '../authentication/authentication.service';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
loginForm: FormGroup;
error = '';
constructor(
private formBuilder: FormBuilder,
private authenticationService: AuthenticationService,
private router: Router
) {
this.loginForm = formBuilder.group({
username: ['', Validators.required],
password: ['', Validators.required]
});
}
onSubmit() {
this.authenticationService
.authenticate(this.loginForm.value)
.subscribe(
data => {
localStorage.setItem('id_token', data.token);
this.router.navigate(['post']);
},
error => this.error = error.message
);
}
}
My Nelmio Cors config
nelmio_cors:
defaults:
allow_credentials: false
allow_origin: []
allow_headers: []
allow_methods: []
expose_headers: []
max_age: 0
hosts: []
origin_regex: false
forced_allow_origin_value: ~
paths:
'^/api/':
allow_origin: ['http://localhost:8100']
allow_headers: ['origin', 'content-type', 'authorization']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
max_age: 3600
'^/':
origin_regex: false
allow_origin: ['*']
allow_headers: ['origin', 'content-type', 'authorization']
allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
max_age: 3600
hosts: ['^api\.']
Routing
This is the routing of the api/login_check in app/config/routing.yml
api_login_check:
path: /api/login_check
methods: [POST, OPTIONS]
Routing for login controller
api_login:
path: /api/login
defaults: { _controller: "DoctixUserBundle:API\\ApiSecurity:postLogin", _format:json }
authentification:
path: /api/auth-tokens
defaults: { _controller: "DoctixUserBundle:API\\ApiSecurity:postAuthTokens", _format:json }
Authentication Service
export class AuthenticationService {
constructor(private http: Http) {}
public apiBase = 'http://127.0.0.1:8000/';
authenticate(user: any) {
// const url = this.apiBase + 'api/login';
const body = new URLSearchParams();
body.append('username', user.username);
body.append('password', user.password);
const headers = new Headers({'Content-Type': 'application/json'});
const options = new RequestOptions({headers});
return this.http.post(this.apiBase + 'api/login_check', body.toString(), options).map((data: Response) => data.json() );
/*.post(url, body.toString(), options)
.map((data: Response) => data.json());*/
} Thanks