Next.js & Identity (auth) Tutorial #8 - Netlify Dev

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

Key Takeaways

Sets up Netlify Dev to test serverless functions locally using the Netlify CLI

Full Transcript

okay then so now we've created our first netlife function and we've deployed it and we can access it using this endpoint right here we can access that from the browser and we get that response that we want but i want to test my functions locally before i deploy them and currently i can't do that because our local dev server doesn't run them or even know about them so if i was to try and access our functions using localhost port 3000 like this it doesn't work however we can install the netlife cli to give us access to a special netlify dev command and this will spin up a local server so we can preview our site just like before just like this but it will also allow us to run functions locally too now netlify does this by running our functions on one port and our next js site on another port so we have two separate things running on different ports one with our site and one with the functions now by default they can't communicate with each other because they're on different ports so netlify dev spins up a proxy server as well on a single port to bring these two things together under this one port so they can communicate with each other so now we can just use the one port on localhost preview our site and access our netlife functions so let's try installing the netlife cli to do this so you want to install this globally on your computer so in a terminal type npm install netlify hyphen cli and then hyphen g to install this globally on your computer so press enter to do that all right then so now that's installed we can now run netlify dev but before you do this make sure the other process which is running our site on localhost port 3000 when we typed npm run dev right here make sure you cancel out of that process so control c and then yes because this command over here netlify dev is gonna spin up a server at port 3000 as well to show our site on but it's also going to create the proxy server on a different port but it is going to use that 3000 port so make sure you cancel out of the other process first then you can run netlify dev and if we take a look at this let's just make it a bit bigger we can see we've got localhost oops it's opened up already for me let me cross off that we can see we've got up here localhost on port3000 this is for our website but then also it's created this proxy server for us as well and using this we can preview the website but also we can access the functions so we can see that's the one that's opened up right here now there is a problem we don't see any of the links inside the nav bar over here and that's because we're using this different port right here now before when we started to use netlify identity we had to enter in the development settings so it could communicate with netlify on the back end now we've not done that for this port so we need to go to our nav bar just very quickly and i want to close this for a second and what i'm going to do is comment out this where we say auth ready and this as well and i'm going to save this come back over here and now we see this login thing now when we click on this it's again going to ask us for the url of our netlify site so it can communicate with it so every time we use a different localhost port you're going to have to do this so i'm going to go back to our project and i'm going to grab this thing right here so copy that and paste it inside here set the site's url and now i can log in and log out and all that kind of jazz so now i can go back over here and uncomment this because it's now going to work oops don't know what we've done there let me just uh ctrl z let's get rid of this right here and we just want to add in our parentheses again riley messed that up but now that should work so if i save we can still see this let's just test the login flow i'm going to say mario at the netninja code it uk and then test1234 and then i'm gonna log in and now we can see this all works and i can log out as well awesome so now we're previewing on this port i can access the functions on that port as well so if i go to functions and grab this function endpoints all the way up to here so not the domain right here but this part of it this part of the path i'm going to copy that and i'm going to paste it after localhost port 8888 and now i should be able to access it oops i've grabbed all of it i want to take a lot of this away so all of this so just from netlify if i press enter now now it works now we can use that function locally which is awesome so what i could do now is make a request to this endpoint right here to fetch this data from one of our page components so i'm going to do that from the guides component right here so let's open up that guide component inside the pages folder right here and to do this we're going to be using use effect so use effect and i'm going to click on this to auto import it from react this is gonna fire a function and we're gonna pass in an empty dependency array so it just fires once when the components first mounts all right so let's do a fetch request and we're gonna paste in this url right here so it's a relative url so this is going to work locally and also when we deploy this as well so we're going to fetch that resource and then down here we tack on the then method to fire a function when this is complete because it's asynchronous it returns a promise so we take the response inside this function and we do something with it now when we're using the fetch api like this we have to take this response and grab the json data from it and pass it into something we can use now to do that we say response.json which is a function this is also asynchronous and returns a promise so we tack on a then method again to fire another function when we actually have that data returned to us from this and we take that data into this function and we can do something with it so i'm just going to log that to the console console.log data and remember that data is going to be this thing right here so let's give this a whirl i'm going to open up the console now over here and if i refresh it's going to make that request and now we see this data right here this object so we've made a request to our function the function runs at the minute locally but later on it's also going to work when we deploy this as well and it's retrieving that json data for us and we're just logging it to the console so now we know how to work with netlife functions we know how to deploy them we also know how to work with them locally using the netlify cli and netlifydev in the next video what we're going to do is create another function that is going to help us work with netlife identity to detect whether a user is logged in or not and then we can return a response dependence on that status if they're logged in we'll return data if they're not we'll return something else some kind of error so we'll start that process next

Original Description

Hey gang, in this Next.js & Netlify tutorial we'll explore netlify dev using the netlify CLI. This will allow us to test our serverless functions locally. 🐱‍👤 View this course in full without ads on Net Ninja Pro: https://netninja.dev/p/next-js-with-netlify-identity 🐱‍💻 Course Files: + https://github.com/iamshaunjp/next-netlify-identity 🐱‍👤 JOIN THE YOUTUBE NET NINJA GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS 3 & Firebase - https://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 Useful playlists: + Next.js Tutorial for Beginners - https://www.youtube.com/watch?v=A63UxsQsEbU&list=PL4cUxeGkcC9g9gP2onazU5-2M-AzA8eBw + Full React tutorial - https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d 🐱‍💻 🐱‍💻 Other links: + Get VS Code - https://code.visualstudio.com/ + Netlify Identity docs - https://docs.netlify.com/visitor-access/identity/ + Netlify Functions docs - https://docs.netlify.com/functions/overview/ 🐱‍💻 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

Related Reads

📰
Starting the Frontend for a Full-Stack E-commerce Store (Auth Store, Axios, and Public vs. Protected Routing)
Learn to set up the frontend for a full-stack e-commerce store using Auth Store, Axios, and public vs. protected routing
Dev.to · Chinwuba
📰
Practice Frontend Typeahead Interviews With AbortController and ARIA
Improve your frontend interview skills by practicing typeahead searches with AbortController and ARIA, learning to handle debounce, cancellation, keyboard navigation, and accessibility features.
Dev.to · Karuha
📰
Building Maintainable Frontend Systems
Learn to build maintainable frontend systems for long-term success by following practical guidelines and best practices
Dev.to · Ufomadu Nnaemeka
📰
Using the Publish–Subscribe Pattern in React with Native Browser Events
Learn to simplify component communication in React using the publish-subscribe pattern with native browser events
Medium · JavaScript
Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →