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
Is this all so that we can have a different set of behaviours for API users (vs. browser users)?
205
+
206
+
8. Override API SessionsController
207
+
208
+
This controller responds with json by default, signs in the user and returns the jwt token. I'm guessing that this sign in process is what allows the token to be used transparently and what allows `current_customer` to be set so other controllers just work?
209
+
210
+
```
211
+
class Api::SessionsController < Devise::SessionsController
212
+
# I'm guessing this isn't required since we don't track signed in/signed out status for the API user?
213
+
skip_before_action :verify_signed_out_user
214
+
# This sets the default response format to json instead of html
215
+
respond_to :json
216
+
# POST /api/login
217
+
def create
218
+
unless request.format == :json
219
+
sign_out # why is this needed?
220
+
render status: 406,
221
+
json: { message: "JSON requests only." } and return
222
+
end
223
+
# auth_options should have `scope: :api_customer`
224
+
resource = warden.authenticate!(auth_options)
225
+
if resource.blank?
226
+
render status: 401,
227
+
json: { response: "Access denied." } and return
228
+
end
229
+
sign_in(resource_name, resource)
230
+
respond_with resource, location: after_sign_in_path_for(resource) do |format|
231
+
format.json {
232
+
render json: { success: true,
233
+
jwt: current_token,
234
+
response: "Authentication successful" }
235
+
}
236
+
end
237
+
end
238
+
239
+
private
240
+
241
+
def current_token
242
+
request.env["warden-jwt_auth.token"]
243
+
end
244
+
end
245
+
```
246
+
9. Add “new” view in json format
247
+
248
+
If this file isn't added, the follow error is generated when attempting to login:
249
+
250
+
```
251
+
undefined method `api_customers_url' for #<Api::SessionsController:0x00007fb9ded22298> Did you mean? api_customer_session_url
0 commit comments