Let's Build: With Ruby on Rails - Extending Devise Series - Confirmation Emails

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey all welcome to another installment in the series within a series I'm calling let's build with Ruby on Rails and then extending devised so we're gonna be extending the devise Jim in particular in this group of videos so for here there's probably other videos either coming or have already been recorded in this playlist definitely check each out if you want to learn more about how to extend divides itself this guide I had used my kickoff Tailwind github repo to create this as a practice app but I'm gonna actually create it from scratch without that and this guide just to show you how to install the vies in particular and then just configure it from there so we'll actually use devise Jim in particular manually you still have to set it all up and everything from scratch just in case you weren't clear of how to do that because my kickoff template actually does that by default from the start so I wanted to be thorough this guide will be essentially digging into sending out an email a confirmed email that basically says when the user signs up they need to confirm their email address before they can enter the site there are ways you can actually have it to where the user doesn't have to confirm before they enter but they still get sent that email but I think it's in terms of security and just best practices probably the best approach is to go ahead and send it and only allow them in until they've actually confirmed so to put this to practice I'll just sign up and I have mail cat you're running on the side so this is the Jim I use that actually hooks into the app and will fire off an email locally that it will actually send like it really does in production to kind of give you that emulation of the real thing happening so actually sign up now and hopefully get an email we do we also get this new message because we have this confirmable option set with our device configuration so it goes and says a message with the confirmation link has been sent to your email address please follow the link to activate your account so to get that obviously ignore this email we'll need to update that but this is all it is in the end it's got an confirm leak if you look in the bottom left there's an actual token that gets generated which is kind of a security practice to generate this hash that goes to the URL so it matches the app on the end if those two keys are tokens match then it lets you in otherwise it does not so I'll go ahead and click that and then we're redirected to what I made just this basic dashboard layout so well kind of mimic this maybe not the dashboard portion but more or less just getting that confirmation step in and so it's not something that comes with divides by default you have to actually configure it to do so so what I'll do is kind of create a brand new vanilla rails app and then start from there by installing device so I'm gonna go get the device Jim I like to just reference it during install just so you can see it here it is so there's a general set up probably like way down getting started here so what I want to do is create a new app so I'm gonna go to my site's directory in there I'll just do a rails new device confirm example say confirm a bowl and that's it so I'm gonna just go run a brand new rails app it's not gonna have any of my templates or anything you could pass that and bypass some of this set up but I'll leave that up to you to do so create that and wait a little bit right so it installed we can go ahead and enter the app I already forgot the name of it already so let me figure out what it was my brain Wow look at all that stuff okay confirmable example so I'll just CD into that there we go we're in our app open this in Visual Studio code it's just on the other window here probably put it in a different window so here's the new app we haven't really initialized it with get so you see everything's green over here so the nothing's been added at this point we got a basic rails 6 app in the making right here so device the first thing I want to do is actually install the vies so actually you head to I'm gonna go to the rubygems site just to get the latest version I like to get it just the names have the the specific version and so instead of just the general gym install this and this is what I mean like on their on their docks they have just Jim devised and I want this kind of squiggly line stuff by running bundle install it should go fetch out a couple things it needs as well as the main gym so good to go they're great so with device comes a bit of configuration it it does come with this generate or what they call a generator that you can run and it creates a configuration file as well as in migration file that we'll need and then from there will generate more for device to work so we'll run rails generate devise install you'll notice it takes on setup here we'll need to configure it to use this as the mailers since device does send mailers as we saw in the initial setup there so I'll kill the Jim file I'll go into config environments and development I already know where this is supposed to go just in this file this basically just says any mailers should be sent on our localhost instead of some predefined service or something out there in the wild since it is a local development environment so configure environments development so close that file what's next we need a route path so what I'll do is just generate maybe eight what did I do for the example app I think it was a dashboard yeah so let me create just a general controller suppose just a controller dashboard and index so saying the controller name is gonna be dashboard and then we'll have an index action on that it's just gonna be a static page that will end up being my root path in the end so it just does a bit of config adds the controller adds a new route which we'll configure here in a second and then index action so that means a view is generated it's called index.html a few test files helper files and then assets file we don't really need to worry about so I'm back in our code got this new controller dashboard controller with an index action on it which means I can essentially set up my route path to that if I want I could have maybe just another random action to that's just a home so because basically in the end I want the confirm user to be logged in and and actually visiting this path so the authenticated users should only be able to visit this so I'll just say authenticate this is part of devise so here you could pass multiple actions so in our case only index but you could also just say nothing here or you could say accept and you're just basically telling what actions or paths to authenticate so basically is devise does this user needs to be logged in when they visit this path so often you'll see like edit or destroy stuff like that here but in terms of authenticating a user I want them not to be able to visit this path entirely no matter the resource in the routes so instead what I'll actually do is update our routes speaking of so this is gonna just put a basic get request and I just want to do a basic resources I just I like doing this more for some reason so dashboard I think it's plural dashboard and then just only index here and as well so that'll just say just only make this route actually the one that's inner routes in particular in the app so what we could do is boot up the server let me kill the other one real quick go to this one and I'll say rails server and we'll see what happens with the routing just to give you a high-level overview of what's different here so here I have other routes that is just not the root path so we don't have a route defined so if we were to go to dashboards or info routes excuse me it's just like a nice place to see all your routing we only have that index path because I've only defined that in my routes so if you just say only it'll go ahead and just generate that one only if I save this and go back to this page all these other ones show up we don't want all those because I really don't need them at this point maybe in the future but not right now so that's a little aside there so in this case we do need another actual controller save for the home page so I'll just actually create a home controller to just makes a little easier so I'll just say rails generate controller home and then index as well and that's good to go so for the home we'll go back to here the routes think I'm in the right directory right looks like I had this like unsaved so let me go to this file again routes let's close this for now and go back to it there we go and what I want to do is the same for just home so we'll just say home controller only UNIX great so that's all we need for the routing that just says we're gonna have a route that's home index and then dashboard index so the dashboard is gonna be what's authenticated and the home index a will be public so we'll set the route path to that actually in this file too so let's say route and then to home index you can do it like this and typically you'd have to restart the server but I think we're good not to we'll see in a minute yeah I don't even have it running so that's one problem let me boot that up okay and this should go to just a blank screen of home index now just as your in home index there we go cool so that's our default rails install and just some basic routing and actions and paths so now we need to make devised to kind of do all the magic about with confirmable so when I did do the devise install we had some more instructions to add and we double checked we should have now a config file that's in the devise initializers so in the initializers and then devise we should have more stuff to configure here here's that email date they say change so they be able to say Andy at example for now this will be what the sender email will be in the end you can always always override this in your mailers that's something I really haven't talked a lot a lot on this channel is an active mailer actually mailer I can't remember the name and how to configure that and just send emails with the apps Ruby on Rails I should say I think that's the main room when we want to configure there are is a setting to allow the user to not be able to reconfirm so if they were to get their first confirm confirmation email if they were to go to the site the actual path that says Oh login but I didn't get my confirmation email so we get sent their email again we can set that off so I'll just go ahead and do that so one thing I overlooked is getting the device views into practice here so actually go to rails see if there's any commands I can run your rails generate if we do rails generate but don't pass anything I think we could see what we can generate yeah the vis--vis I'll say rails generate buys views that'll help us get some actual HTML on the picture and that all goes into a devise directory and our views so I'll go look at that real quick so we should have this now this all just got added and we could go check that out in the site here still server probably help if I did two windows here but I'm spawning to be clear so I'll boot this server we'll have our index action of home but we can also go to users sign up now or what is it not unit users oh we need one more thing devise needs its own routing mechanism so we need to add that too so let's add go to the docs real quick actually we'll do this we didn't do this bit yet so we need to say rails generate devise and then pass in the model we want devise to actually pertain to so in most cases it's gonna be a user model it could be anything from an account model or a profile etc I think user is probably the most standard I've seen so maybe roll with that so we'll just generate this and I'll create a new model that's our user model so you don't go you don't need to generate your own model before this it'll create that migration file which we'll need and all reference in a second then some unit tests as well as add that that bit before the routing I just mentioned that I overlooked so we can go check out our routes you notice this has been appended so that's great now we can actually go to that path I was talking about once the server's booted rails server and it's actually gonna ping us through run migrations yeah so if you are new to rail 6 this is a new feature the button to run pending migrations so we can actually just run them in place which is pretty fancy that seems like a feature that should have been there from the start but whatever so here's the basic primitive login form we've got our email or password confirmation you just signup there's no user name field or anything else like my other template kind of generates for you if you use that so it's just email password and confirmation to sign up but in doing so this would just create an accountant and you would just redirect back to wherever probably the root path in this case by default but since we're doing that confirmable bit we actually want to go ahead and pin that to our model and kind of tell the devise that we do want that action to take place so we've got that user model in our app models directory now so we can actually uncomment this one in particular confirmable you notice there's quite a few others as you can figure like Omni off the bowl that's one I'll probably use in the future these other ones are kind of neat lockable like so many accounts can get locked if they're mistreated or something's wrong with them trackables one that's I don't know if I would maybe enforce that you're tracking a lot of data about the user and I think the new law is kind of prohibits some of that but I could be wrong aside from the defaults here we're gonna actually add confirmable to the mix so one more thing before you go and just you know generate a new user remember that migration that was added and it's the only one right now so here's if you generate that it's a brand new devise install you notice some comments here and it's actually pretty handy because all you need to do is just uncommon thiis and basically it's set up to rock and roll and since we turned that reconfirm herbal to false I'm gonna actually comment this last one off so I don't want users to be able to reconfirm so essentially with the confirmable option we're adding all these other fields plus these three so a string for the confirmation token which gets generated when the email is sent and appended to that link that allows the token to talk to each other and just be in sync when the user actually clicks the link and heads the actual app from the email it confirmed at time just as a date time and then it confirms sent at as a date time property so that's all this all gets generated on the user's table so we can actually run that migration now so I'll go ahead and do that in fact I'm gonna go to a new tab just to keep the site's the go to a new session or new profiles okay so within here I'll just run rails DB migrate actually I think I did not save that or did I I have the schema now and we do have a users bit there oh you know what I ran the migration initially so it we've kind of gone ahead we got a little too trigger-happy so I'm gonna actually undo rails DB rollback my initial migration and we've got the whole users table dropped at this point so there's nothing in our schema I'm gonna rerun this migration with these new to our new three parameters uncommitted so now I'll run rails DB migrate and those should actually enter the user model now and our schema so there we go let's go back to our migration to just our schema just a double check and now we do have those fields perfect so if you press that button like I did earlier you might need to roll back and do this again okay so our routes are set up our devise configuration is set up there's tons more you could do that but we'll leave it alone for now and one thing we'll need to add is a new controller to the mix so we'll need to modify our routing here to include our own controller instead of the default one that comes with device so the reason for that is after the user confirms their account and they enter the app I want them to be able to sign in and then also be redirected after that to a specific place so that will be where that dashboards controller comes in that's what will redirect that user so what needs to happen there is we need to tell it what path to go once that link is clicked in the email so in our case we need to add controllers and then within that confirmations will be called confirmations so if that isn't quite clear it basically says the app is going to look for a controller and the controllers path here that's called confirmations controller you can call this whatever you want but I tend to name it the same thing as what devise passes here so there's actually a lot more you can pass here definitely reference the docs we might talk about more of these in the future as we extend devise further but essentially we need to add a new controller at this point so I'll just call it confirmations controller and this one's gonna look a little different than the other ones so here's what a basic controller looks like but in this case we need to inherit from the devise Jim so our class will look a little different since it will say confirmation controller and we'll hair it from devise itself but the subclass of confirmations controller so its owns controller and then we just have a private method that's only accessible within this class that basically says if when the user is confirmed what we want to enter this method after confirmation path for and we'll pass in the resource name this is all from the devise routing so we get information on the specific resource that gets passed through so then we could say sign in as that user based on that hash that comes back that confirmation token and then we can redirect back to the dashboards paths you don't even have to said redirect or anything here you could just say dashboards path so that's gonna go to the index that we created as the users signed in so once they're signed in if you remember we're checking if they're signed in here before anyone can enter this action and then they're able to see more about that dashboard so in essence that's the bulk of the work to get this to work you could try this now and do know I need to install the mail catcher bit to work on this app so I'm gonna go to the mail catcher site I think it's mail catch you're it's not me yeah there's a bit of configuration we need to add for a rails project in our development environment so just pay copy and paste this bit here go back into config environments development and just below the one we added for divides we can add this for the actual delivery method so this is basically faking a SMTP sending of an email using our another local port of 1025 which is what mail catcher runs on and then if you don't have mail cut you're already running I do in another window it can run by default in the background so you needn't worry there but if you just run mail catcher like that it should work of course you do need - actually installed male catcher it's a it's a global gym so you can just say gym install male catcher if you don't already have it should install you can run that command you should be good to go and then you just visit this address 1080 and you'll see the stuff you won't see anything probably if you haven't sent me miles local yet but you will see it shortly once we start sending stuff so in essence we can go and maybe reboot our rails server just to be sure everything's set up and one thing I did forget to actually do is create notices in the views so we can say application notice I think alert that's all we need there I'm gonna double check that that doesn't fail it's been a minute since I've actually configured devise I usually have a premade solution yeah sure we should be good okay so in this case now we can just send or go to our users path so users sign up I'm gonna enter an email and then password I'll hit sign up there we go there's that notice and then if you look in male catcher we got that it comes from a knee example per arc devised configuration we get the basic default confirmation instructions here and then we could say confirm my account we'll go back to the app and it looks like I should have dashboard app there or dashboard path excuse me my fault but refresh there you go even levels already confirmed so we've already confirmed since we visited that path so as technically we're good to go if I go back to localhost 3000 and I could go to dashboard should be signed in okay so if I signed in now Andy at web Crunch password there we go so now we're at the dashboard at this point so we were able to create our account we got confirmed confirmation and everything's working great so you see that in practice in a lot of apps out in the wild it's a good step to implore I think it's one that's a little more configuration but honestly it's worth it so you see this is the request that went through when we did all that stuff in the logs it's really good to check your logs just to see what it's all happening if you can't figure out bug here's that confirmation token from before that gets generated with device so you might be wondering well how do I customize that email and you can go in to devise mailer confirmation instructions and modify it right here so you'll be able to have access to that if you want to change the look and feel this there's a few gems out there that help with emails foundation for emails is one or there's some third-party stuff that you could check out but essentially this is the HTML version so that I think that sums up what this tutorial is going to cover future stuff will probably be a little more involved in terms of just getting devise to do a lot more magic maybe I'm the auth and stuff like that logging in with social media apps etc so look forward to that and I hope you learned something with this one all right I'll see you the next hello rails is my new course on Ruby on Rails I'll teach you Ruby on Rails from the ground up visit hello rails IO for more information [Music] you
Info
Channel: Web-Crunch
Views: 6,556
Rating: undefined out of 5
Keywords: ruby on rails, devise, devise gem, ruby, rails, ruby 2019, rails 2019, ruby on rails tutorials, learn ruby on rails, how to use ruby on rails, rails tutorial 2019, rails tutorials, web development, web design, web app framework, coding tutorials, learn to code, how to code 2019, web dev tutorials, devise confirmable, confirmable, confirmable devise, devise gem 2019, devise gem tutorials, devise gem rails, sending emails with rails, ruby on rails action mailer, action mailer
Id: D_50cO1TO0A
Channel Id: undefined
Length: 26min 13sec (1573 seconds)
Published: Sun Jun 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.