Skip to content

Commit a60112b

Browse files
author
Nils Henning
committed
[WIP][TASK] update devise guide
1 parent 89189d6 commit a60112b

File tree

1 file changed

+71
-4
lines changed
  • docs/guides/5-authorization_authentication

1 file changed

+71
-4
lines changed

docs/guides/5-authorization_authentication/devise.md

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ class ExampleController < ApplicationController
5959
end
6060
```
6161

62-
## Devise Login and Logout
62+
## Devise Login
6363

6464
Using the default devise login views should work without a problem, but they will not be integrated inside a matestack app. Let's assume we have a profile matestack app called `Profile::App`. If we want to take advantage of matestacks transitions features (not reloading our app layout between page transitions) we can not use devise views, because we would need to redirect to them and therefore need to reload the whole page. Requiring us for example to implement our navigation twice. In our `Profile::App` and also in the our devise sign in view.
6565

6666
Therefore we need to adjust a few things and create some pages. First we need a custom sign in page containing a form with email and password inputs.
6767

68-
`app/matestack/profile/pages/sign_in.rb`
68+
`app/matestack/profile/pages/sessions/sign_in.rb`
6969
```ruby
70-
class Profile::Pages::SignIn < Matestack::Ui::Page
70+
class Profile::Pages::Sessions::SignIn < Matestack::Ui::Page
7171

7272
def response
7373
heading text: 'Login'
@@ -78,21 +78,88 @@ class Profile::Pages::SignIn < Matestack::Ui::Page
7878
button text: 'Login'
7979
end
8080
end
81+
toggl show_on: 'login_failure' do
82+
'Your email or password is not valid.'
83+
end
8184
end
8285

8386
private
8487

8588
def form_config
8689
for: :user,
8790
method: :post,
88-
path: user_session_path
91+
path: user_session_path,
92+
success: {
93+
transition: {
94+
follow_response: true
95+
}
96+
},
97+
failure: {
98+
emit: 'login_failure'
99+
}
100+
end
101+
102+
end
103+
```
104+
105+
This page displays a form with a email and password input. The default required parameters for a devise login. It also contains a `toggle` component which gets shown when the event `login_failure` is emitted. This event gets emitted in case our form submit was unsuccessful as we specified it in our `form_config` hash. If the form is successful our app will make a transition to the page the server would redirect to.
106+
107+
In order to render our sign in page when someone tries to access a route which needs authentication or someone visits the sign in page we must override devise session controller in order to render this page. We do this by configuring our routes to use a custom controller.
108+
109+
`app/config/routes.rb`
110+
```ruby
111+
Rails.application.routes.draw do
112+
113+
devise_for :users, controllers: {
114+
sessions: 'users/sessions'
115+
}
116+
117+
end
118+
```
119+
120+
Override the `new` action in order to render our sign in page.
121+
122+
`app/controllers/users/sessions_controller.rb`
123+
```ruby
124+
class Users::SessionController < Devise::SessionController
125+
# include your component registry in order to use custom components
126+
include Components::Registry
127+
128+
matestack_app Profile::App # specify the corresponding app to wrap pages in
129+
130+
# override in order to render a page
131+
def new
132+
render Profile::Pages::Sessions::SignIn
89133
end
90134

91135
end
92136
```
93137

138+
Finally we need to override the create method in order to fully leverage matestacks potential. Matestack expects to retrieve a json response with a html error code if the sign in has failed due to matestacks form error handling. To achieve this we need to override the `create` method as you can see below:
139+
140+
```ruby
141+
def create
142+
self.resource = warden.authenticate(auth_options)
143+
return render json: {}, status: 401 unless resource
144+
sign_in(resource_name, resource)
145+
respond_with resource, location: after_sign_in_path_for(resource)
146+
end
94147
```
95148

149+
We stayed as close to devise implementation as possible. The important part is line 3 where we return a json response with error code 401 if warden couldn't authenticate the resource.
150+
151+
**Wrap Up**
152+
That's it. Now you have a working fully integrated login with devise and matestack. All we needed to do was to create a sign in page, update our routes to use a custom session controller and override two methods in this controller.
153+
154+
## Devise logout
155+
156+
----
157+
TODO devise logout, registration etc.
158+
----
159+
160+
161+
162+
96163
## Example
97164

98165
This is just your average Rails user controller. The `before_action` gets called on initial pageload and on every subsequent AJAX request the client sends.

0 commit comments

Comments
 (0)