market_place_api icon indicating copy to clipboard operation
market_place_api copied to clipboard

How to create(Sign Up) a new user?

Open EchoLiao opened this issue 9 years ago • 20 comments

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 avatar Jun 07 '15 03:06 EchoLiao

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

EchoLiao avatar Jun 07 '15 09:06 EchoLiao

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

kurenn avatar Jun 08 '15 15:06 kurenn

     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?

EchoLiao avatar Jun 08 '15 15:06 EchoLiao

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

kurenn avatar Jun 08 '15 16:06 kurenn

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?

EchoLiao avatar Jun 08 '15 16:06 EchoLiao

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

kurenn avatar Jun 08 '15 16:06 kurenn

I just try it, it worked! Many thanks!

EchoLiao avatar Jun 08 '15 16:06 EchoLiao

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?

EchoLiao avatar Jun 08 '15 16:06 EchoLiao

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

kurenn avatar Jun 08 '15 17:06 kurenn

Hi,

from reading this I think one should link this issue with the following issue: https://github.com/kurenn/market_place_api/issues/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

janotterstetter avatar Jun 17 '15 15:06 janotterstetter

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...

kurenn avatar Jun 17 '15 19:06 kurenn

@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

janotterstetter avatar Jun 18 '15 08:06 janotterstetter

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

oeddyo avatar Jul 06 '15 06:07 oeddyo

@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!

kurenn avatar Jul 31 '15 17:07 kurenn

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?

lesreaper avatar Nov 26 '15 02:11 lesreaper

Do you have the subdomain setup on your routes file?

kurenn avatar Nov 26 '15 23:11 kurenn

Yes.

lesreaper avatar Nov 26 '15 23:11 lesreaper

That is why, you have to change that to respond to a path instead of an api as discussed in here https://github.com/kurenn/market_place_api/issues/52

kurenn avatar Nov 27 '15 00:11 kurenn

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 <[email protected]mailto:[email protected]> wrote:

That is why, you have to change that to respond to a path instead of an api as discussed in here #52https://github.com/kurenn/market_place_api/issues/52

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

lesreaper avatar Dec 09 '15 14:12 lesreaper

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

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

kurenn avatar Dec 14 '15 03:12 kurenn