{"action":"closed","issue":{"url":"https://api.github.com/repos/lynndylanhurley/devise_token_auth/issues/938","repository_url":"https://api.github.com/repos/lynndylanhurley/devise_token_auth","labels_url":"https://api.github.com/repos/lynndylanhurley/devise_token_auth/issues/938/labels{/name}","comments_url":"https://api.github.com/repos/lynndylanhurley/devise_token_auth/issues/938/comments","events_url":"https://api.github.com/repos/lynndylanhurley/devise_token_auth/issues/938/events","html_url":"https://github.com/lynndylanhurley/devise_token_auth/issues/938","id":249178016,"number":938,"title":"Token generation fails when user has incomplete data that is mandatory","user":{"login":"heridev","id":1863670,"avatar_url":"https://avatars0.githubusercontent.com/u/1863670?v=4","gravatar_id":"","url":"https://api.github.com/users/heridev","html_url":"https://github.com/heridev","followers_url":"https://api.github.com/users/heridev/followers","following_url":"https://api.github.com/users/heridev/following{/other_user}","gists_url":"https://api.github.com/users/heridev/gists{/gist_id}","starred_url":"https://api.github.com/users/heridev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/heridev/subscriptions","organizations_url":"https://api.github.com/users/heridev/orgs","repos_url":"https://api.github.com/users/heridev/repos","events_url":"https://api.github.com/users/heridev/events{/privacy}","received_events_url":"https://api.github.com/users/heridev/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2017-08-09T22:15:54Z","updated_at":"2017-10-07T18:35:43Z","closed_at":"2017-10-07T18:35:43Z","author_association":"NONE","body":"As far as I know all the versions for `devise_token_auth` use `self.save!` when a new token is generated within `create_new_auth_token`, the problem with that occurs when you have custom validation rules, for instance:\r\n\r\nUser schema:\r\n```\r\nUser(id: integer, email: string, first_name:string, last_name:string)\r\n```\r\n\r\nValidation rule:\r\n```\r\nvalidates_presence_of :first_name\r\n```\r\n\r\nSo any user created previously(before adding the validation rule) and that did not have `first_name` will have authentication problems because in every place that you invoke `current_user` or those sort of helpers that internally calls `create_new_auth_token` will not create a valid token.\r\n\r\nRight now the way we solve this authentication issue for those kinds of users is skipping validation rules with the usage of an attr accessor, as an example:\r\n```\r\n#user.rb\r\nvalidates_presence_of :first_name, unless: :skip_validation_name\r\n```\r\n\r\nWithin the controller(eg: sessions_controller.rb)\r\n```\r\n@resource.tokens[@client_id] = {\r\n  token:  BCrypt::Password.create(@token),\r\n  expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i\r\n}\r\n\r\n@resource.skip_validation_name = true\r\n@resource.save!\r\n```\r\nand the other way is deactivating any validation rules overriding the original method `create_new_auth_token(client_id=nil)` and using `validate: false`:\r\n\r\nWith something like this:\r\n```\r\n    self.tokens[client_id] = {\r\n      token:      token_hash,\r\n      expiry:     expiry,\r\n      last_token: last_token,\r\n      updated_at: Time.now\r\n    }\r\n\r\n    self.save!(validate: false)\r\n\r\n    return build_auth_header(token, client_id)\r\n```\r\n\r\nDo you guys know any other approach to solve these sort of issues?"}}
{"actor":{"display_login":"zachfeldman"}}