Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Registration - "email address already in use" when it is unique #230

Closed
johnzw89 opened this issue May 1, 2015 · 3 comments
Closed

Comments

@johnzw89
Copy link

johnzw89 commented May 1, 2015

Hello,

I started with just devise and now am integrating this gem and ng-token-auth. I got sign in working, but for sign up, the API constantly returns email is not unique, when I can create a new user in the console with exact credentials.

UserRegistrationsController registration form submit function

$scope.submitRegistration = function (credentials){
      credentials.password_confirmation = reg_form.password;
      credentials.provider = 'email';

      $auth.submitRegistration(credentials).then(function(resp) { 
      console.log(resp);
    }).catch(function(resp) { 
      // handle error response
      console.log(resp);
    });
}

I am customizing the registration controller because I need to gate users with a code for private beta release, so I set a custom route to point to the controller:

routes.rb

namespace 'api' do
    scope 'v1' do
      mount_devise_token_auth_for 'User', at: 'auth', defaults: {format: :json}, controllers: {
        ....
        registrations:      'token_auth_override/registrations', #CUSTOM
        ....
      }
    end
end

I am setting the strong params in the application_controller, as recommended in the docs both here and devise:
application_controller.rb

class ApplicationController < ActionController::Base
  include ActionController::MimeResponds
  protect_from_forgery

  respond_to :html, :json
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
     devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:alias, :invite_code, :first_name, :last_name, :email, :password, :password_confirmation, :bio, :youtube_url, :personal_url, :twitter_url, :facebook_url, :user_profile_pic, :provider) }
  end
end

registration request from the client hit this controller in
token_auth_override/registrations_controller.rb

class TokenAuthOverride::RegistrationsController < DeviseTokenAuth::RegistrationsController
    include DeviseTokenAuth::Concerns::SetUserByToken
    before_action :configure_permitted_parameters, if: :devise_controller?

  respond_to :json
  def create

    invite_code = SignUpCode.find_by_code_string(params[:invite_code])
        if invite_code && invite_code.active?
            @user = User.new(
                email: params[:email],
                first_name: params[:first_name],
                last_name: params[:last_name],
                provider: params[:provider],
                password: params[:password],
                password_confirmation: params[:password_confirmation]
            )
            @user.level = invite_code.level

            if @user.save
                # sign user in
                sign_in(@user)
                invite_code.increment
                respond_to do |format|
                    format.json { render json: @user }
                end
            else
                render json: {
          status: 'error',
          errors: @user.errors,
          data: @user,
          valid: @user.valid?
        }, status: 403
            end
        else
            # message that code is invalid
            respond_to do |format|
                    format.json { render json: {message: 'Invite code invalid'}, status: :precondition_failed }
            end
        end
  end
end

json response looks something like this:

data: {id: null, email: "test12312@test.com", created_at: "2015-05-01T23:21:04.492Z",…}
errors: {email: ["This email address is already in use"]}
status: "error"
valid: false

Oddly, if i remove include DeviseTokenAuth::Concerns::User in my user.rb model, I do not run into this error. I'd be happy to provide any additional snippets.

Thanks in advance for any help/thoughts. Been struggling to get this working for a while now...

@johnzw89
Copy link
Author

johnzw89 commented May 6, 2015

going to close this since I've gone with the path of building oauth functionality from scratch

@johnzw89 johnzw89 closed this as completed May 6, 2015
@07502746869
Copy link

$scope.submitRegistration = function (credentials){
credentials.password_confirmation = reg_form.password;
credentials.provider = 'email';

  $auth.submitRegistration(credentials).then(function(resp) { 
  console.log(resp);
}).catch(function(resp) { 
  // handle error response
  console.log(resp);
});

}

@07502746869
Copy link

Reshwanraza22@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants