Vuex 4 & Firebase Auth Tutorial #8 - Login Action

Net Ninja · Beginner ·🌐 Frontend Engineering ·4y ago

Key Takeaways

This video tutorial covers creating a login action using Vuex 4 and Firebase Auth, including setting up the login function, handling errors, and updating the user state.

Full Transcript

all right then so now we have our sign up action right here when a user signs up and when a user signs up firebase sends us back the user on the response so we commit that mutation set user and pass in that user so that we update the state right here to be that new user who's just signed up and that happens so now when they sign up our global state becomes that new user and then we can access that user wherever we want in the application now what i'd like to do is move on to the next action which is going to be the login action and this is actually going to be very similar to this one right here so first of all we need to import another function up here from firebase and this one is called sign in with email and password so sign in with email and password like so all right so now we have that i'm going to create a new function down here so comma and then async login like so we take in the context object and also we want the email and password on the payload so let's destructure that like so and then the inside of the function is going to be almost identical to this one so what i'm going to do is copy all this and paste it down here so first of all let's change this to login action and then over here we still get a response the function this time though is called sign in with email and password we're still passing the auth object as the first argument then the email then the password then we're logging out the response here we don't need that and in fact we don't need it up here as well let's get rid of that but if we have a response then we commit a mutation set user and we pass in the user object on that response object so again when we sign in and we commit this mutation right here we're saying okay update the user state to be that new user we pass in as a payload all right so that's when they sign in all right so then we'll say right here could not complete login like so and that's pretty much it for this action so now all we need to do is use the action inside the login component so again a lot of this is going to be very similar to this signup code right here so what i'm going to do is just copy all of this from error down here like so copy all of that and come over here and replace this thing so up here paste it in so we have the error now we have the store and the router and in fact we need to import those so let's do that as well import and we want the use store function from vue x then we also want use router as well so import use router and that comes from view router like so okay so handle submit is an async function we try this where we dispatch an action this time it needs to be the login action we're still passing the email and password values which are these refs and then if it's a success we push them to the home page if not we catch the error and we update our error ref to be the error message we need to also return the error right here and then we can output the error up here as well so i'll do another div with a v hyphen if and we set that equal to error so only if there's an error where we try to output it so in curly braces error like so then close off the div alright so that my friends is pretty much it so when they submit this form now we should be trying to dispatch the login action which is going to try to sign them up or rather sign them in sorry with this function and then when that's complete we get a response object back we commit a mutation to update our state with that user that's logged in all right okay so let's now save all this and give it a whirl all right so we already created that user mario at netninja.dev let's try logging in with that user so mario netninja dot dev and then test one two three in fact we'll just do test one two three which is the incorrect password because i want to show you the error right here so log in and we can see firebase error wrong password now you might not want to show that error to your user you could write a custom error instead but for the sake of this tutorial it's fine we know it's the wrong password so now i can add on forum five login then it should redirect us to the home page and we can see user state changed and this is the new user state in the store now and we can see it right here alright awesome so now we can sign in and now we can log in and when we do those things we're updating our store state to reflect that user who was either signed up or logged in next up we need to handle what happens when we try to log out

Original Description

Hey gang, in this lesson we'll create a login action to log the user in & then update our user state. 🔥🔥 Get the full course now on Net Ninja Pro 👉 https://netninja.dev/p/vuex-with-firebase-auth 🐱‍💻 Access the course files on GitHub: https://github.com/iamshaunjp/Vuex-4-Firebase-Auth 🐱‍💻 Starter Project: https://github.com/iamshaunjp/Vuex-4-Firebase-Auth/tree/lesson-1 🐱‍💻 Vue 3 & Firebase Complete Course: On Net Ninja Pro - https://netninja.dev/p/build-websites-with-vue-3-firebase On Udemy - https://thenetninja.co.uk/udemy/vue-and-firebase 🐱‍💻 Vue 3 Basics Course: On YouTube - https://www.youtube.com/watch?v=YrxBCBibVo0&list=PL4cUxeGkcC9hYYGbV60Vq3IXYNfDk8At1 🐱‍💻 Getting Started with Firebase Course: On Net Ninja Pro - https://netninja.dev/p/getting-started-with-firebase-9 On YouTube - https://www.youtube.com/watch?v=9zdvmgGsww0&list=PL4cUxeGkcC9jERUGvbudErNCeSZHWUVlb 🐱‍👤 My Premium Udemy courses: https://thenetninja.co.uk/udemy/react-and-firebase https://thenetninja.co.uk/udemy/vue-and-firebase https://thenetninja.co.uk/udemy/modern-javascript https://thenetninja.co.uk/udemy/d3-and-firebase 🐱‍👤 Get access to all other premium courses on Net Ninja Pro: https://netninja.dev/ 🐱‍💻 My Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

This tutorial teaches you how to create a login action using Vuex 4 and Firebase Auth, and how to handle errors and update the user state after login. You'll learn how to use Vue Router for client-side routing and Vuex for state management.

Key Takeaways
  1. Import the necessary functions from Firebase and Vue
  2. Create a new login action
  3. Handle errors during login
  4. Update the user state after login
  5. Use Vue Router for client-side routing
  6. Utilize Vuex for state management
💡 You can use Vuex to manage the user state and update it after login, and Vue Router for client-side routing.

Related Reads

📰
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Learn the 5 levels of front end engineering to improve your skills and avoid getting stuck in a career rut
Medium · Programming
📰
Browser-Based PDF Editing with Vue 3 and pdf-lib
Learn to build a browser-based PDF editor using Vue 3 and pdf-lib, enabling users to edit PDFs directly in the browser
Dev.to · sunshey
📰
Say Goodbye To Electron?
Learn about a new approach to building native applications without Electron, using frontend-style development
Medium · Programming
📰
Why Vanilla JS? In the article below, I am sharing my story of building SaaS product in vanilla js and explaining why I decided to go with this approach. https://guseyn.com/html/posts/why-vanilla-js.html
Learn why choosing Vanilla JS can be a great approach for building SaaS products and how it can simplify development
Dev.to · Guseyn Ismayylov
Up next
How To Build A Twitter Clone - React Next JS - Appwrite Crash Course
Adrian Twarog
Watch →