Permalink
Browse files

User registration and sign-in

  • Loading branch information...
RohitRox committed Jun 21, 2015
1 parent 58fa43d commit 10a17cb0c5a8e24015a5f2f77607b0e390f6acca
@@ -1,4 +1,11 @@
class Api::BaseController < ApplicationController
include ActionController::ImplicitRender
respond_to :json
-end
+
+ protected
+
+ def render_unauthorized(payload)
+ render json: payload.merge(response: { code: 401 })
+ end
+
+end
@@ -0,0 +1,14 @@
+class Api::RegistrationsController < Api::BaseController
+
+ def create
+ @user = User.new(user_params)
+ @user.save
+ end
+
+ private
+
+ def user_params
+ params.require(:user).permit(:email, :password, :password_confirmation)
+ end
+
+end
@@ -0,0 +1,25 @@
+class Api::SessionsController < Api::BaseController
+ before_filter :ensure_params_exist
+
+ def create
+ @user = User.find_for_database_authentication(email: user_params[:email])
+ return invalid_login_attempt unless @user
+ return invalid_login_attempt unless @user.valid_password?(user_params[:password])
+ end
+
+ protected
+
+ def user_params
+ params.require(:user).permit(:email, :password)
+ end
+
+ def ensure_params_exist
+ if user_params[:email].blank? || user_params[:password].blank?
+ return render_unauthorized errors: { unauthenticated: ["Incomplete credentials"] }
+ end
+ end
+
+ def invalid_login_attempt
+ render_unauthorized errors: { unauthenticated: ["Invalid credentials"] }
+ end
+end
@@ -0,0 +1,13 @@
+if @user.errors.present?
+ json.errors @user.errors.messages
+ json.response do
+ json.code 422
+ end
+else
+ json.data do
+ json.email @user.email
+ end
+ json.response do
+ json.code 201
+ end
+end
@@ -0,0 +1,6 @@
+json.data do
+ json.email @user.email
+end
+json.response do
+ json.code 200
+end
View
@@ -3,6 +3,7 @@
devise_for :users
namespace :api, defaults: { format: :json } do
resources :posts, except: [:new, :edit]
+ devise_for :users, :controllers => {sessions: 'api/sessions', registrations: 'api/registrations'}
end
mount Raddocs::App => "/api/docs"
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20150609034017) do
+ActiveRecord::Schema.define(version: 20150620203112) do
create_table "posts", force: :cascade do |t|
t.string "title"
@@ -20,4 +20,22 @@
t.datetime "updated_at", null: false
end
+ create_table "users", force: :cascade do |t|
+ t.string "email", default: "", null: false
+ t.string "encrypted_password", default: "", null: false
+ t.string "reset_password_token"
+ t.datetime "reset_password_sent_at"
+ t.datetime "remember_created_at"
+ t.integer "sign_in_count", default: 0, null: false
+ t.datetime "current_sign_in_at"
+ t.datetime "last_sign_in_at"
+ t.string "current_sign_in_ip"
+ t.string "last_sign_in_ip"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
+ add_index "users", ["email"], name: "index_users_on_email", unique: true
+ add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
+
end
View
@@ -34,6 +34,46 @@
"groups": "all"
}
]
+ },
+ {
+ "name": "Users",
+ "examples": [
+ {
+ "description": "Creating a new user with blank params",
+ "link": "users/creating_a_new_user_with_blank_params.json",
+ "groups": "all"
+ },
+ {
+ "description": "Creating a new user with invalid params",
+ "link": "users/creating_a_new_user_with_invalid_params.json",
+ "groups": "all"
+ },
+ {
+ "description": "Creating a new user with valid params",
+ "link": "users/creating_a_new_user_with_valid_params.json",
+ "groups": "all"
+ },
+ {
+ "description": "Signing in user with blank params",
+ "link": "users/signing_in_user_with_blank_params.json",
+ "groups": "all"
+ },
+ {
+ "description": "Signing in user with improper params",
+ "link": "users/signing_in_user_with_improper_params.json",
+ "groups": "all"
+ },
+ {
+ "description": "Signing in user with invalid password",
+ "link": "users/signing_in_user_with_invalid_password.json",
+ "groups": "all"
+ },
+ {
+ "description": "Signing in user with valid email and password",
+ "link": "users/signing_in_user_with_valid_email_and_password.json",
+ "groups": "all"
+ }
+ ]
}
]
}
@@ -45,8 +45,8 @@
"Content-Type": "application/json; charset=utf-8",
"ETag": "W/\"7c8ac7d7a81e80de2f74b51a33b85915\"",
"Cache-Control": "max-age=0, private, must-revalidate",
- "X-Request-Id": "106b89f3-6475-454e-b70b-e31353767e48",
- "X-Runtime": "0.027896",
+ "X-Request-Id": "05329767-e67c-4afa-897d-bf826a2b0aec",
+ "X-Runtime": "0.035359",
"Content-Length": "134"
},
"response_content_type": "application/json; charset=utf-8",
@@ -45,8 +45,8 @@
"Content-Type": "application/json; charset=utf-8",
"ETag": "W/\"691eec56b0faf743df98c78c11330d81\"",
"Cache-Control": "max-age=0, private, must-revalidate",
- "X-Request-Id": "d46c8456-b50c-4505-971e-04fef10527c0",
- "X-Runtime": "0.008210",
+ "X-Request-Id": "91c7750f-905a-40c9-a6af-2a5d9dbdd3f1",
+ "X-Runtime": "0.005697",
"Content-Length": "154"
},
"response_content_type": "application/json; charset=utf-8",
@@ -34,8 +34,8 @@
"Content-Type": "application/json; charset=utf-8",
"ETag": "W/\"ac74fb0159d8224a1689d5eeeb10b55e\"",
"Cache-Control": "max-age=0, private, must-revalidate",
- "X-Request-Id": "0c6533e1-03e1-4dcd-8683-ac7b785518a0",
- "X-Runtime": "0.016291",
+ "X-Request-Id": "be780fb8-8b3f-4000-8d12-d5bed9de8c18",
+ "X-Runtime": "0.013113",
"Content-Length": "25"
},
"response_content_type": "application/json; charset=utf-8",
@@ -53,17 +53,17 @@
"request_content_type": "application/json",
"response_status": 200,
"response_status_text": "OK",
- "response_body": "{\"data\":[{\"id\":2,\"title\":\"Iure dolore omnis rerum est.\",\"content\":\"Vel alias quos magnam quisquam hic sit. Quaerat et non sit quas. Magni neque soluta sapiente iusto quod sed. Minus reprehenderit ipsam et assumenda esse.\"},{\"id\":1,\"title\":\"Natus molestiae vel voluptatem cumque ut.\",\"content\":\"Vel alias quos magnam quisquam hic sit. Quaerat et non sit quas. Magni neque soluta sapiente iusto quod sed. Minus reprehenderit ipsam et assumenda esse.\"}],\"meta\":{\"current_page\":1,\"next_page\":null,\"prev_page\":null,\"total_pages\":1,\"total_count\":2,\"sort\":\"created_at\",\"order\":\"desc\"}}",
+ "response_body": "{\"data\":[{\"id\":2,\"title\":\"Numquam pariatur ipsum magnam ut tempore mollitia sit iure.\",\"content\":\"Porro tempora sunt fugit culpa. Quibusdam expedita reiciendis blanditiis omnis voluptatum recusandae. Dolor sit nobis aliquam dolorem a est tenetur distinctio. Dicta vitae quos expedita corrupti est.\"},{\"id\":1,\"title\":\"Eveniet dolor nulla iure sunt enim aspernatur cumque.\",\"content\":\"Porro tempora sunt fugit culpa. Quibusdam expedita reiciendis blanditiis omnis voluptatum recusandae. Dolor sit nobis aliquam dolorem a est tenetur distinctio. Dicta vitae quos expedita corrupti est.\"}],\"meta\":{\"current_page\":1,\"next_page\":null,\"prev_page\":null,\"total_pages\":1,\"total_count\":2,\"sort\":\"created_at\",\"order\":\"desc\"}}",
"response_headers": {
"X-Frame-Options": "SAMEORIGIN",
"X-XSS-Protection": "1; mode=block",
"X-Content-Type-Options": "nosniff",
"Content-Type": "application/json; charset=utf-8",
- "ETag": "W/\"063237b8ee88b426d9a443c1d6a19f9f\"",
+ "ETag": "W/\"e06c92fdfc71ee4bea77a997f1e3a187\"",
"Cache-Control": "max-age=0, private, must-revalidate",
- "X-Request-Id": "2a3344a3-a01d-46e9-85a2-1503a7159ebf",
- "X-Runtime": "0.037717",
- "Content-Length": "578"
+ "X-Request-Id": "e2425e8c-0f03-4ca2-aaf6-757c2c6ceb9f",
+ "X-Runtime": "0.046905",
+ "Content-Length": "713"
},
"response_content_type": "application/json; charset=utf-8",
"curl": null
@@ -45,8 +45,8 @@
"Content-Type": "application/json; charset=utf-8",
"ETag": "W/\"7c8ac7d7a81e80de2f74b51a33b85915\"",
"Cache-Control": "max-age=0, private, must-revalidate",
- "X-Request-Id": "a17c6e69-a649-41f2-b957-ec58157bada4",
- "X-Runtime": "0.029898",
+ "X-Request-Id": "badb442b-06ec-498a-a294-bea83deaceca",
+ "X-Runtime": "0.023467",
"Content-Length": "134"
},
"response_content_type": "application/json; charset=utf-8",
@@ -45,8 +45,8 @@
"Content-Type": "application/json; charset=utf-8",
"ETag": "W/\"74967b4ccbef6294000347ef90ca027f\"",
"Cache-Control": "max-age=0, private, must-revalidate",
- "X-Request-Id": "e5475e33-9ff1-422f-95fc-c87f256f2c39",
- "X-Runtime": "0.006558",
+ "X-Request-Id": "9b93b605-a502-4ea5-9870-2d76072b66ca",
+ "X-Runtime": "0.007030",
"Content-Length": "158"
},
"response_content_type": "application/json; charset=utf-8",
@@ -0,0 +1,56 @@
+{
+ "resource": "Users",
+ "http_method": "POST",
+ "route": "/api/users",
+ "description": "Creating a new user with blank params",
+ "explanation": null,
+ "parameters": [
+ {
+ "required": true,
+ "scope": "user",
+ "name": "email",
+ "description": "Email"
+ },
+ {
+ "required": true,
+ "scope": "user",
+ "name": "password",
+ "description": "Password"
+ }
+ ],
+ "response_fields": [
+
+ ],
+ "requests": [
+ {
+ "request_method": "POST",
+ "request_path": "/api/users",
+ "request_body": "{\"user\":{\"email\":\"\",\"password\":\"\"}}",
+ "request_headers": {
+ "Accept": "application/json",
+ "Content-Type": "application/json",
+ "Host": "example.org",
+ "Cookie": ""
+ },
+ "request_query_parameters": {
+ },
+ "request_content_type": "application/json",
+ "response_status": 200,
+ "response_status_text": "OK",
+ "response_body": "{\"errors\":{\"email\":[\"can't be blank\"],\"password\":[\"can't be blank\"]},\"response\":{\"code\":422}}",
+ "response_headers": {
+ "X-Frame-Options": "SAMEORIGIN",
+ "X-XSS-Protection": "1; mode=block",
+ "X-Content-Type-Options": "nosniff",
+ "Content-Type": "application/json; charset=utf-8",
+ "ETag": "W/\"f7013e28846dfb6dd0778f47f7c253e4\"",
+ "Cache-Control": "max-age=0, private, must-revalidate",
+ "X-Request-Id": "79ff0968-d7f4-4708-a5cc-b15111014534",
+ "X-Runtime": "0.005107",
+ "Content-Length": "93"
+ },
+ "response_content_type": "application/json; charset=utf-8",
+ "curl": null
+ }
+ ]
+}
@@ -0,0 +1,56 @@
+{
+ "resource": "Users",
+ "http_method": "POST",
+ "route": "/api/users",
+ "description": "Creating a new user with invalid params",
+ "explanation": null,
+ "parameters": [
+ {
+ "required": true,
+ "scope": "user",
+ "name": "email",
+ "description": "Email"
+ },
+ {
+ "required": true,
+ "scope": "user",
+ "name": "password",
+ "description": "Password"
+ }
+ ],
+ "response_fields": [
+
+ ],
+ "requests": [
+ {
+ "request_method": "POST",
+ "request_path": "/api/users",
+ "request_body": "{\"user\":{\"email\":\"email@examplecom\",\"password\":\"123456\"}}",
+ "request_headers": {
+ "Accept": "application/json",
+ "Content-Type": "application/json",
+ "Host": "example.org",
+ "Cookie": ""
+ },
+ "request_query_parameters": {
+ },
+ "request_content_type": "application/json",
+ "response_status": 200,
+ "response_status_text": "OK",
+ "response_body": "{\"errors\":{\"email\":[\"is invalid\"],\"password\":[\"is too short (minimum is 8 characters)\"]},\"response\":{\"code\":422}}",
+ "response_headers": {
+ "X-Frame-Options": "SAMEORIGIN",
+ "X-XSS-Protection": "1; mode=block",
+ "X-Content-Type-Options": "nosniff",
+ "Content-Type": "application/json; charset=utf-8",
+ "ETag": "W/\"d51af62289f965fc828d6587d6712275\"",
+ "Cache-Control": "max-age=0, private, must-revalidate",
+ "X-Request-Id": "b993ec55-08b6-41a1-b563-3b8326ed603f",
+ "X-Runtime": "0.006219",
+ "Content-Length": "113"
+ },
+ "response_content_type": "application/json; charset=utf-8",
+ "curl": null
+ }
+ ]
+}
Oops, something went wrong.

0 comments on commit 10a17cb

Please sign in to comment.