0
Good morning, I would like a Login (Authentication) guidance using Json-Server (FAKE API). Json-Server supports this feature?
I was following some lessons in Alura about Ionic and Angular, and they use the POST method to log in, but when trying to do this in my project using Json-Server, it adds the data in the API, being a valid or invalid login!
I will post my codes below to try to make clear my procedures.
My Json-Server in your file db.json contains the following data:
"usuarios": [
{
"id": 1,
"nome": "Dennis",
"sobrenome": "Cezar",
"endereco": "Endereço Completo",
"telefone": "XXXXX-XXXX",
"email": "[email protected]",
"senha": "123",
"acesso": 1
},
{
"id": 2,
"nome": "Marcos",
"sobrenome": "Gonçalves",
"endereco": "Endereço Completo",
"telefone": "XXXXX-XXXX",
"email": "[email protected]",
"senha": "123",
"acesso": 2
}
]
In my component Usuarioservice.TS of the Ionic is as follows:
API: string = "http://localhost:3000/usuarios/";
constructor(private http: HttpClient) { }
getLogin(email: string, senha: string) {
return this.http.post(this.API, {email: email, senha: senha});
}
In my component Loginpage.HTML is as follows:
<form [formGroup]="loginForm">
<ion-list>
<ion-item>
<ion-label position="floating">E-mail</ion-label>
<ion-input type="text" formControlName="email" name="email"></ion-input>
</ion-item>
<p *ngIf="loginForm.get('email').errors?.required" class="textoRequired">E-mail é obrigatório!</p>
<ion-item>
<ion-label position="floating">Senha</ion-label>
<ion-input type="password" formControlName="senha" name="senha"></ion-input>
</ion-item>
<p *ngIf="loginForm.get('senha').errors?.required" class="textoRequired">Senha é obrigatória!</p>
</ion-list>
<div>
<ion-button [disabled]="loginForm.invalid" class="ion-padding" expand="block" color="tertiary" (click)="loginUsuario()">Entrar</ion-button>
</div></form>
Finally, in my component Loginpage.TS is as follows:
loginForm: FormGroup;
constructor(private router: Router,
private formBuilder: FormBuilder,
private usuarioService: UsuarioService,
public toastController: ToastController) { }
ngOnInit() {
this.loginForm = this.formBuilder.group({
email: ['', Validators.required],
senha: ['', Validators.required]
});
}
loginUsuario() {
const email = this.loginForm.get('email').value;
const senha = this.loginForm.get('senha').value;
this.usuarioService.getLogin(email, senha)
.subscribe(() => {
console.log("Logou com sucesso!");
}, erro => {
console.log(erro);
this.loginForm.reset();
});
}
I hope this information helps to clarify my procedures.
The form picks up the data correctly, but instead of checking on the Json-Server if the user exists or not, it creates a new Object with the ID, Email and Password data I entered in the Login fields.
Is that Json-Server is not prepared for this, I am doing something incorrectly?
I would like, if possible, some guidance!
Thanks in advance.
So Dennis, you probably haven’t seen all the classes, the method Post will create new users in the Api and not authenticate, this authentication work is totally part, laborious and quite broad. Maybe in the next classes the subject is addressed or not, I do not know the course. I do not know if with the
json-server
can to perform authentication, the most correct is to ask on the platform that is attending the classes directly to the teacher.– LeAndrade
Thanks Leandrade! So, the curious thing is that in the classes the teacher uses the post method to make the Login request, so I was very confused. I asked a question there about it and they could not inform me. But anyway, I will continue researching and trying to understand! Thanks!
– Dennis Cezar