Hi Friends,
Hope you are all well. This is episode 2 of creating a chat app using Ionic 3 and Firebase series. In this post let’s see how to create a sign up provision for the user using which he/she could create a new account and make use of it.
Let’s begin.
A screencast of this post.
The code for this post is available here.
The complete code for this app is available here. (Please note that this is a work in progress and code will be committed to this repo in each part).
Let’s start from where we left off. Pull the previous code from my github repo available here.
Now go ahead and create a new page called signup using the below command.
1 |
ionic g page signup |
Let’s style this page and connect it to the login page.
Open up login.ts and add the below method.
1 2 3 |
signup() { this.navCtrl.push('SignupPage'); } |
Your login.ts should look like as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams } from 'ionic-angular'; import { usercreds } from '../../models/interfaces/usercreds'; import { AuthProvider } from '../../providers/auth/auth'; /** * Generated class for the LoginPage page. * * See http://ionicframework.com/docs/components/#navigation for more info * on Ionic pages and navigation. */ @IonicPage() @Component({ selector: 'page-login', templateUrl: 'login.html', }) export class LoginPage { credentials = {} as usercreds; constructor(public navCtrl: NavController, public navParams: NavParams, public authservice: AuthProvider) { } ionViewDidLoad() { console.log('ionViewDidLoad LoginPage'); } signin() { this.authservice.login(this.credentials).then((res: any) => { if (!res.code) this.navCtrl.setRoot('TabsPage'); else alert(res); }) } signup() { this.navCtrl.push('SignupPage'); } } |
Now that this is done let’s go ahead and copy the below css code into signup.scss
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
page-signup { .background { background-image: url('../assets/bgimage.jpg'); } .scroll-content { align-content: center; display: flex; flex-direction: column; justify-content: center; text-align: center; } ion-card.card { box-shadow: none; background: rgba(0, 0, 0, 0.5); border-radius: 5px; } a, p, ion-card-header.card-header { color: #fff!important; } .list > .item-block:first-child { border: medium none; } .item { margin-bottom: 10px; background: rgba(255, 255, 255, 0.5); border: medium none; .text-input, { color: #fff; } input::-moz-placeholder{ color: #fff!important; } input:-moz-placeholder { color: #fff!important; } *:-moz-placeholder{ color: #fff!important; } *:-ms-input-placeholder{ color: #fff!important; } *::-webkit-input-placeholder{ color: #fff!important; } } .icon { padding-right: 10px; } .bottom { bottom: 0; } .input-cover { position: static; } } |
This is the exact styling code that we used for login page as well. Open up signup.html file and add the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<!-- Generated template for the SignupPage page. See http://ionicframework.com/docs/components/#navigation for more info on Ionic pages and navigation. --> <ion-header> </ion-header> <ion-content class="background"> <ion-card> <ion-card-header> Signup </ion-card-header> <ion-card-content> <ion-list > <ion-item> <ion-input type="email" placeholder="Email" [(ngModel)]="newuser.email"></ion-input> </ion-item> <ion-item> <ion-input type="password" placeholder="Password" [(ngModel)]="newuser.password"></ion-input> </ion-item> <ion-item> <ion-input type="text" placeholder="Nick Name" [(ngModel)]="newuser.displayName"></ion-input> </ion-item> <button ion-button block round outline color="light" (click)="signup()">Sign Up</button> <button ion-button full clear color="light" (click)="goback()">Go Back</button> </ion-list> </ion-card-content> </ion-card> </ion-content> |
Few things to notice:
1) We have three fields namely email, password and nickname and two buttons.
2) The sign up button is used to add the user to the application, whereas the go back button is used to move the user back to the login page.
Now open up signup.ts file and add the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams, LoadingController, ToastController } from 'ionic-angular'; import { UserProvider } from '../../providers/user/user'; /** * Generated class for the SignupPage page. * * See http://ionicframework.com/docs/components/#navigation for more info * on Ionic pages and navigation. */ @IonicPage() @Component({ selector: 'page-signup', templateUrl: 'signup.html', }) export class SignupPage { newuser = { email: '', password: '', displayName: '' } constructor(public navCtrl: NavController, public navParams: NavParams, public userservice: UserProvider, public loadingCtrl: LoadingController, public toastCtrl: ToastController) { } signup() { var toaster = this.toastCtrl.create({ duration: 3000, position: 'bottom' }); if (this.newuser.email == '' || this.newuser.password == '' || this.newuser.displayName == '') { toaster.setMessage('All fields are required dude'); toaster.present(); } else if (this.newuser.password.length < 7) { toaster.setMessage('Password is not strong. Try giving more than six characters'); toaster.present(); } else { let loader = this.loadingCtrl.create({ content: 'Please wait' }); loader.present(); this.userservice.adduser(this.newuser).then((res: any) => { loader.dismiss(); if (res.success) this.navCtrl.push('ProfilepicPage'); else alert('Error' + res); }) } } goback() { this.navCtrl.setRoot('LoginPage'); } } |
Things to notice:
- We have two functions namely signup() and goback(). Go back does what it’s name suggests.
- Inside the signup() function we are simply performing some basic validation stuff and then calling the adduser method in the userservice to add the user.
- The loading controller is used to provide a loading animation while the rest call is made and the toast controller is used to display a toast in case of any validation errors.
Wait we haven’t created the userservice yet. Let’s go ahead and do that.
1 |
ionic g provider user |
Now get into providers/user/user.ts and add the below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import { Injectable } from '@angular/core'; import { AngularFireAuth } from 'angularfire2/auth'; import firebase from 'firebase'; /* Generated class for the UserProvider provider. See https://angular.io/docs/ts/latest/guide/dependency-injection.html for more info on providers and Angular 2 DI. */ @Injectable() export class UserProvider { firedata = firebase.database().ref('/chatusers'); constructor(public afireauth: AngularFireAuth) { } adduser(newuser) { var promise = new Promise((resolve, reject) => { this.afireauth.auth.createUserWithEmailAndPassword(newuser.email, newuser.password).then(() => { this.afireauth.auth.currentUser.updateProfile({ displayName: newuser.displayName, photoURL: '' }).then(() => { this.firedata.child(this.afireauth.auth.currentUser.uid).set({ uid: this.afireauth.auth.currentUser.uid, displayName: newuser.displayName, photoURL: 'give a dummy placeholder url here' }).then(() => { resolve({ success: true }); }).catch((err) => { reject(err); }) }).catch((err) => { reject(err); }) }).catch((err) => { reject(err); }) }) return promise; } } |
Let’s break this down.
We are using the createUserwithEmailAndPassword() in the angularfire2/auth library to create a new user.
Once the user is created then I am using the upadateProfile method in the same library to add the displayName for the user.
Once that is done then we are creating a child in the chatusers collection with the newly registered user’s uid as the key. While creating this child I am also adding the photoURL (a dummy placeholder url).
Please note that we are storing information about all the users in our chatusers collection. This is primarily because we won’t be able to get information about all the users from firebase. This is to avoid issues and provide an increased level of security.
Now that we have created an user, we have to be able to login successfully using this user.
1 2 3 4 5 6 7 |
this.userservice.adduser(this.newuser).then((res: any) => { loader.dismiss(); if (res.success) this.navCtrl.push('ProfilepicPage'); else alert('Error' + res); }) |
You could see that upon user creation we are redirecting the user to a specific page called profilepicpage.
Go ahead and create a page called Profile Pic using the below command.
1 |
ionic g page profilepic |
This page is used to choose a profile picture for your account.
We will be designing this provision in the next episode.
Let’s go ahead and run whatever we have written so far. If everything goes fine, you would see the screens as shown below.
In our post we will see how to choose a profile picture for the user from your phone’s filesystem.
Hope this helped you guys. If you found this helpful, kindly share it with someone else and help them too.
I would highly appreciate it if you could become my patron (costs a couple of bucks and you get early access to all my videos plus benefits). Click here if you interested. With the help of patrons I could make more content and release them free of charge.
Thanks for reading guys. Peace..