I am new to IONIC framework.Need to develop mobile application using IONIC 3 and firebase used to store data and for authentication. Installed all required modules through npm.
Followed below steps to create IONIC 3 app
1) ionic start TestDemo tabs
2) ionic generate page signup
3) npm install firebase angularfire2 --save
4) imported to app.module.ts
import { StatusBar } from '#ionic-native/status-bar';
import { SplashScreen } from '#ionic-native/splash-screen';
import { AngularFireModule } from 'angularfire2';
import { AngularFireAuthModule } from 'angularfire2/auth';
import { environment } from '../environments/environment';
#NgModule({
declarations: [
WelcomePage,
LoginPage,
SignupPage,
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
AngularFireModule.initializeApp(environment.firebase),
AngularFireAuthModule
]
5) signup.ts
import { Component, ViewChild } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { AngularFireAuth } from 'angularfire2/auth';
#IonicPage()
#Component({
selector: 'page-signup',
templateUrl: 'signup.html',
})
export class SignupPage {
#ViewChild("email") email;
#ViewChild("mobileno") mobileno;
#ViewChild("username") username;
#ViewChild("password") password;
constructor(public navCtrl: NavController, public navParams: NavParams,private fire: AngularFireAuth ) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad SignupPage');
}
async signup(){
try{
const result = await this.fire.auth.createUserWithEmailAndPassword(this.username.value,this.password.value);
console.log(result);
}
catch(err){
console.log("Error");
}
}
}
6) signup.html
<ion-header>
<ion-navbar>
<ion-title>signup</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item>
<ion-label floating>Email</ion-label>
<ion-input type="text" #email></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Mobile No</ion-label>
<ion-input type="text" #mobileno></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Username</ion-label>
<ion-input type="text" #username></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Password</ion-label>
<ion-input type="password" #password></ion-input>
</ion-item>
<button ion-button round (click)="signup()">Sign Up</button>
</ion-list>
</ion-content>
7) ionic serve
then it giving below error
Error: Cannot find module "#firebase/auth"
at webpackMissingModule (http://localhost:8100/build/vendor.js:66020:65)
at Object.__webpack_exports__.c (http://localhost:8100/build/vendor.js:66020:156)
at __webpack_require__ (http://localhost:8100/build/vendor.js:55:30)
at Object.<anonymous> (http://localhost:8100/build/vendor.js:65949:72)
at __webpack_require__ (http://localhost:8100/build/vendor.js:55:30)
at Object.<anonymous> (http://localhost:8100/build/vendor.js:112662:64)
at __webpack_require__ (http://localhost:8100/build/vendor.js:55:30)
at Object.<anonymous> (http://localhost:8100/build/vendor.js:65937:70)
at __webpack_require__ (http://localhost:8100/build/vendor.js:55:30)
at Object.101 (http://localhost:8100/build/main.js:76:76)
Please help on this.
Thanks in advance
Related
## login.page.ts ##
import { Component, OnInit } from '#angular/core';
import { AuthService } from 'src/services/auth/auth.service';
import { NavController } from '#ionic/angular';
import { AlertService } from 'src/services/alert/alert.service';
import { NgForm } from '#angular/forms';
#Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss']
})
export class LoginPage implements OnInit {
constructor(
private authService: AuthService,
private navCtrl: NavController,
private alertService: AlertService
) { }
ngOnInit() {
}
onLogin(form: NgForm) {
this.authService.login(form.value.username, form.value.password).subscribe(
() => {
this.alertService.presentToast("Logged In");
this.navCtrl.navigateRoot('/welcome');
},
error => {
console.log(error);
},
() => {
this.alertService.presentToast("Failed");
this.navCtrl.navigateRoot('/login');
}
);
}
}
## login.page.scss ##
ion-content.background{
--background: url(../../assets/img/staffandtruck.jpg) 0 0/100% 100% no-repeat;
}
## login.page.html ##
<ion-content padding class="background">
<ion-card>
<img src="assets/img/logo2.png"/>
</ion-card>
<ion-card>
<ion-card-content>
<form #form="ngForm" (ngSubmit)="onLogin(form)">
<ion-item>
<ion-icon slot="start" name="person-circle"></ion-icon>
<ion-label position="floating">Staff ID</ion-label>
<ion-input type="email" name="username" ngModel required></ion-input>
</ion-item>
<ion-item>
<ion-icon slot="start" name="key"></ion-icon>
<ion-label position="floating">Password</ion-label>
<ion-input type="password" name="password" ngModel required></ion-input>
</ion-item>
<ion-card-content>
<ion-button expand="block" color="primary" type="submit">Log in</ion-button>
</ion-card-content>
</form>
</ion-card-content>
</ion-card>
</ion-content>
i am new here and beginner in Ionic Framework. Is there anyone can help me and teach me on how to create a login using API that call username and password without create a Model for User? I don't know how to startup and implement this API. This is my environment setup for Ionic.
My environment setup for Ionic. This is for environment service that calling API for login EnvService.ts. This is for auth service AuthService.ts. But, that all not functioning as I wish.
I'm new in Ionic Framework, and I'm using version 3 to build a simple application that redirects the user to an .html page after logging in. I've edited the home.ts file to make the log in possible, and I've built a test .html called index.html, in the folder "pages/home" inside the ionic project, but I'm getting an error when I try to conned the "submit" button to this index.html page:
Code in home.html
<ion-header>
<ion-navbar>
<ion-title>
Login
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list no-lines>
<ion-item>
<ion-label floating>User</ion-label>
<ion-input type="text" [(ngModel)]="username"></ion-input>
</ion-item>
<ion-item>
<ion-label floating>Senha</ion-label>
<ion-input type="text" [(ngModel)]="password"></ion-input>
</ion-item>
<button block ion-button block (click)="signIn()">Sign in</button>
</ion-list>
</ion-content>
Code in home.ts
import { Component } from '#angular/core';
import { NavController, AlertController, IonicPage } from 'ionic-angular';
import { index } from 'c:/Ionic/task-1/src/pages/home/index.html';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public username : any = '';
public password : any = '';
constructor(public navCtrl: NavController, public alertCtrl: AlertController)
{
}
signIn() {
this.navCtrl.push('index.html');
}
}
The code in index.html
<ion-header>
<ion-navbar>
<ion-title>
Login
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<p>Testing HTML page redirect</p>
</ion-list>
</ion-content>
The error: https://i.stack.imgur.com/vRivh.png
What could I do?
In ionic 3 you can navigate through pages using NavController.You should create a component that you want to route.
For that you have to import the component(page)
HTML file
<button block ion-button block (click)="signIn()">Sign in</button>
TS file
import { NavController } from 'ionic-angular';
import { StartPage } from './start-page';
#Component(
selector: 'app-home',
templateUrl: 'home.html'
})
class HomePage {
constructor(public navCtrl: NavController){}
signIn() {
this.navCtrl.push(StartPage);
}
}
Refer NavController documentation in ionic
check here
In home.ts file :
import { Component } from '#angular/core';
import { NavController, AlertController, IonicPage } from 'ionic-angular';
import { IndexPage} from '../index/index';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public username : any = '';
public password : any = '';
constructor(public navCtrl: NavController, public alertCtrl: AlertController){}
signIn() {
this.navCtrl.push(IndexPage);
}
}
I have install 'angular2-signaturepad' in my ionic project, and I created a test app and that's working fine, But when I try to integrate with my existing project shows me 'signature-pad' is not a known element' and '
<ion-content padding>
<signature-pad [ERROR ->][options]="signaturePadOptions"
(onBeginEvent)="drawStart()" (onEndEvent)="drawComplete()"></signatur"):
ng:///SignPageModule/SignPage.html#10:16
'signature-pad' is not a known element:
1. If 'signature-pad' is an Angular component, then verify that it is part of this module.
2. If 'signature-pad' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '#NgModule.schemas' of this component to suppress this message. ("
<ion-content padding>
[ERROR ->]<signature-pad [options]="signaturePadOptions" (onBeginEvent)="drawStart()" (onEndEvent)="drawComplet"): ng:///SignPageModule/SignPage.html#10:1
Error: Template parse errors:
Can't bind to 'options' since it isn't a known property of 'signature-pad'.
1. If 'signature-pad' is an Angular component and it has 'options' input, then verify that it is part of this module.
2. If 'signature-pad' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '#NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '#NgModule.schemas' of this component
This is the error I m facing. I was added 'CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA' in my app.module.ts file.
This is my sign.html code:
<ion-header>
<ion-navbar>
<ion-title>sign</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-row [ngClass]="{'drawing-active': isDrawing}">
<ion-col></ion-col>
<ion-col style="border: 1px solid #5d6691;">
<signature-pad [options]="signaturePadOptions" (onBeginEvent)="drawStart()" (onEndEvent)="drawComplete()" style="width: 50px; height: 580px; color: red;"> </signature-pad>
</ion-col>
<ion-col></ion-col>
</ion-row>
<ion-row>
<ion-col>
<button ion-button style="color: #fff; background: #e59c9c;" (click)="clearPad()">Clear</button>
</ion-col>
<ion-col>
<button ion-button style="color: #fff; background: #4355a5;" (click)="savePad()">Save</button>
</ion-col>
<ion-col>
<button ion-button style="color: #fff; background: #e59c9c;" (click)="delPad()">delete</button>
</ion-col>
</ion-row>
<ion-row text-center>
<ion-col width-120>
<img [src]="signature" [hidden]= "signature == '' || signature == undefined " style="height: 200px;" />
</ion-col>
</ion-row>
This is my sign.ts code:
import { Component, ViewChild } from '#angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';
import { SignaturePad } from 'angular2-signaturepad/signature-pad';
import { Storage } from '#ionic/storage';
#IonicPage()
#Component({
selector: 'page-sign',
templateUrl: 'sign.html',
})
export class SignPage {
public signature = '';
isDrawing = false;
#ViewChild(SignaturePad) signaturePad: SignaturePad;
private signaturePadOptions: Object = {
'minWidth': 1,
'canvasWidth': 320,
'canvasHeight': 200,
'backgroundColor': '#f6fbff',
'penColor': '#06219b'
};
constructor(public navCtrl: NavController,
public navParams: NavParams,
public storage: Storage,
public toastCtrl: ToastController ) {
}
ionViewDidLoad() {
this.signaturePad.clear()
this.storage.get('savedSignature').then((data) => {
this.signature = data;
});
}
drawComplete() {
this.isDrawing = false;
}
drawStart() {
this.isDrawing = true;
}
savePad() {
this.signature = this.signaturePad.toDataURL();
this.storage.set('savedSignature', this.signature);
this.signaturePad.clear();
let toast = this.toastCtrl.create({
message: 'New Signature saved.',
cssClass: 'homeToast',
duration: 3000,
position: 'top'
});
toast.present();
}
clearPad() {
this.signaturePad.clear();
}
delPad(){
this.storage.clear();
this.signature = '';
}
}
This is my app.module.ts code:
import { BrowserModule } from '#angular/platform-browser';
import { ErrorHandler, NgModule, CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '#angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '#ionic-native/splash-screen';
import { StatusBar } from '#ionic-native/status-bar';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { SignaturePadModule } from 'angular2-signaturepad';
import { IonicStorageModule } from '#ionic/storage';
#NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp,{
backButtonText: '',
autoFocusAssist: true }),
SignaturePadModule,
IonicStorageModule.forRoot()
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA ]
})
export class AppModule {}
I expected signature-pad to be working fine
Make sure to also import the module in sign.module.ts
import { SignaturePadModule } from 'angular2-signaturepad';
#NgModule({
imports: [
...
SignaturePadModule
],
declarations: [SignPage]
})
export class SignPageModule {}
I'm learning Ionic and stuck on an issue. My app is stuck in a loop in which it is repeatedly fetching information from a remote server and causing the browser and crash.
I setup a service, code below, to retrieve the data:
import { HttpClient } from '#angular/common/http';
import { Injectable } from '#angular/core';
/*
Generated class for the CategoriesServiceProvider provider.
See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
#Injectable()
export class CategoriesServiceProvider {
apiURL = 'https://reqres.in/api/users';
constructor(public http: HttpClient) {
console.log('Hello CategoriesServiceProvider Provider');
}
getUsers(){
return new Promise(resolve => {
this.http.get(this.apiURL).subscribe(data => {
resolve(data);
// console.log(data);
}, err => {
console.error(err);
});
});
}
}
Below is the code in my categories.ts file which uses the service above:
import { Component } from '#angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { CategoriesServiceProvider } from '../../providers/categories-service/categories-service';
/**
* Generated class for the CategoriesPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
#IonicPage()
#Component({
selector: 'page-categories',
templateUrl: 'categories.html',
providers: [CategoriesServiceProvider]
})
export class CategoriesPage {
constructor(public navCtrl: NavController, public navParams: NavParams, private categories: CategoriesServiceProvider) {
}
users: any;
getUsers(){
this.categories.getUsers().then(data => {
this.users = data;
console.log(this.users);
});
}
ionViewDidLoad() {
console.log('ionViewDidLoad CategoriesPage');
this.getUsers();
}
}
... followed by categories.html:
<ion-header>
<ion-navbar>
<ion-title>categories</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item *ngFor="let user of getUsers()">
TESTING - {{user.gender}}
</ion-item>
</ion-list>
</ion-content>
I don't know why the http request is repeating instead of fetching once and then going away.
here you are looping on the function to get your data:
<ion-content padding>
<ion-list>
<ion-item *ngFor="let user of getUsers()">
TESTING - {{user.gender}}
</ion-item>
</ion-list>
</ion-content>
Replace it with the retrieved data instead!
Something like :
<ion-content padding>
<ion-list>
<ion-item *ngFor="let user of this.users">
TESTING - {{user.gender}}
</ion-item>
</ion-list>
</ion-content>
Using Ionic4 for the first time and struggling with ngSubmit not triggering the respective method in the login page. Although its always successfully hitting the LoginPage constructor and AuthService constructor. All the respective modules have been imported and there are no console errors too. What am i doing wrong ?
login.page.html:
<ion-header>
<ion-toolbar>
<ion-title>
Login
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<form (ngSubmit)="login()" [formGroup]="loginForm">
<ion-grid>
<ion-row>
<ion-col>
<ion-item>
<ion-input type="text" placeholder="Email" formControlName="email" style="width:50%;"></ion-input>
</ion-item>
</ion-col>
</ion-row>
<ion-row>
<ion-col col-3>
<ion-item>
<ion-input type="password" placeholder="Password" formControlName="password" style="width:50%;"></ion-input>
</ion-item>
</ion-col>
</ion-row>
</ion-grid>
<div padding-horizontal>
<div class="form-error">{{loginError}}</div>
</div>
<ion-grid>
<ion-row>
<ion-col>
<ion-button type="submit" [disabled]="!loginForm.valid" color="primary">Log in</ion-button>
Forgot password?
</ion-col>
</ion-row>
<ion-row>
<ion-col>
<ion-button icon-left block clear (click)="loginWithGoogle()" color="secondary">
<ion-icon name="logo-google"></ion-icon>
Log in with Google
</ion-button>
<ion-button icon-left block clear (click)="signup()" color="primary">
<ion-icon name="person-add"></ion-icon>
Sign up
</ion-button>
</ion-col>
</ion-row>
</ion-grid>
</form>
</ion-content>
login.page.ts:
import { Component, OnInit } from '#angular/core';
import { AuthService } from '../common/services/auth.service';
import { FormGroup, FormBuilder, Validators } from '#angular/forms';
import { Router } from '#angular/router';
#Component({
selector: 'app-login',
templateUrl: './login.page.html',
styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
loginForm: FormGroup;
loginError: string;
constructor(private auth:AuthService,
private router:Router,
private fb: FormBuilder)
{
this.loginForm = this.fb.group({
email: ['', Validators.compose([Validators.required, Validators.email])],
password: ['', Validators.compose([Validators.required, Validators.minLength(6)])]
});
}
ngOnInit() {}
login() {
alert('login');
let data = this.loginForm.value;
if (!data.email) {
return;
}
let credentials = {
email: data.email,
password: data.password
};
this.auth.signInWithEmail(credentials)
.then(
() => this.router.navigate(['/home']),
error => this.loginError = error.message
);
}
}
login.module.ts:
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { FormsModule,ReactiveFormsModule } from '#angular/forms';
import { Routes, RouterModule } from '#angular/router';
import { IonicModule } from '#ionic/angular';
import { LoginPage } from './login.page';
const routes: Routes = [
{
path: '',
component: LoginPage
}
];
#NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
IonicModule,
RouterModule.forChild(routes),
],
entryComponents: [LoginPage],
declarations: [LoginPage]
})
export class LoginPageModule {}
auth.service.ts:
import { Injectable } from '#angular/core';
import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
import AuthProvider = firebase.auth.AuthProvider;
#Injectable({
providedIn: 'root'
})
export class AuthService {
private user: firebase.User;
constructor(public afAuth: AngularFireAuth) {
afAuth.authState.subscribe(user => {
this.user = user;
});
}
signInWithEmail(credentials) {
console.log('Sign in with email');
return this.afAuth.auth.signInWithEmailAndPassword(credentials.email,
credentials.password);
}
}
ngSubmit is reloading the complete page and hence its hitting only login constructor but not the respective method. Removed (ngSubmit) on <form>, type="submit" on <ion-button>, added (click)="login()" event to <ion-button>. It started working.