Next.js & Identity (auth) Tutorial #10 - Client Side Content Guard
Key Takeaways
Builds a client-side content guard using Next.js and Netlify Identity
Full Transcript
all right then so now we have the function guides to return the data if we're logged in if we're not logged in we get back this status code and this message right here so what i'd like to do inside the guides page is to output the content the guides if we have them and also output an error if we have that instead if they're not logged in so i want two pieces of state one for the guides and one for the error so let's create those first so const and then guides and set guides and we're going to set that equal to use state press this to auto import it from react and to begin with this is going to be null when we first load the page we also want one for the error so const error on set error and we set that equal to use state again this time this is going to be null as well to begin with all right so we have this now what we need to do is update these values dependent on the result of this so right here what i'm going to do is delete this and open up a function instead so right here when we get the response what i'd like to do is just log this to the console so console.log response like so and if i save this and refresh we can see right here the response object yeah now inside this we have an okay property now notice in this case it's false and that's because we're getting this error status code back right here so we know from this property okay being false that we don't have the guides and there was some kind of error an authentication error in our case so we could use that property to determine that on the front end and if this is false then we can throw an error so that's what we're going to do we're going to check response.ok so i'm going to say if and then not response dot okay and that means that if this is false then it's going to fire this block so if it's not okay the response we want to throw an error and that error is going to have a message and i'm going to say you must be logged in to view this content now you could use the error message we get back from the response but i don't want to have to pass the json to do that so i've just made up this error message right here now since we have an error being thrown here we can catch that in the catch block so at the end i'm going to tack on a catch method and this fires a function if an error is thrown and we take in that error as an argument to that function now with this area we can do something and what i want to do is update this error right here so i can use set error to do that and i'm going to set it equal to the error object we get right here dot message and the message property is this thing right here so all we're doing is updating the error state with this message if there's an error if the response is not okay so that we can show it down here in the template later on now if we do get an error at some point i want to set this to null it's not already to begin with but they might log in and we might update that later on and if they then log out i want to reset this back to null so what i'm going to do is set that to null right here so set guides and then no like so okay so that's the error kind of sorted we've not output it yet but in terms of the fetch that's all we need to do now if this response is okay then it's not going to fire this if block and it's going to carry on instead and at that point i want to return response.json so we can get the data because if it's okay we obviously have data on the response so at that point this then method is going to fire now we don't just want to log out the data to the console in this case we want to update the guides right here so i can say set guides and then pass in the data to do that and i also want to reset the error so i'm going to say set error and set that to be null again because if we first land on the page and we try to make this request without being authenticated we're gonna get an error right here right because we set it over here now if we log in and stay on the same page it's gonna try to make this fetch request again and get the data back i then don't want to keep the error and keep showing it on the page so i want to reset it and that's why we do this all right so now we're setting the error and the guide depending on the state of this fetch request now what we can do is check for those properties inside the templates and output them if we have them so let me get rid of this h2 and instead we're going to do a few different lines of code here the first thing i'm going to do is check for auth ready because if all thread it is not true then we're not going to show anything yet and at that point maybe we show something like a loading screen or a loading message so i'm going to do curly braces and say not auth ready so if authentication is not ready yet then i'm going to output a div that just says inside loading so until we've established that identity connection we don't know really whether we're going to get the data successfully or not and during that time even if it's only for a second or a split second we're going to show loading all right so the second thing we want to output if we have an error is the error so error and and then we're going to output a bit of template so parentheses inside here i'm going to do a div with a class name and i'm going to set that equal to styles dot error like so now i can access styles because i import them right here from the guides module which is inside the styles folder over here now we'll add in the error class later on but for now let's just give it that class and close off this div as well now inside this div i want to output the error inside a paragraph tag so error like so so only if we have an error then we'll output the error all right so finally if we have the data i want to output the data so we need to check for the guides and then double ampersand and then we want to map through the guides because it will be an array of data and we'll take each guide as we map through it and we'll return some template for each guide so the template i want to return is going to be a div first of all so let's open and close this now this needs a key property because when we map through data and output a template for each item in that data each item that we output has to have a key property so that react can keep track of them so the key is going to be unique for each one i'm just going to use the title because the title of each guide in my case is unique but probably normally you would use something like an id property all right then so i'm also going to give this a class name and set that equal to styles dot card again this class doesn't exist yet but we'll make it later all right then so inside this we'll do an h3 and that will be for the guide title so guide dot title and then under the h3 i'm going to do an h4 and that is going to say written by dot author because we have an author property on each guide and then finally i'm just going to do some lorem ipsum right here but to be honest that would probably be some actual real data for the guide if we had it so instead of just having a title and an author maybe it would have a content property as well and we'd output that right here all right so that's pretty much it i'm going to cross my fingers now and hope this works so you see when we first load this we're not logged in and we get this error message back right here you must be logged in to view this content so if i refresh let's see if that still works yep we still see this message if i log in so mario at the code at netninja.com and then test1234 i'm gonna log in hopefully we're gonna see the data now yep we see all of that data and if i refresh we should see all of that data as well if i log out then we should hide that data and it says you must be logged in to view this content awesome so this is all working the only thing we need to do now really is style this content because it looks absolutely pants so let's open up the guides module and i'm literally just going to copy these from my github repo over here woohoo so let me copy all of this stuff and don't forget you can get this by going to the course files the link is down below choosing lesson 10 and then finding the guides module css file so i've copied those i just want to paste them over here like so save it and come back over here okay so that's the error message that looks a bit better and if i log in with mario the net ninja code uk test1234 i'm gonna log in and now we can see they look a bit better as well awesome so everything now my friends is working now finally i just want to deploy this to netlify to make sure everything works up on the web as well so let me open up my terminal over here i'm going to open up the second terminal and i'll say git add then git commit m and we'll say identity function added for the message press enter we need to push this up now git push origin main once we push that up to github it's going to trigger deployments on netlife forest and if i refresh over here hopefully we'll see that process starting yet we do awesome so let's just wait for this to build once that's done we can open this up in a browser and we can try this out i'm going to go to guides and you can see i'm not logged in so we get the error message if i log in now i'm going to say mario at the net ninja code uk and then test one two three four i'm gonna log in and then hopefully we should see the content and if i refresh we get that content as well awesome all working
Original Description
Hey gang, in this Next.js tutorial we'll put a client-side content guard in place to protect content intended only for authenticated users.
🐱👤 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
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
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
Related Reads
📰
📰
📰
📰
Building a Browser-Based Tiled Poster PDF Generator
Dev.to · Gaven
3 Next.js Micro-Interactions to Make Your App Feel Premium
Dev.to · jackke88
Test your vanilla JS app in the real browser, with no build step
Dev.to · Kevin Julián Martínez Escobar
Modern Frontends Don’t Have One “Ready” State
Dev.to · Markus Gasser
🎓
Tutor Explanation
DeepCamp AI