You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/5-authorization_authentication/devise.md
+71-4Lines changed: 71 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,15 +59,15 @@ class ExampleController < ApplicationController
59
59
end
60
60
```
61
61
62
-
## Devise Login and Logout
62
+
## Devise Login
63
63
64
64
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.
65
65
66
66
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.
@@ -78,21 +78,88 @@ class Profile::Pages::SignIn < Matestack::Ui::Page
78
78
button text:'Login'
79
79
end
80
80
end
81
+
toggl show_on:'login_failure'do
82
+
'Your email or password is not valid.'
83
+
end
81
84
end
82
85
83
86
private
84
87
85
88
defform_config
86
89
for::user,
87
90
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.
# include your component registry in order to use custom components
126
+
includeComponents::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
+
defnew
132
+
render Profile::Pages::Sessions::SignIn
89
133
end
90
134
91
135
end
92
136
```
93
137
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:
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
+
96
163
## Example
97
164
98
165
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