Add Auth & Protect Routes in React in 3 Minutes (Kinde)

Add Auth & Protect Routes in React in 3 Minutes (Kinde)

ByteGrad

6 месяцев назад

21,567 Просмотров

Hi, I'm Wesley. I'm a brand ambassador for Kinde (paid sponsorship). Add auth to your React app fast: https://bit.ly/3QOe1Bh
👉 Full code: https://github.com/ByteGrad/kinde-react-example
👉 NEW React & Next.js Course: https://bytegrad.com/courses/professional-react-nextjs

👉 NEW React & Next.js Course: https://bytegrad.com/courses/professional-react-nextjs
👉 Professional JavaScript Course: https://bytegrad.com/courses/professional-javascript
👉 Professional CSS Course: https://bytegrad.com/courses/professional-css

👉 Web development roadmap 2024 & 2025: https://email.bytegrad.com
👉 Email newsletter (BIG update soon): https://email.bytegrad.com
👉 Discord: all my courses have a private Discord where I actively participate
👉 Kinde: check out Kinde for auth and more https://bit.ly/3QOe1Bh

⏱️ Timestamps:
00:00 Auth in React overview
00:28 Kinde setup
02:25 Login & Register
03:04 Authentication options
03:58 Show user info
05:44 Loading state
06:23 Protected route component
08:39 API call with bearer token

#webdevelopment #programming #coding #reactjs #nextjs
Ссылки и html тэги не поддерживаются


Комментарии:

@prashh47
@prashh47 - 12.03.2024 12:06

React native please

Ответить
@dhanarajccs
@dhanarajccs - 12.03.2024 12:08

Awesome video ❤🎉

Ответить
@bryson2662
@bryson2662 - 12.03.2024 13:17

Can you make a tanstack router video

Ответить
@vinniv6806
@vinniv6806 - 12.03.2024 13:29

or just write your own auth logic. It's simple. And you will learn something valuable about it.

Ответить
@Exmantika
@Exmantika - 12.03.2024 13:54

Thanks for the video! This would be really good for OAuth, as you've demonstrated.

Ответить
@blackknight1268
@blackknight1268 - 12.03.2024 16:08

My bro said don't learn web development, ai will replace coding jobs and stuff making me anxious and stuff, and i just started with web development path, idk what to do please guide me about this Ai taking everything like what is it i just joined in man 😢😢😢

Ответить
@RagTheNutsOff
@RagTheNutsOff - 12.03.2024 17:01

Howdy, that's straightforwards compared to writing the base yourself. However can this now be expanded upon further to allow say different user levels of access, any subscription values etc..? Matt

Ответить
@pratiksavaliya3890
@pratiksavaliya3890 - 12.03.2024 17:04

This is simple...please make video on how to integrate this with backend node/express server with protected api end point (authorisation)....i see no resources for that

Ответить
@basitjawad4090
@basitjawad4090 - 16.03.2024 21:46

that was pretty informative. Learnt something today. 🎉

Ответить
@darawan_omar
@darawan_omar - 18.03.2024 16:54

import { auth } from "@/auth";
// import { signOut } from "next-auth/react";
import { authRoutes, apiAuthPrefix, DEFAULT_LOGGIN_REDIRECT } from "@/routes";
import { notFound, redirect } from "next/navigation";
import { NextResponse } from "next/server";
import { signOut } from "next-auth/react";

export default auth(async (req) => {
const isLoggined = !!req.auth;
const { nextUrl } = req;
const userPagePermission = ["main", "sale_a", "users", "my_debts"];

const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix);
const isAuthRoute = authRoutes.includes(nextUrl.pathname);
const isAccess = userPagePermission.includes(nextUrl.pathname.split("/")[1]);

if (isApiAuthRoute) {
return;
}
if (isAuthRoute) {
if (isLoggined) {
return Response.redirect(new URL(DEFAULT_LOGGIN_REDIRECT, nextUrl));
}
return;
}
// if (isAccess) {
// console.log("access it");
// return;
// }
// if (!isAccess) {
// // signOut();
// // redirect("/not-access");
// // console.log("not access");
// }

if (!isLoggined) {
return NextResponse.redirect(new URL("/login", req.url));
}
return;
});

// Optionally, don't invoke Middleware on some paths
export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};...................................................this is my Middleware code in the condition when !isAccess true clear the session-token i save it when user login it when clear the session it will return the user to login automaticlly , but i do not know how do that the both method signOut of Auth like signOut of server side or Client side not working in the condition !isAccess give me the sloution please ? i use Auth js the version 5 beta ..........i appreciate for helping me ❤

Ответить
@Can-el3cj
@Can-el3cj - 19.03.2024 21:41

nice video!

Ответить
@voldemortvi4264
@voldemortvi4264 - 20.03.2024 11:32

i swear this was the easiest authentication in react i have ever seen

Ответить
@darawan_omar
@darawan_omar - 20.03.2024 22:56

import { auth } from "@/auth";
import { authRoutes, apiAuthPrefix, DEFAULT_LOGGIN_REDIRECT } from "@/routes";
import { notFound, redirect } from "next/navigation";
import { NextResponse } from "next/server";

export default auth((req) => {
const isLoggined = !!req.auth;
const { nextUrl } = req;
const userPagePermission = ["main", "sale_a", "users", "my_debts"];
const isApiAuthRoute = nextUrl.pathname.startsWith(apiAuthPrefix);
const isAuthRoute = authRoutes.includes(nextUrl.pathname);
const isAccess = userPagePermission.includes(nextUrl.pathname.split("/")[1]);

if (isApiAuthRoute) {
return;
}
if (isAuthRoute) {
if (isLoggined) {
return Response.redirect(new URL(DEFAULT_LOGGIN_REDIRECT, nextUrl));
}
return;
}

if (!isLoggined) {
return NextResponse.redirect(new URL("/login", req.url));
}
return;
});

export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
};
..............................this is my middleware code i use auth js the version 5 beta how we can do that if the isAccess is not true logout the user by clear the session like use the signOut server side function after clear the session automaticaly user redirect to login page ? how we can implemnet the signOut if isAccess false ?

Ответить
@404-not-found-service
@404-not-found-service - 22.03.2024 00:44

Thank you, I just wanted to implement this in my project with next, a hug and these videos are appreciated <3

Ответить
@MaheshR-l8p
@MaheshR-l8p - 27.03.2024 21:28

Is there any git repository to learn reactjs (professional) with redux, middleware.

Ответить
@trinidadjohnpatrickc.441
@trinidadjohnpatrickc.441 - 10.05.2024 03:21

This is pretty useful if you already know how authentication works and dont want to hassle yourself coding it by scratch. Thanks for the video!

Ответить
@MortenLindorf
@MortenLindorf - 13.05.2024 15:28

Brilliant pitch, great knowledge

Ответить
@VibratorySix8
@VibratorySix8 - 05.10.2024 23:49

i don't know if i missed a part of the video or not but when i tried this when ever i refresh the page or go to diffrent page i lose the authentication and have to relogin how do i keep the user logged in ?

Ответить