SvelteKit Authentication Using Cookies

Поділитися
Вставка
  • Опубліковано 31 січ 2025

КОМЕНТАРІ • 130

  • @JoyofCodeDev
    @JoyofCodeDev  2 роки тому +8

    Hey friends! 👋
    I hope you learned how SvelteKit auth works but in your projects I would advise you to use an auth library that deals with edge cases like Lucia.

    • @ricardoreix
      @ricardoreix 2 роки тому +4

      Would love to see a video about that 👍

    • @hnazmul
      @hnazmul Рік тому

      @@ricardoreix agree with you.

  • @kymion
    @kymion Рік тому +6

    I really, really appreciate how you make a text article with every video, especially with longer videos like this. It makes it SO much easier to focus on things that I need more time to think about than jumping around pausing all over the place.

  • @aldyreal
    @aldyreal 2 роки тому +4

    Thank you so much for this!
    I was new to sveltekit, found one of your old vid about this, but after that route changes I couldn't really understand the old one.
    You're the best!

  • @hmiiro
    @hmiiro 2 роки тому

    Yooo! my voice-man is here with a great refresh. it was really a joy to watch this one! Keep them coming! very insightful.

  • @mendodev8775
    @mendodev8775 2 роки тому +2

    I love your videos. It's really easy to chill and follow along. And whenever I'm in a hurry it's great to have access to the web article and just see the code quickly.
    Thanks so much for sharing this!

  • @pevey
    @pevey 2 роки тому +14

    I totally agree about Prisma. I often think, Am I missing something? Why doesn't every JS developer in the world use Prisma? :) I tend to work in the opposite direction as you did in this video. I set up and make changes directly in the database, and then in the project I can just 'npx prisma db pull' and then 'npx prisma generate'. Magically the DB and ORM are synced. Love it.

  • @stripeyhorse
    @stripeyhorse 2 роки тому +2

    Loving this content. It’s really helping me with sveltekit.. Your videos are so easy to follow and you explain things well..

  • @JuanmaNomad
    @JuanmaNomad 4 місяці тому

    Thank you! It was a very helpful tutorial. Loving you content 👌🏼

  • @codenx2
    @codenx2 2 роки тому

    The precious and rarest piece on earth as on date. Thanks.

  • @marlopainter8246
    @marlopainter8246 Рік тому

    Thank you for your wonderful explanations! With combined help from you, and others, I have a working authentication system on my app. I'm also integrating JWT into the cookie. I love how you explained the code as you went, and not just narrated your typing... Thank you again for your awesome Svelte videos!

  • @leboyoyo
    @leboyoyo 2 роки тому +1

    Bro I really wish I can give 10 thumbs up!!!! 👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍👍 Excellent video everything works as expected!

  • @xicor9469
    @xicor9469 Рік тому

    You are a god to me, the way you explain everything is phenomenal. Truly adds joy to my life.

  • @alexvass
    @alexvass 2 роки тому

    Thanks

  • @hrefamid9563
    @hrefamid9563 2 роки тому

    Love this! I’ve been waiting for this refresh 🤩

  • @mihaiandrei434
    @mihaiandrei434 2 роки тому

    Thank you! I was really looking into cookie auth yesterday, and found your other videos. Thanks again! You are awesome!

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      I try my best! 😎

    • @mihaiandrei434
      @mihaiandrei434 2 роки тому

      ​@@JoyofCodeDev A little question. Should the token we send as a cookie be encrypted? I saw that a lot of packages require a secret ( ex: express-session ) and I'm not sure why.

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      @@mihaiandrei434 I can't say I know the answer.

    • @mihaiandrei434
      @mihaiandrei434 2 роки тому

      @@JoyofCodeDev i did some research. You did it alright. No need to hash it😬

  • @cookifymedia
    @cookifymedia 2 роки тому

    Great video as allways. Never tried Prisma but it is on my list. Keep up the good work 💻

  • @OgyXD
    @OgyXD 6 місяців тому

    Insane video man

  • @khalilayari3230
    @khalilayari3230 3 місяці тому

    Amazing tutorial. Only thing missing is how to deal and process expiring cookies

  • @aminebouramada9500
    @aminebouramada9500 2 роки тому

    thank you bro for all you effort to explain this , you are awesome!

  • @marcello_s
    @marcello_s 2 роки тому +1

    That would be so cool to see how you would implement a login, register and logout with JWT auth, access - refresh tokens. I’m struggling with this at the moment..
    Anyway, your videos are amazing and it’s crazy how well made and concise your videos are. Thank you very much.

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому +1

      I don't know a lot about doing it with JSON Web Tokens but I wouldn't mind having a look when I have time! 😄

  • @joimeecajandab1438
    @joimeecajandab1438 2 роки тому

    love this video... thanks so much!

  • @thedelanyo
    @thedelanyo 2 роки тому

    Super info. Thanks man. I should say if role is User and user tries accessing admin, the admin server load should redirect.

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      The admin route is just an example! 😄

  • @foobar555
    @foobar555 7 місяців тому

    I would love to see an updated version of this using SvelteKit 2.0 and Svelte 5! Maybe even with JWT Tokens instead of just a uuid.

  • @ricardoreix
    @ricardoreix 2 роки тому +1

    I ended up to implement an auth system the same way than you, the only thing different is that I'm using a jwt token inside a cookie instead of a session, that way I can extract the user information in the server without having to query the database

  • @jonathanemmett8043
    @jonathanemmett8043 2 роки тому

    Fantastic, very well done man.

  • @jaydudhatra7129
    @jaydudhatra7129 2 роки тому

    Hey! Thanks so much for this video!

  • @diegoavendanohernandez9908
    @diegoavendanohernandez9908 2 роки тому

    You are great sr. please continue with sveltekit and prisma, an ecommerce would be great

  • @1roOt
    @1roOt 2 роки тому +2

    can I request a JWT auth turorial for the current sveltekit? :P With refresh token and all that? I struggle so hard implementing this. This turorial was great! Thank you very much for this!

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому +1

      I would love to but I don't know much about it and using cookies seems easier if you can.

    • @1roOt
      @1roOt 2 роки тому

      @@JoyofCodeDev thanks for the reply! I ditched my project for now and I try to use Prisma like you showed here. Seems like sveltekit is not made to work with a regular REST API. I know it can be done, but currently there is not much information and I really get confused about where to store which token and such

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому +1

      @@1roOt I would read kit.svelte.dev/docs/routing because that's what SvelteKit is made for and if you need help take advantage of the Svelte Discord server svelte.dev/chat.

  • @bearsdotzone
    @bearsdotzone 2 роки тому

    Lucia docs got updated for the SvelteKit RC. Very handy!

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому +1

      The developer is also incredibly nice and helped provide feedback! 😄

  • @chidam333
    @chidam333 2 роки тому

    Thank you for this !!! i do think adding types prisma and all this might make it hard for beginners to follow .

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      I would never show you something I wouldn't do myself and underestimate you if I skip things for convenience.

  • @djdankmemes9257
    @djdankmemes9257 2 роки тому

    9:47 literally never knew there was an issue or that there was a way to refresh the page for that issue. I thought I just didn't understand typscript....

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      This has improved but if you have problems generating the types restarting the development server should resolve the problem.

  • @uopb
    @uopb 2 роки тому

    Thank you for this update. Really gotta get going with Prisma especially now that it has mongodb support so I should supposedly just be able to generate my schemas and do the swap…

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому +2

      I might make a Prisma guide because most of what I've seen just reads the documentation to you but it's hard to understand how to model your data if you're not familiar with SQL and how it maps to Prisma.

    • @uopb
      @uopb 2 роки тому

      @@JoyofCodeDev sounds good. Trying it out with a playground project tonight.

  • @OldKing11100
    @OldKing11100 2 роки тому +4

    13:30 For those who have large forms you can Object.fromEntries(data).

  • @zoubirzarqi2731
    @zoubirzarqi2731 2 роки тому

    Super Helpful ,

  • @zzej
    @zzej 2 роки тому +3

    hooks are like middlewares?

  • @oscarvasquez5033
    @oscarvasquez5033 Рік тому

    Thanks you so much!!!

  • @kashnigahbaruda
    @kashnigahbaruda 2 роки тому

    Great stuff! Does sveltekit have anyway to use a common "load" function for all routes in the (protected) folder? Or do we still have to check on every route?

    • @aldyreal
      @aldyreal 2 роки тому

      Yes it does! Search "When to use layout groups" in SvelteKit Documentation

    • @uopb
      @uopb 2 роки тому +2

      You can and it works great most of the time but beware that the load function for a layout is not guaranteed to always run when navigating from one child route to another under the layout.
      This will be possible to have to run for every request in a not too distant future though (see linked GitHub issue). Personally I YOLO it currently and just live with it but the safe way is to do like Matt does here in this video.
      Check sveltejs/kit issue 6315

  • @TanviAravind
    @TanviAravind Рік тому

    Any upcoming video that uses oauth2?

  • @ojosh1h
    @ojosh1h 2 роки тому

    Great tutorial! Everything works except the user logout button on mobile. The logout form button does not reset the cookies.

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      What browser are you using?

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      @@ojosh1h You can leave it here and I'm going to have a look at it.

    • @ojosh1h
      @ojosh1h 2 роки тому

      I cannot add a reply it keeps getting removed

  • @annismonadjem6901
    @annismonadjem6901 Рік тому

    Hi Matia,
    Since I first visited your site I have gained confidence in your professionalism and dedication you place in your work. I have been diligently following many of your video tutorials and have learnt a ton of new skills. It goes without saying, that I am greatly indebted to you for the skills and knowledge you have thought me.
    In the last few days I have also educated myself in the possibilities offered by a host of AI driven No/Low Code platforms: ChatGPT-4, Framer, Midjourney, Feather, Notion AI, Canva AI, Synthesia, Web Flow and Figma to just name a few as they are many more. All these platforms are competing to dominate the front-and-backend technologies by people that have NO training whatsoever (while a typical developer would have dedicated years of hard work).
    The Al technology platforms as of now can produce astonishing pieces of code very fast, optimized for SEO, for Accessibility, for UX, for content, for graphics and etc etc.
    The scary part is that ChatGPT-5, currently in the making is planned for release before the end of this year and experts are saying it is going to be a game changer !
    My question to you is as follows.
    How we, who are following your video tutorials, can ever be able to make any reasonable use of the techniques you are teaching us ? How are we going to make any earning with the skills you are sharing with us ?
    Lets consider the matter rationally: absolute majority of us following your tutorials, are learning web development to make an earning. My fear is that most of your students, including myself, will have to nearly throw away all we have learnt from you and very soon start using AI platforms in order to make some/any earning. Knowledge of HTML, CSS, JS, NodeJS, Svelte, SvelteKit seems to be a relic of the past for absolute majority of developers.
    Or am I wrong ? And if you are of a different opinion, please, share with us/me how people like me - potential web developers - are to compete for earning with No/Low Code developers.
    I am asking for your kind advise on this matter. I would definitely prefer if I could be making use of my current knowledge and training in Svelte and SvelteKit. And hence my questions above are all about how I can make an earning with coding knowledge in Svelte/SvelteKit (and JS, CSS, HTML, etc) in the age of AI ?

  • @federicodifrancesco1248
    @federicodifrancesco1248 2 роки тому

    AWESOME!!!

  • @kaibe5241
    @kaibe5241 Рік тому

    Do you know how to hydrate said data with a class that is available on all pages? I am currently doing site lookups, but I want the object available on the client to be a Site object. That object contains basic information (provided by the server), but also a bunch of helper methods to get the appropriate data where required, such as site features.etc.
    I know I can use a writable store that gets shared between all components, and establish the Site object via that - but the problem is that this value should not be writable - it should be set once, and then forget about it, and remain immutable.
    Any ideas?

  • @byte__3076
    @byte__3076 2 роки тому

    Amazing video. Just a question: I have a params page that has the action to delete the user; the user is deleted successfully but using the progressive enhancement it does not redirect me, but brings me to the +error.svelte since the page itself does not exist anymore. But when I remove the progressive enhancement it works

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      Your form has to have it's own endpoint if you want to use progressive enhancement.
      For example one thing I tried in the past was having register, login and logout for one action but that doesn't work since the form can't POST somewhere else and work without JavaScript.
      If you need further help join the Svelte Discord server svelte.dev/chat.

  • @Sp3cccc
    @Sp3cccc 2 місяці тому

    21:00 cookie auth

  • @namtruong4071
    @namtruong4071 2 роки тому

    Great video.

  • @scott_itall8638
    @scott_itall8638 2 роки тому +3

    import { redirect, fail } instead of invalid.

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      Those are recent changes! 😄

    • @Friedrich713
      @Friedrich713 2 роки тому

      Thanks for sharing! You saved me

    • @scott_itall8638
      @scott_itall8638 2 роки тому

      @@JoyofCodeDev to be expected with such a young framework.

  • @thereisnoheaven
    @thereisnoheaven 2 роки тому

    I am a newbie for sveltkit and your videos have helped me a lot!! thanks much for all your great works. But I got one problem with logout from the implementation from this video. Longing out sort of works fine, it redirects to a target page, login page or home page etc, but when I click the back button, the previous page still shows up. And yeah if I reload the previous page from back button, it is gone and the login page shows up, of course it is normal because i logged out. guess it is a cache problem, but I have no idea how to resolve this problem with sveltkit. Any idea on this?

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      I don't remember having that problem but I would use an auth library like Lucia in your project since now you understand how auth works and can use it with confidence github.com/pilcrowOnPaper/lucia-auth .

  • @mavdotjs
    @mavdotjs 2 роки тому

    Nice!

  • @ahmedabdulrazzaq2015
    @ahmedabdulrazzaq2015 2 роки тому

    so handler works as middleware... in this case is it too much to make this call everytime we send a request to the server?

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      It's the safest method.

    • @ahmedabdulrazzaq2015
      @ahmedabdulrazzaq2015 2 роки тому

      @@JoyofCodeDev I was thinking about it, I couldn’t find a different solution

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      You could authenticate the user inside the layout but using hooks is safer: ua-cam.com/video/UbhhJWV3bmI/v-deo.html.

  • @bilguuntamirsoronzontugs9677
    @bilguuntamirsoronzontugs9677 2 роки тому

    is login part im trying to redirect to home page but it routing to '/?login' how to fix this?
    throw redirect(302, '/') not working

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      Try asking your question with an example on Discord: joyofcode.xyz/invite.

  • @ajipriyowibowo956
    @ajipriyowibowo956 2 роки тому

    Does prisma work with any Database, like Postgresql. Mysql etc ?

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      That's the idea! You only have to change the settings and don't have to change your database. Keep in mind SQLite might have less features than PostgreSQL like enums which is important to know.

  • @Manishchoudhary-r2e
    @Manishchoudhary-r2e Рік тому

    Which OS you are using?

    • @JoyofCodeDev
      @JoyofCodeDev  Рік тому

      Here is what I use: joyofcode.xyz/uses.

  • @killerdroid99
    @killerdroid99 2 роки тому

    I am using nest js for backend stuff, login works fine but how do I access the cookie/session data throughout the app?

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      You can do it the same way as I show you in the video.

    • @killerdroid99
      @killerdroid99 2 роки тому

      @@JoyofCodeDev yeah I have figured it out (by watching the video 2 more times 😅)

  • @GIOVANNYGARCIAHOLGUIN
    @GIOVANNYGARCIAHOLGUIN 2 роки тому

    hi friend, i have an api with express, could i use this login system too?

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому +1

      I don't know! You're only concerned if the user is authenticated or not but I don't know how it plays with an existing API.

    • @GIOVANNYGARCIAHOLGUIN
      @GIOVANNYGARCIAHOLGUIN 2 роки тому

      @@JoyofCodeDev ok, thanks

  • @boiasd1600
    @boiasd1600 Рік тому

    What VSCode theme are you using?

    • @JoyofCodeDev
      @JoyofCodeDev  Рік тому

      You can find a link to what I use in the description.

  • @ethanpiranha67
    @ethanpiranha67 Рік тому

    Hey! Thank you so much for your help! I am learning both Svelte and Sveltekit and I have one question: I didn't know about the page store ($app/stores) and was using a writeable store (with subscribe, set and update), and was setting the store within the hook, as opposed to setting event.locals as you do. I noticed that the store wasn't updating when I was subscribing to it in another page.svelte (is was still using the initial value I set in the store with the writeable() constructor despite the hook being hit before the page was load). I'm a little surprised why this isn't working, as isn't what you are suggesting in the video with using event.locals and the page store in $app/store following the same ideas? Thanks so much!

    • @JoyofCodeDev
      @JoyofCodeDev  Рік тому +1

      It doesn't work because stores aren't shared across the client and server. You can read more about state management and what to avoid in SvelteKit: kit.svelte.dev/docs/state-management.

    • @ethanpiranha67
      @ethanpiranha67 Рік тому

      @@JoyofCodeDev Thank you!

  • @sjfieksnd
    @sjfieksnd 2 роки тому

    Is this serverless?

  • @kaibe5241
    @kaibe5241 Рік тому

    Your lack of use of tab autocompletion triggers me so bad...

  • @karolus__
    @karolus__ 2 роки тому

    Not sure if SvelteKit team improved use:enhance method but it works without any additional code in layout and login page

  • @ramadhanafif
    @ramadhanafif Рік тому

    I'm confused. How does this scale when there is multiple users accessing the server? Does the locals from hooks.server.ts get shared? Or is it just add an authentication object to other pages?

  • @ResIpsa-pk4ih
    @ResIpsa-pk4ih 2 роки тому

    I think there might be a bug in your code related to $page.data.user -> it appears to be undefined and thus breaks the nav bar functionality. I was pulling my hair out on my project because I couldn't get it to work, and then I just cloned yours as a sanity check and it also appears to be broken on yours. I haven't been able to find anything about it changing in the docs, so I'm not sure what is going on. But it looks like you could potentially use LayoutServerData instead...?

    • @JoyofCodeDev
      @JoyofCodeDev  2 роки тому

      I never encountered this! Did some update happen perhaps? A reproduction might be great.

    • @karolus__
      @karolus__ 2 роки тому +1

      1. user.role is if fact an object so passing user.role.name to locals is important:
      event.locals.user = {
      name: user.username,
      role: user.role.name
      }
      2. if you're using typescript it's better to use Page Data like this to have full types support:
      import type { PageData } from './$types'
      export let data: PageData

  • @colinhowells
    @colinhowells 2 роки тому +3

    if you had a donation method (YT/buymeacoffee/etc), i would use it to show my appreciation - thanks for putting these together