I just have a huge number of questions ... But before that you need to describe what is actually happening.
Preamble
I'm trying to teach Angular2 to log in to the Rails 5 beta 3 application. With this, Rails runs in API mode. As I understand it, in order for Devise to work in API mode, you need a gem devise_token_auth . Heme I installed and configured, but after that the problems started:
Unpermitted parameters: user, session
The first thing I tried to do after setting up, send a simple POST request and see what happens. And the following happened:
Processing by DeviseTokenAuth::SessionsController#create as *json* Parameters: {"user"=>{"username"=>"demo", "password"=>"[FILTERED]"}, "session"=>{"user"=>{"username"=>"demo", "password"=>"[FILTERED]"}}} Unpermitted parameters: user, session Rendered devise_token_auth/sessions/create.json (0.2ms) Completed 401 Unauthorized in 18ms (Views: 3.2ms | ActiveRecord: 0.0ms) And from now on, I don’t understand a bit, since when should I prescribe the permit for the parameters that ActionController::ParamsWrapper dynamically creates as a wrapper? Moreover:
devise_parameter_sanitizer.permit(:sign_in) do |user_params| user_params.permit(:user, :session) end does not change anything. Has anyone encountered this before?
Authentication header
Having rummaged in the documentation in search of a solution to the problem, I found another:
Header of each request. The headers follow the RFC 6750 Bearer Token format
Those. The request header should contain the following data:
"access-token": "wwwww", "token-type": "Bearer", "client": "xxxxx", "expiry": "yyyyy", "uid": "zzzzz" In AngularJS, they were generated by the ng-token-auth module, but how do I generate them using Angular2, which does not have this module?
Summing up the interim result (because I feel, after these problems I stumble upon even more of them), here is a list of my questions:
- How to fix
unpermitted parameters: user, sessionindevise_token_auth? - How to generate
access-token? - How to calculate
expiryand is there a "rails-way" for the token lifetime? - What should I write in the
client? Since this identifier allows the user to simultaneously log in from multiple devices, what should he look like? What should be written there?
- What data should be returned to the client after authorization (besides
200)? - What other pitfalls can wait for me when I try to log in through the Rails API?
UPD
Here is the request that I send:
private _user_session_url = 'auth/sign_in'; login (username: string, password: string) : Observable<User> { let body = JSON.stringify({ username: username, password: password }); // TODO: generate header data. let headers = new Headers({ 'Content-Type': 'application/json', 'accept': 'json' }); let options = new RequestOptions({ headers: headers}); return this.http.post(this._user_session_url, body, options) .map((data: any) => data.json()) .catch(this.handleError) } As you can see, the session in the request body is not. It is created by ActionController :: ParamsWrapper
In confirmation, I quote Request Payload:
{"username":"demo","password":"123"}