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

How to create(Sign Up) a new user? #33

Open
EchoLiao opened this issue Jun 7, 2015 · 20 comments
Open

How to create(Sign Up) a new user? #33

EchoLiao opened this issue Jun 7, 2015 · 20 comments

Comments

@EchoLiao
Copy link

EchoLiao commented Jun 7, 2015

Why `Sign Up' is one GET methods? I test it in Postman, it result in errors.
I don't known how to get start. Please help.

screen shot 2015-06-07 at 11 21 54 am

screen shot 2015-06-07 at 11 25 33 am

But, I known how to create product, and it created successfully.
screen shot 2015-06-07 at 11 45 13 am

@EchoLiao
Copy link
Author

EchoLiao commented Jun 7, 2015

I found the reason. It should POST http://api.myapi2.dev/users. The URI /users/sign_up(.:format) is to request the sign up form page.

screen shot 2015-06-07 at 5 12 18 pm

_But_ there is a new issue. After I submit by click the Send button, it can create new user successfully in database, but Postman still result in errors. Why?

screen shot 2015-06-07 at 5 12 30 pm

screen shot 2015-06-07 at 5 12 44 pm

@kurenn
Copy link
Owner

kurenn commented Jun 8, 2015

You should post to the users controller create action, do you have an example on how it looks like?

@EchoLiao
Copy link
Author

EchoLiao commented Jun 8, 2015

     api_users POST   /users(.:format)                       api/v1/users#create {:format=>:json, :subdomain=>"api"}
     user_registration POST   /users(.:format)            devise/registrations#create

Yes, but the URI /users has two match. It seem to match with the first one. I don't known why.
What does example mean?

@kurenn
Copy link
Owner

kurenn commented Jun 8, 2015

You can remove the devise routes, they are not being used

@EchoLiao
Copy link
Author

EchoLiao commented Jun 8, 2015

I can't remove it directly. Because i add devise email confirmation for register.
It add three routes:

       user_confirmation POST   /users/confirmation(.:format)          devise/confirmations#create
   new_user_confirmation GET    /users/confirmation/new(.:format)      devise/confirmations#new
                         GET    /users/confirmation(.:format)          devise/confirmations#show

The first one is need. Is there a way to remove the useless devise routes only?

@kurenn
Copy link
Owner

kurenn commented Jun 8, 2015

remove then the :registrable module from the user model that should work!

@EchoLiao
Copy link
Author

EchoLiao commented Jun 8, 2015

I just try it, it worked! Many thanks!

@EchoLiao
Copy link
Author

EchoLiao commented Jun 8, 2015

I think the forms GET routes should remove too.
Such as:

   new_user_password GET    /users/password/new(.:format)          devise/passwords#new
   edit_user_password GET    /users/password/edit(.:format)         devise/passwords#edit

but retain the POST routes:

                      PATCH  /users/password(.:format)              devise/passwords#update
                      PUT    /users/password(.:format)              devise/passwords#update

How can i do it?

@kurenn
Copy link
Owner

kurenn commented Jun 8, 2015

It has to be with the devise modules, maybe the recoverable? or some other....

@janotterstetter
Copy link

Hi,

from reading this I think one should link this issue with the following issue: #8

Seems that they're about the same things regarding devise routes an the apionrails user controller routes.

Any idea how to solve the "duplicate" routes?
From my point of view the don't use devise options like "recoverable" won't solve the issue.

As I understand we choose devise for the authentication stuff and won't get rid of it.
What about not using the apionrails user controller routes?

Best

Jan

@kurenn
Copy link
Owner

kurenn commented Jun 17, 2015

Yeah that, could be a good solution, not to use the users_controller but we will have to override the one on devise.

A good solution would be as describe on issue #8 just move the devise_for :users call after the :api namespace...

@janotterstetter
Copy link

@kurenn thanks for your reply :-)

When I move the the devise_for :users call after the :api namespace the routes are listed beneath our :api path. Thats fine, but we do have duplicate routes now.

When I bundle exec rspec spec all tests passes. Thats fine.
But our specs are using our own user / session controllers and not the ones of devise.

Question:
Why we are not using the devise controllers?
Don't we want to use devise for the heavy authentication stuff, register a new user, sign in, sign out etc.?

Devise don't return json responses when used as is. Shouldnt we inherit from the devise controllers?

When I don't use the above "hack" (remove then the :registrable module from the user model) and try to use postman to create a user I'm getting "routing error - uninitialized constant Api::V1::RegistrationsController" due to the routes "mismatch" of our own and of the devise routes.

My rake routes:

Prefix Verb URI Pattern Controller#Action
    new_api_user_session GET    | /users/sign_in(.:format)       | api/v1/sessions#new {:format=>:json, :subdomain=>"api"}
        api_user_session POST   | /users/sign_in(.:format)       | api/v1/sessions#create {:format=>:json, :subdomain=>"api"}
destroy_api_user_session DELETE | /users/sign_out(.:format)      | api/v1/sessions#destroy {:format=>:json, :subdomain=>"api"}
       api_user_password POST   | /users/password(.:format)      | api/v1/passwords#create {:format=>:json, :subdomain=>"api"}
   new_api_user_password GET    | /users/password/new(.:format)  | api/v1/passwords#new {:format=>:json, :subdomain=>"api"}
  edit_api_user_password GET    | /users/password/edit(.:format) | api/v1/passwords#edit {:format=>:json, :subdomain=>"api"}
                         PATCH  | /users/password(.:format)      | api/v1/passwords#update {:format=>:json, :subdomain=>"api"}
                         PUT    | /users/password(.:format)      | api/v1/passwords#update {:format=>:json, :subdomain=>"api"}

cancel_api_user_registration GET | /users/cancel(.:format) | api/v1/registrations#cancel {:format=>:json, :subdomain=>"api"}
api_user_registration POST | /users(.:format) | api/v1/registrations#create {:format=>:json, :subdomain=>"api"}
new_api_user_registration GET | /users/sign_up(.:format) | api/v1/registrations#new {:format=>:json, :subdomain=>"api"}
edit_api_user_registration GET | /users/edit(.:format) | api/v1/registrations#edit {:format=>:json, :subdomain=>"api"}
PATCH | /users(.:format) | api/v1/registrations#update {:format=>:json, :subdomain=>"api"}
PUT | /users(.:format) | api/v1/registrations#update {:format=>:json, :subdomain=>"api"}
DELETE | /users(.:format) | api/v1/registrations#destroy {:format=>:json, :subdomain=>"api"}
api_users POST | /users(.:format) | api/v1/users#create {:format=>:json, :subdomain=>"api"}
api_user GET | /users/:id(.:format) | api/v1/users#show {:format=>:json, :subdomain=>"api"}
PATCH | /users/:id(.:format) | api/v1/users#update {:format=>:json, :subdomain=>"api"}
PUT | /users/:id(.:format) | api/v1/users#update {:format=>:json, :subdomain=>"api"}
DELETE | /users/:id(.:format) | api/v1/users#destroy {:format=>:json, :subdomain=>"api"}
api_sessions POST | /sessions(.:format) | api/v1/sessions#create {:format=>:json, :subdomain=>"api"}
api_session DELETE | /sessions/:id(.:format) | api/v1/sessions#destroy {:format=>:json, :subdomain=>"api"}

Question
I guess the crucial part to use of devise is the registration / password recover process etc.
So how can we use the crucial parts of devise and then use our "own" controllers for the "simple" stuff as showing, updating the user?

In future version one can image to use devise omniauth feature to register users with out api app that are google+ or twitter or etc. user.

Best

Jan

@oeddyo
Copy link

oeddyo commented Jul 6, 2015

@kurenn Also curious to @janotterstetter 's question. Could you help us out please?

@kurenn
Copy link
Owner

kurenn commented Jul 31, 2015

@janotterstetter Yeah you could use devise to register users, but you will have to override the registrations controller from devise to meet your needs, and on the tutorial I wanted to keep it super simple to register users.

Now to get the users controller create action to work, using postman:

screen shot 2015-07-31 at 12 17 16 pm

Please let me know how it goes, sorry for the delay on the answer!

@lesreaper
Copy link

I'm not sure how to test. I'm using localhost:3000, and none of my postman stuff works. Does this have to be loaded on a server to test the API?

@kurenn
Copy link
Owner

kurenn commented Nov 26, 2015

Do you have the subdomain setup on your routes file?

@lesreaper
Copy link

Yes.

@kurenn
Copy link
Owner

kurenn commented Nov 27, 2015

That is why, you have to change that to respond to a path instead of an api as discussed in here #52

@lesreaper
Copy link

Thanks for getting back to me last time. I appreciate it. I took a crack at your ticket suggestion and implemented it in my routes as seen below.

Now, I’m getting all kinds of timeouts and routing errors. I’m hoping that things are working as I go through, but I’m almost done with the book and have no idea if anything I’m doing is working on the API end, though the tests work great. I attached my log.

What am I doing wrong here?

Here is my routes

require 'api_constraints'

HapnApp::Application.routes.draw do

mount SabisuRails::Engine => "/sabisu_rails"

devise_for :users

namespace :api, path: '/api' do
scope module: :v1,
constraints: ApiConstraints.new(version: 1, default: true) do

  resources :users, :only => [:show, :create, :update, :destroy]
  resources :sessions, :only => [:create, :destroy]
  resources :events, :only => [:show, :index]


end

end
end

All I really need to know at the end of this is:

  1. What is the URL I’m going to access to get my resource information, both as I develop on my local machine and then again on Heroku?
  2. How do I test this URL as I’m creating my site?

I know this can’t be difficult once you learn it, but man, it seems a long way off.

Thanks,

Rich

On Nov 26, 2015, at 7:01 PM, Abraham <notifications@github.commailto:notifications@github.com> wrote:

That is why, you have to change that to respond to a path instead of an api as discussed in here #52#52


Reply to this email directly or view it on GitHubhttps://github.com//issues/33#issuecomment-160007925.

@kurenn
Copy link
Owner

kurenn commented Dec 14, 2015

@lesreaper Now you will have to access the resources like:

api/v1/users/ or whatever you want to access

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

5 participants