Hi, I’m new to Ionic and I’m starting to develop an app, and for now I just need to implement a provisory register/login using SQLite.
The problem is that I am not able to log in after registering a user, because it seems that the .get (email: string)
function in UsuarioProvider
is not returning the Usuario
object. My only return is the “User not found” toast.
I would appreciate if someone could indicate possible causes, since I do not have much experience with the framework
usuarios.ts
import { Injectable } from '@angular/core';
import { SQLiteObject } from '@ionic-native/sqlite';
import { DatabaseProvider } from '../database/database';
@Injectable()
export class UsuariosProvider {
constructor(private dbProvider: DatabaseProvider) { }
public insert(usuario: Usuario) {
return this.dbProvider.getDB()
.then((db: SQLiteObject) => {
let sql = 'insert into usuarios (nome, email, senha) values (?, ?, ?)';
let data = [usuario.nome, usuario.email, usuario.senha];
return db.executeSql(sql, data)
.catch((e) => console.error(e));
})
.catch((e) => console.error(e));
}
public update(usuario: Usuario) {
return this.dbProvider.getDB()
.then((db: SQLiteObject) => {
let sql = 'update usuarios set nome = ?, email = ?, senha = ? where id = ?';
let data = [usuario.nome, usuario.email, usuario.senha, usuario.id];
return db.executeSql(sql, data)
.catch((e) => console.error(e));
})
.catch((e) => console.error(e));
}
public remove(id: number) {
return this.dbProvider.getDB()
.then((db: SQLiteObject) => {
let sql = 'delete from usuarios where id = ?';
let data = [id];
return db.executeSql(sql, data)
.catch((e) => console.error(e));
})
.catch((e) => console.error(e));
}
public get(email: string) {
return this.dbProvider.getDB()
.then((db: SQLiteObject) => {
let sql = 'select * from usuarios where email = ?';
let data = [email];
return db.executeSql(sql, data)
.then((data: any) => {
if (data.rows.length > 0) {
let item = data.rows.item(0);
let usuario = new Usuario();
usuario.id = item.id;
usuario.nome = item.nome;
usuario.senha = item.senha;
usuario.email = item.email;
return usuario;
}
return null;
})
.catch((e) => console.error(e));
})
.catch((e) => console.error(e));
}
}
export class Usuario {
id: number;
nome: string;
email: string;
senha: string;
}
database.ts
import { Injectable } from '@angular/core';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';
@Injectable()
export class DatabaseProvider {
constructor(private sqlite: SQLite) { }
public getDB() {
return this.sqlite.create({
name: 'database.db',
location: 'default'
});
}
public createDatabase() {
return this.getDB()
.then((db: SQLiteObject) => {
this.createTables(db);
})
.catch(e => console.log(e));
}
private createTables(db: SQLiteObject) {
db.sqlBatch([
['CREATE TABLE IF NOT EXISTS usuarios (id integer primary key AUTOINCREMENT NOT NULL, nome TEXT, email TEXT, senha TEXT)'],
['CREATE TABLE IF NOT EXISTS atividades (id integer primary key AUTOINCREMENT NOT NULL, titulo TEXT, data DATE, hora TEXT']
])
.then(() => console.log('Tables created'))
.catch(e => console.error('Error creating tables', e));
}
}
login.ts
import { Component } from '@angular/core';
import { NavController, ToastController } from 'ionic-angular';
import { CadastroPage } from '../cadastro/cadastro';
import { PrincipalPage } from '../principal/principal';
import { UsuariosProvider, Usuario } from '../../providers/usuarios/usuarios'
@Component({
selector: 'page-login',
templateUrl: 'login.html'
})
export class LoginPage {
usuario: Usuario;
constructor(public navCtrl: NavController, private toast:
ToastController, private usuarioProvider: UsuariosProvider) {
this.usuario = new Usuario();
}
private login() {
this.usuarioProvider.get(this.usuario.email)
.then((result: Usuario) => {
if (this.usuario.senha==result.senha && this.usuario.senha!="") {
this.goToPrincipal();
} else {
this.toast.create({ message: 'Wrong password! Try again', duration: 3000, position: 'bottom' }).present();
}
})
.catch(() => {
this.toast.create({ message: 'User not found!', duration: 3000, position: 'bottom' }).present();
})
}
public goToPrincipal(){
this.navCtrl.setRoot(PrincipalPage);
}
public goToCadastro(){
this.navCtrl.push(CadastroPage);
}
}
login.html
<ion-content padding>
<div text-center>
<img margin-top="5%" src="assets/imgs/logo-placeholder.png">
<br>
<button class="btnLoginGoogle"> <ion-icon name="logo-google" size=large margin-right=auto></ion-icon>
FAZER LOGIN COM O GOOGLE </button>
<ion-label class="labelLogin"> OU </ion-label>
<ion-list>
<ion-item>
<ion-input [(ngModel)]="usuario.email" margin-top="15px" class="txtFieldLogin" type="email" placeholder="E-mail"></ion-input>
</ion-item>
<ion-item>
<ion-input [(ngModel)]="usuario.senha" class="txtFieldLogin" type="password" placeholder="Senha">
</ion-input>
</ion-item>
<ion-label [hidden]="true"></ion-label>
</ion-list>
<button (click)="login()" class="btnEntrar">ENTRAR</button>
<button (click)="goToCadastro();" class="btnCadastrar"> AINDA NÃO POSSUI UMA CONTA?</button>
</div>
</ion-content>