Part 2 - Sign In
In part 1 of our chat app, users are allowed to log in using their gmail user names and passwords. No credentials are stored on the fire store for that. With this sign in, we want to enable users to first register using an email and a password and then be able to sign in using their own specified email addresses and passwords.
To create this sign in screen, we have followed the
component approach to create our page.
Our login screen is sitting at the center of the screen and looks like this:
We initialize a VMComponent and specify its route path.
signin.Initialize(vue, "signin", "/signin", Me)
If we want to navigate to it anywhere in the app, we execute this code.
vue.NavigateTo("/signin")
We store a map in our state where the email and password will be in memory:
signin.SetData("login", vue.NewMap)
To create the sign in screen.. we
1. Create a container and add some attributes and classes to it so that it gets centered in the middle of the screen.
2. We add a row to the container, i.e. r1 and then a column r1c1 to it.
3. We add a card to the r1c1 that has a card text and card actions.
4. On the card text we add the logo image and the heading, centered.
5. We then add a form, that contains the email and the password.
6. We add a divider, followed by card actions and inside the card actions add a button.
7. We then add the inject the card to the container and feed the container as a template to the component.
NB: We are using the base class here so that one is able to understand what happens underneath. This also allows us to be flexible with our designs so that they meet our needs.
'create a container
Dim vsignin As VMElement = vm.VContainer("vsignin")
'let it sit in the center of the page
vsignin.AddAttr("fluid", True).AddClass("fill-height")
'create a row
Dim r1 As VMElement = vm.VRow("").AddAttributes(CreateMap("align":"center","justify":"center"))
'create a column
Dim r1c1 As VMElement = vm.VCol("").AddSizes("8", "4", "4", "4")
'create a card
Dim scard As VMElement = vm.VCard("").AddClass("elevation-12")
'create the card text
Dim ctext As VMElement = vm.VCardText("")
'add image
Dim div As VMElement = vm.Div("").AddClass("layout column align-center")
Dim img As VMElement = vm.Img("").AddAttributes(CreateMap("src":"./assets/vuejs.png", "alt":"VueJS Logo", "width":"180","height":"180"))
Dim h1 As VMElement = vm.H1("").AddClass("flex my-4 primary--text").SetText("Sign In")
div.AddStyle("margin-top", "-100px")
div.AddElement(img)
div.AddElement(h1)
ctext.AddElement(div)
'create a form
Dim cform As VMElement = vm.Form("frmsignin")
'create email input
Dim txtEmail As VMElement = vm.VTextField("txtemail")
txtEmail.AddAttributes(CreateMap("append-icon": "person", "name":"email", "label":"Email Address", "type":"text"))
txtEmail.AddAttributes(CreateMap("v-model": "login.email"))
'txtEmail.AddAttributes(CreateMap( ":error": "error", ":rules": "[emailrules]"))
cform.AddElement(txtEmail)
'create password input
Dim txtPassword As VMElement = vm.VTextField("txtpassword")
txtPassword.AddAttributes(CreateMap(":type": "hidepassword ? 'password' : 'text'"))
txtPassword.AddAttributes(CreateMap(":append-icon": "hidepassword ? 'visibility_off' : 'visibility'"))
txtPassword.AddAttributes(CreateMap("name":"password", "label":"Password", "v-model":"login.password"))
txtPassword.AddAttributes(CreateMap("@click:append": "hidepassword = !hidepassword"))
'txtPassword.AddAttributes(CreateMap(":error":"error", ":rules":"[passwordrules]"))
cform.AddElement(txtPassword)
'add form to the card text
ctext.AddElement(cform)
'add card text to the card
scard.AddElement(ctext)
'card actions
Dim cactions As VMElement = vm.VCardActions("")
cactions.AddSpacer
'add a button to the card actions
Dim btnSignIn As VMElement = vm.VBtn("")
btnSignIn.AddAttributes(CreateMap("color":"primary", "@click": "loginx", ":loading":"loading")).SetText("Login")
cactions.AddElement(btnSignIn)
'add a divider to the card
scard.AddDivider
'add card actions to the card
scard.AddElement(cactions)
'add the card tp r1c1
r1c1.AddElement(scard)
'add r1c1 to r1
r1.AddElement(r1c1)
'add r1 to the container
vsignin.AddElement(r1)
'add container to component template
signin.AddElement(vsignin)
Ta!
PS: When a user logs, the details being confirmed are what the user used to register.
'define the callback
Sub loginx(e As BANanoEvent)
'get the user details
Dim userdata As Map = signin.getdata("login")
Dim semail As String = userdata.get("email")
Dim spassword As String = userdata.get("password")
If semail = "" Or spassword = "" Then Return
'show loading
signin.SetData("loading", True)
'
Dim fb As BANanoFireStoreDB = pgIndex.fb
'sign in using email and password
Dim sres As Map
Dim serr As Map
Dim signinp As BANanoPromise = fb.signInWithEmailAndPassword(semail, spassword)
signinp.Then(sres)
signin.SetData("loading", False)
vue.NavigateTo("/")
signinp.Else(serr)
Dim xerror As String = fb.getMessage(serr)
signin.SetData("loading", False)
pgIndex.ShowSnackBarError(xerror)
signinp.end
End Sub