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

Add api token #22

Merged
merged 1 commit into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/controllers/v1/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def auth_user
"Please sign in",
"Please sign in"
) and return unless current_user


end

def message_success(message, content)
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/v1/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ def create
) and return unless user.valid_password?(session_params[:password])

sign_in(user)

render json: message_success(
"Sign in successfully",
"Sign in successfully"
)
user.send(:generate_token)

render json: message_success("Sign in successfully", {api_token: user.api_token})
end

def destroy
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class V1::UsersController < V1::BaseController
def create
user = User.create!(user_params)

render json: message_success("Sign up successfully", user)
render json: message_success("Sign up successfully", {api_token: user.api_token})
end

private
Expand Down
12 changes: 12 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,16 @@ class User < ActiveRecord::Base

has_many :posts
has_many :comments

before_create :generate_token

private
def time_expire
(Time.current + 30.days).to_i
end

def generate_token
self.api_token = SecureRandom.urlsafe_base64(nil, false)
self.expire_at = time_expire
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddApiTokenAndExpireAtToUsers < ActiveRecord::Migration
def change
add_column :users, :api_token, :string
add_column :users, :expire_at, :integer
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150917162231) do
ActiveRecord::Schema.define(version: 20150917165426) do

create_table "comments", force: :cascade do |t|
t.string "content", limit: 255
Expand Down Expand Up @@ -47,6 +47,8 @@
t.string "last_sign_in_ip", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "api_token", limit: 255
t.integer "expire_at", limit: 4
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
Expand Down