Angular Login and Signup Page | Local-Storage | angular tutorial | angular tutorial for beginners

LEARNING PARTNER
26 Oct 202214:54

Summary

TLDRIn this Learning Partner video, the focus is on creating login and sign-up pages using Angular. The tutorial demonstrates how to utilize HTML and CSS templates from freefrontend.com, integrating them with Angular's forms module for data binding. It covers storing user data in local storage upon sign-up and checking credentials during login. The video also addresses common issues like data replication and provides solutions. The presenter guides viewers through the process, emphasizing the importance of initializing objects to avoid reference issues. The tutorial concludes with a demonstration of the login functionality, highlighting the importance of matching user credentials for successful authentication.

Takeaways

  • 💻 The video demonstrates how to create a login and signup page using Angular, focusing on local storage for storing user data.
  • 🔎 The template used for the login/signup page was downloaded from Google and uses HTML and CSS from freefrontend.com.
  • 📋 Instead of integrating with an API, the data will be stored in the browser's local storage, specifically in an array containing user credentials.
  • 📝 Two objects are created for storing the user data, one for the signup form with fields for username, email, and password, and another for the login form with username and password.
  • 🔗 The video uses Angular's NG Model for two-way data binding, connecting the form fields to the objects created.
  • ⚠️ The FormsModule must be imported in the app module for the NG Model to work without causing errors during compilation.
  • 📦 When a user signs up, their data is pushed to an array and stored in local storage after being converted to a JSON string using `json.stringify`.
  • 🔄 A common issue of repeated entries in local storage is fixed by clearing the object references after each submission.
  • 🔑 During login, the user’s credentials are compared with the data in local storage. If a match is found, the user logs in successfully; otherwise, an error message is shown.
  • 🔍 The video concludes by noting that this logic can be adapted for API integration instead of relying on local storage, while keeping the same core principles.

Q & A

  • What is the main focus of the video script?

    -The main focus of the video script is to demonstrate how to create login and sign-up pages using Angular, including storing user data in local storage.

  • Where can one find the HTML and CSS code for the login page as mentioned in the script?

    -The HTML and CSS code for the login page can be found on freefrontend.com, as mentioned in the script.

  • What is the purpose of the 'signupUsers' array in the script?

    -The 'signupUsers' array is used to store the user data provided during the sign-up process before saving it to local storage.

  • How is the sign-up form data bound to the 'signupUsers' array in Angular?

    -The sign-up form data is bound to the 'signupUsers' array using the 'ngModel' directive in Angular, which is linked to the form fields like username, email, and password.

  • What is the role of the 'FormsModule' in the Angular application discussed in the script?

    -The 'FormsModule' is necessary for using the 'ngModel' directive in Angular. It must be imported into the app module to enable the use of 'ngModel'.

  • How does the script handle the storage of user data into local storage?

    -The script stores user data into local storage by pushing the sign-up object into the 'signupUsers' array and then using 'localStorage.setItem' to save the array as a stringified JSON.

  • What is the issue faced when storing data in local storage as described in the script?

    -The issue faced is that the same data gets replicated in local storage. This is resolved by initializing the sign-up object before pushing it into the array.

  • How does the script handle the login process?

    -The login process involves reading the 'signupUsers' array from local storage, and then checking if the provided username and password match any record in the array.

  • What is the method used to read data from local storage in the script?

    -The method used to read data from local storage is 'localStorage.getItem', which retrieves the data associated with the specified key.

  • How does the script differentiate between correct and incorrect login credentials?

    -The script checks if a user with the provided username and password exists in the 'signupUsers' array. If a match is found, it confirms a successful login; otherwise, it shows an error for wrong credentials.

  • What is the suggestion for integrating with an API instead of using local storage?

    -The suggestion is to use the same logic for handling sign-up and login processes but instead of storing and reading from local storage, one should make API calls with the provided user input.

Outlines

00:00

🌐 Introduction to Angular Login and Sign Up Page

The video begins with an introduction to creating a login and sign-up page using Angular. The presenter explains that they have downloaded a template from Google and will demonstrate how to implement it. The template is sourced from freefrontend.com and consists of HTML and CSS. The presenter's focus is on creating a user interface without integrating it with any API, instead opting to store user data in local storage. The process involves creating arrays to store user data, defining variables for sign-up and login objects, and binding these objects to forms using Angular's two-way data binding with the 'ngModel' directive. The presenter also emphasizes the need to import the FormsModule in the app module to use 'ngModel'.

05:01

📝 Storing User Data in Local Storage

In this segment, the presenter discusses the process of storing user data in local storage upon sign-up. They explain how to push the sign-up object into an array and then save that array to local storage using the 'localStorage.setItem' method. The data must be converted to a string format using 'JSON.stringify' before storage. The presenter also addresses an issue with data replication and provides a solution by initializing the sign-up object before pushing it to the array. They then demonstrate how to read data from local storage and populate the sign-up array upon page load. Additionally, the presenter outlines the process for the login functionality, which involves reading from local storage and verifying user credentials against the stored data.

10:04

🔐 Implementing Login Functionality and Handling Credentials

The final paragraph focuses on the implementation of the login functionality. The presenter describes how to check user credentials against the data stored in the local storage array. They use a Lambda expression with the 'find' method to search for a user record that matches the provided username and password. If a match is found, an alert is shown indicating a successful login. If no match is found, an error message is displayed. The presenter also discusses potential issues with form binding and how to ensure that the correct data is being passed for validation. They conclude by suggesting that while local storage is used for demonstration, in a real-world application, an API would be used for handling user data.

Mindmap

Keywords

💡Angular

Angular is a platform and framework for building single-page client applications using HTML and TypeScript. In the video, Angular is used to create a login and sign-up page. The script mentions using Angular to handle form binding, events, and data storage, which are core functionalities in web application development.

💡HTML

HTML (HyperText Markup Language) is the standard markup language for creating web pages and web applications. The video script refers to downloading a template from Google, which likely involves HTML for structuring the login and sign-up page's content.

💡CSS

CSS (Cascading Style Sheets) is used for describing the presentation of a document written in HTML. In the context of the video, CSS is mentioned in relation to styling the login and sign-up page, ensuring it has a visually appealing layout.

💡Local Storage

Local Storage is a way to store data on the client's browser. It is used in the video to save user data after sign-up and to verify user credentials during login. The script explains how to store and retrieve data from local storage, which is a common practice for maintaining user state in web applications.

💡Form Binding

Form binding in Angular refers to the process of linking form controls to data models. The script describes how to bind the sign-up and login forms to objects using the 'ngModel' directive, which is essential for capturing user input in web forms.

💡Event Handling

Event handling is a programming concept where certain actions are performed in response to user actions like clicks or key presses. The video script discusses creating event handlers for the sign-up and login buttons, which are crucial for processing form submissions in web applications.

💡JSON

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. The script mentions using JSON.stringify to convert data into a string format for storage and JSON.parse to convert it back into an object, which is necessary when dealing with local storage.

💡TypeScript

TypeScript is a superset of JavaScript that adds optional static typing to the language. It is used in Angular for writing application logic. The video script implies the use of TypeScript when discussing the creation of objects and arrays to handle user data.

💡API

An API (Application Programming Interface) is a set of routines, protocols, and tools for building software applications. The script suggests that while the current implementation uses local storage, a real-world application would likely integrate with an API for user authentication and data management.

💡ngModel

ngModel is a directive in Angular used for two-way data binding between a domain model and the view. The video script explains how to use ngModel to bind form fields to data models, which is a fundamental technique in Angular for creating interactive forms.

💡Freefrontend.com

Freefrontend.com is a resource mentioned in the script as a source for free HTML and CSS templates. The video uses a template from this site to demonstrate how to create a login and sign-up page, highlighting the utility of such resources for web developers.

Highlights

Introduction to creating login and sign-up pages using Angular.

Using a template from freefrontend.com as a starting point for the design.

Explanation of copying and pasting CSS and HTML to achieve the desired output.

Storing user data in local storage for demonstration purposes.

Creating an array to store sign-up users and defining the data structure.

Binding form fields to the sign-up object using Angular's NG model.

Ensuring the FormsModule is included in the app module for NG model to work.

Creating event handlers for the sign-up and login buttons.

Storing sign-up data into an array and then into local storage.

Using JSON.stringify to convert data into a string format for local storage.

Addressing the issue of data replication by initializing the sign-up object.

Reading data from local storage and assigning it to the sign-up users array.

Writing a query to check user credentials against stored data for login.

Using JSON.parse to convert string data back into an object for login verification.

Handling login success and failure with alert messages based on user credentials.

Debugging and checking the login process with both correct and incorrect credentials.

Advice on using APIs for sign-up and login in a real-world application.

Conclusion and a call to action for viewers to subscribe and like the video.

Transcripts

play00:00

welcome back to Learning Partner so in

play00:02

this video we are going to see login and

play00:04

sign up page again with angular only so

play00:06

this is the normal template I have

play00:08

downloaded from Google only so let's see

play00:10

how we I have come to this page simply I

play00:14

have searched login page HTML CSS then

play00:16

we have a site that freefrontend.com so

play00:19

if we go to that we have found this

play00:22

example okay the code is already ready I

play00:25

just have we just have to copy paste it

play00:28

okay it's just a CSS and HTML and if you

play00:31

copy paste it in your component you will

play00:33

get the same output okay so let's get

play00:35

started uh so far we are not integrating

play00:38

it with this any API so simply we are

play00:40

going to store the data into the local

play00:42

storage means whenever user is logged in

play00:44

user is signing up we will store the

play00:47

user's array like how many users do the

play00:50

sign up into the local storage and while

play00:52

login we will just simply check for the

play00:56

user credential into that local storage

play00:58

array okay so let's get started first we

play01:01

need to create an array to store the

play01:03

signup user so sign up users will be my

play01:07

variable name

play01:09

that will be again any of errors

play01:13

instead of any you can create class or

play01:15

construct class or interface also based

play01:18

on the choice just to save the time I'm

play01:19

using enter uh just make it in as any

play01:22

let's create a object

play01:25

which will which we will bind to the

play01:27

form that is sign up

play01:30

object

play01:32

colony

play01:38

equal to curly bracket so let's see how

play01:41

many fields we need let's go back to the

play01:43

browser

play01:45

okay so in sign up we need username

play01:47

email and the password so let's create

play01:49

an object of for these three properties

play01:52

username

play02:00

email and password

play02:05

okay so these three things we need then

play02:08

in let's create an object for the login

play02:10

also so login object colon any equal to

play02:13

curly bracket then we have username and

play02:16

password so I login we just need the two

play02:18

Fields so we are creating second object

play02:20

for that

play02:24

let's get rid of the email

play02:28

okay so we have our array ready then

play02:31

signup object is also ready and login

play02:33

object is also ready now next thing is

play02:35

we have to bind this to our form using

play02:37

NG model so sign up object so here you

play02:41

can see we have got two form one is the

play02:43

sign up form and second is the login

play02:44

form so here we can use the NG model

play02:47

that is banana bracket

play02:49

and we have to buy in our model dot

play02:52

particular field name

play02:54

username

play02:57

let me just copy paste the basic things

play03:00

email

play03:02

username and password

play03:07

it's better to copy paste so that we can

play03:09

we won't make any spelling mistake okay

play03:12

so our first task is completed next

play03:15

let's bind the login form also with the

play03:18

login object

play03:20

it has only two Fields username and the

play03:22

password

play03:24

okay so I'm not covering the basic

play03:25

things like uh to use the NG model you

play03:28

need forms model present in your app

play03:30

module let's see how is that let's go to

play03:32

the forms module

play03:34

app module

play03:36

in the tab module this should be forms

play03:39

model should be present okay if this is

play03:41

not present in your app module then NG

play03:43

model won't be recognized then you will

play03:45

have a compilation error okay so first

play03:47

thing you need to add the forms module

play03:48

in the app module in the import section

play03:50

okay let's go back to our component so

play03:53

our NG model we have binded next thing

play03:56

is like on sign up and on login button

play03:58

we need to write the events so let's

play04:00

write those event

play04:05

foreign

play04:08

up and then on login

play04:17

let's create these events

play04:26

same as second one login also

play04:33

okay now on first things we will do is

play04:36

like on sign up on sign up what we need

play04:37

to do we need to store the uh

play04:40

information what what user will be

play04:42

provided into the local storage as in

play04:44

the array form okay so for that

play04:47

first we need to store the data into the

play04:50

array so for that this dot signup users

play04:54

array

play04:55

dot push so we are going to push the

play04:58

current object into our array this DOT

play05:01

sign up array

play05:02

on sign up object okay next thing once

play05:06

we push the current object to the array

play05:08

we have to store this array into the

play05:10

local storage so local storage

play05:12

dot set item that is the method we use

play05:15

to store the item it has two parameter

play05:16

first is the key and second is the value

play05:18

sign up users

play05:21

this will be our key then while storing

play05:24

the data into the local storage we have

play05:26

to convert the data into the string

play05:27

format so we have to go for the

play05:29

json.singify

play05:32

and we have to pass our array that is

play05:34

this dot signup users

play05:37

okay so let's let's just see till this

play05:40

now let's save all and check it

play05:45

okay

play05:46

let's open the console

play05:49

in console we have one error let's check

play05:51

it

play05:52

okay it's nothing else

play05:55

so let me clear the logo storage first

play05:57

okay

play05:59

providing username

play06:04

email

play06:07

we are not going to see the validation

play06:08

because it now we are more mainly

play06:10

focusing on the login and the signup

play06:12

process

play06:13

okay let's do a click on the sign up

play06:16

okay so you can see once we clicked on

play06:18

the sign up you can see in the local

play06:19

storage we have got the array okay let's

play06:22

add one more record

play06:31

foreign

play06:38

okay let's do the sign up

play06:40

okay so here you can see in the local

play06:42

storage area we have got two records but

play06:44

let me check like why it is getting uh

play06:48

the old record got repeated okay so for

play06:50

that while storing the data we need to

play06:53

detach the array with our local object

play06:56

so for that

play06:59

let's create let's

play07:02

okay so the issue what we face like same

play07:06

data was getting replicated so for that

play07:08

we just need to initialize our object

play07:10

okay so let's just copy this sign up

play07:13

object and let's initialize this once we

play07:15

push it this dot sidewalk object let's

play07:18

remove this any let's save it and try to

play07:21

check now

play07:30

foreign

play07:32

was like we didn't clear it or

play07:35

initialize this so it was keeping the

play07:37

reference okay so this is the first user

play07:39

let's add the second one

play07:49

okay so you can see in the local storage

play07:52

we have got separate elements first we

play07:54

use record is there and second myory

play07:56

record is there okay now next thing is

play07:58

for the sign up login so on the login

play08:01

what we are going to do we are going to

play08:03

read the data from that local storage

play08:04

and see if that whatever the user ID

play08:07

email and the password is present or not

play08:09

if it is there instead of email we can

play08:12

make it as make it use of the username

play08:14

also so we will check like if the

play08:17

provided username and password is there

play08:19

or not if it is there then we will allow

play08:22

the user to log in so we will just

play08:25

simply show the alert message like user

play08:27

is successfully logged in and if the

play08:29

wrong password is there we will show the

play08:31

error in the alert box

play08:33

okay so let's on the login button we

play08:36

have what we are going to do on the page

play08:38

let's say

play08:40

while sign up we have stored the data

play08:42

into the local storage right and so and

play08:46

same data we are storing in the array

play08:48

also that is signup user so we have to

play08:50

check with this sign up array only but

play08:52

if we refresh the page again we need to

play08:54

read it back so on the log ng1 unit we

play08:57

have to read the local storage data and

play08:59

push it uh assign it to the

play09:03

sign of users array so constant so first

play09:07

we have to read the local storage data

play09:09

and put it in the constant local data

play09:14

is equal to local storage dot gate item

play09:16

so while storing the data we had the

play09:18

method set item while reading the data

play09:21

we have the gate item okay

play09:23

then what this is this was our key

play09:26

okay then we have to add the if null

play09:29

check if local data is not equal to none

play09:31

means if this particular key is present

play09:33

in the local storage then only we are

play09:36

going to execute the remaining

play09:38

statements

play09:39

then these dot signup users

play09:44

is equal to Json dot parse because while

play09:47

storing the data into the local storage

play09:48

we have to converted that to the string

play09:51

so we have to again convert that into

play09:53

the array object so we have to pass it

play09:55

local data

play09:59

okay okay

play10:00

so on the login what we need to do we

play10:04

just need to run a query on signup users

play10:07

okay

play10:10

so in login page we have to write

play10:12

constant is user exist const is user

play10:16

exist

play10:20

is equal to this dot signup users dot

play10:23

find

play10:24

okay so find is a method where we can

play10:26

assume like only one record can be found

play10:29

with the matching conditions so here we

play10:32

have to write the Lambda expression

play10:34

we have to compare with m dot that will

play10:37

be username

play10:40

equal to equal to

play10:45

this Dot Login object dot username which

play10:48

we will bind to the form

play10:51

use and and the second thing password

play10:54

also we need to compare m dot password

play10:57

is equal to login object.password

play11:08

okay so this is how our whole condition

play11:11

will look like from this array we are

play11:13

trying to find a particular user where

play11:15

username is this and password is also

play11:17

this if we got the any particular

play11:20

records we will get the record if not

play11:22

the in is user exist will be undefined

play11:25

so let's add a condition for that so if

play11:27

is user not equal to undefined means we

play11:31

have got the user then we can show the

play11:33

alert box

play11:35

like user found user logged in

play11:37

successfully

play11:40

user logged in successfully

play11:43

in else we can show the error like round

play11:45

credentials

play11:59

wrong credentials

play12:02

means if we don't find any matching user

play12:05

in this array so we can say like it is a

play12:07

wrong credential

play12:10

okay

play12:11

so let's let's just add a debugger and

play12:13

check it we will check both the

play12:15

scenarios when once we pass the correct

play12:17

username and password and one where we

play12:20

provide the wrong details okay so let's

play12:22

check the local storage

play12:25

so let's try the first one that is

play12:27

username is piyush as you can see in the

play12:29

bottom

play12:31

okay why we are getting the same data

play12:35

let's check the form I think same name

play12:37

might be binded

play12:41

username is this and password is this

play12:48

okay because NG model is same

play12:52

username username field is there so we

play12:55

just need to change it

play12:57

it was a password that's why once we

play12:59

change once we are changing the first

play13:00

same was replicating to the other text

play13:03

box

play13:03

let's click on the login

play13:05

[Music]

play13:06

username is piyush

play13:09

and password is one two three five okay

play13:11

you can see in the bottom

play13:13

so let's click on the login

play13:16

okay let's do the F10

play13:20

one more time okay now if you hover on

play13:23

the is user exist

play13:25

you can see the data what the current

play13:27

particular object we have got

play13:30

if you see the login object

play13:33

okay we have got the positioning

play13:35

password we have got the password we

play13:37

have entered okay so in this sign up

play13:39

users

play13:42

sign up users we have that record where

play13:44

username is this and password is this

play13:46

okay so we have said the is user exist

play13:50

is not undefined means we have got the

play13:52

particular record so we will show the

play13:54

alert okay let's try with the wrong

play13:56

password now

play14:00

let's try with the wrong password

play14:06

okay now let's see

play14:10

okay now if you check is user is

play14:12

undefined because the password we have

play14:15

provided is wrong

play14:18

see this is not the password we have in

play14:20

the array for that particular record so

play14:22

we have got undefined okay so it will go

play14:25

to the S block so this is the way you

play14:28

have to perform the sign up and the

play14:30

login instead of local storage you have

play14:32

to use the API but the basic logic will

play14:35

be the same now instead of the local

play14:36

storage code and the Reading part you

play14:39

just have to hit your API with provided

play14:41

input okay

play14:43

so that's it with this current video

play14:45

next time again we will see the more

play14:48

exploring topic related to angular thank

play14:50

you please do subscribe and like my

play14:52

videos

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
AngularLogin PageSign-UpLocal StorageWeb DevelopmentFrontendHTMLCSSJavaScriptTutorial
¿Necesitas un resumen en inglés?