![]() ![]() pipeline :auth do plug :basic_auth, pile_env( :calendlex, :basic_auth )Įnd live_session :private, on_mount: Ĭonst originalInnerHTML = this. lib/calendlex_web/router.ex defmodule CalendlexWeb.Router do use CalendlexWeb, :router import Plug.BasicAuth But how can we protect a bunch of live routes using this approach? Let's jump to the router file and find out: #. In this particular case, we will use basic HTTP authentication for the sake of simplicity. It receives the session data and LiveView socket, and should generate the necessary. Therefore, we must use an authentication mechanism to protect all the admin routes. mount/2 will be invoked when the live view is rendered from the controller. Since this section of the application will expose sensitive data, we don't want to make it accessible to anyone not authorized. Let's get cracking! The private admin scope In this part, we are going to start implementing the private side of our application, in which we will be able to manage the existing event types and create new ones. We also implemented the booking form, which schedules a new event for the selected event type, date, and time slot. In the last part of the series, we generated the available time slots for a given data and rendered them as clickable items in the calendar's live view. Generating the initial project and domain models.This is an opportunity that live_session doesn’t waste: it takes a :root_layout option to let you specify the root layout for the member LiveViews all at once.This post belongs to the Building a simple Calendly clone with Phoenix LiveView series. This prevents the root layout from changing, which you should keep in mind when grouping LiveViews into live sessions!Ĭonversely, putting routes into different live sessions forces a page reload through the plug pipeline on navigation between them. Navigating among live routes within a single live session lets you avoid the overhead of a full page reload. Using a different root layout for grouped like routes Plugs/pipelines to secure web requests, and hooks to secure each of the routes in the session, on the LiveView’s mount. In order to secure all the routes in the / scope, we applied both security mechanisms we mentioned earlier. Live_session :admin, on_mount: do live "/users/register", UserRegistrationLive, :new live "/users/log_in", UserLoginLive, :new live "/users/reset_password", UserForgotPasswordLive, :new end post "/users/log_in", UserSessionController, :create end In our router.ex file, we define a live_session and give it an atom as a name: Let’s start learning how to group different live routes using live_session. Wouldn’t it be better if we could switch between LiveViews without making any HTTP requests (saving a couple ms in the process)? That’s when live_session comes into play! If we look at our iex console, each of these steps is described in the logs: Which means that a full page reload is being done. Then, a connection with the server is established and the LiveView to be rendered is mounted. When we navigate between different pages within our application using LiveView, every time we want to mount a new root LiveView, an HTTP request is made. ![]() Today we’ll take a closer look at this feature and see that it has more going for it than is immediately obvious. ![]() ![]() We can navigate between the routes in the same session over the existing websocket, without any additional HTTP request, thus making navigation between live routes even faster!īut live_session also has some other interesting ramifications. Under the umbrella of LiveView navigation we have the live_session/3 macro to group live routes into live sessions. Behind LiveView’s magic, there are a bunch of design decisions, but also interesting features we can use. Phoenix LiveView often makes us feel like “wow, that was really fast!” and that is not a coincidence. If you want to deploy your Phoenix LiveView app right now, then check out how to get started. This is a post about the benefits we can get from using live_session, and the super powers we get from combining it with hooks. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |